iOS开发核心动画之画图板

1. 效果图



2. 用一个View来描述画图板,给画图板添加拖动的手势

 
  
  1. // 从xib中加载
  2. - (void)awakeFromNib
  3. {
  4. [self setUpGesture];
  5. }
  6. // 代码创建
  7. - (instancetype)initWithFrame:(CGRect)frame
  8. {
  9. if (self = [super initWithFrame:frame]) {
  10. [self setUpGesture];
  11. }
  12. return self;
  13. }
  14. // 初始化添加拖动手势
  15. - (void)setUpGesture
  16. {
  17. UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
  18. [self addGestureRecognizer:pan];
  19. self.color = [UIColor blackColor];
  20. self.width = 1;
  21. }

3. 监听手势方法

1> 判断拖动开始拖动设置为路径的的起始点,添加路径并保存路径,将当前路径添加到数组中

设置路径的相关属性(颜色/宽度/圆角)

2> 拖动过程中的点添加直线重绘

 
  
  1. // 监听手势方法
  2. - (void)pan:(UIPanGestureRecognizer *)pan
  3. {
  4. // 获取当前触摸点
  5. CGPoint curP = [pan locationInView:self];
  6. if (pan.state == UIGestureRecognizerStateBegan) { // 开始拖动
  7. // 获取开始点
  8. CGPoint startP = curP;
  9. // 创建路径
  10. LDBezierPath *path = [[LDBezierPath alloc] init];
  11. self.path = path;
  12. [self.pathArray addObject:path];
  13. // 设置宽度
  14. [path setLineWidth:self.width];
  15. // 设置颜色
  16. [path setLineColor:self.color];
  17. // 设置圆角
  18. [path setLineJoinStyle:kCGLineJoinRound];
  19. [path setLineCapStyle:kCGLineCapRound];
  20. // 添加起始点
  21. [path moveToPoint:startP];
  22. } else if (pan.state == UIGestureRecognizerStateChanged) {
  23. // 添加直线
  24. [self.path addLineToPoint:curP];
  25. // 重绘
  26. [self setNeedsDisplay];
  27. }
  28. }

4. 绘制, 遍历数组中的路径,判断是否是图片,如果是图片则将图片绘制上去,反之为路径,直接绘制路径

 
  
  1. // 绘制
  2. - (void)drawRect:(CGRect)rect {
  3. // 变量路径数组重绘
  4. for (LDBezierPath *path in self.pathArray) {
  5. if (path.class == [UIImage class]) {
  6. UIImage *image = (UIImage *)path;
  7. [image drawInRect:rect];
  8. NSLog(@"%@", image);
  9. } else {
  10. [path.lineColor set];
  11. [path stroke];
  12. }
  13. }
  14. }

5. 图片保存

 
  
  1. // 保存
  2. - (void)save
  3. {
  4. if (self.pathArray.count) {
  5. // 1.开启位图上下文
  6. UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 0);
  7. // 2.获取当前上下文
  8. CGContextRef ref = UIGraphicsGetCurrentContext();
  9. // 3.将view的layer渲染到位图上下文总
  10. [self.layer renderInContext:ref];
  11. // 4.获取位图上下文中的image
  12. UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
  13. // 5.关闭位图上下文
  14. UIGraphicsEndImageContext();
  15. // 6.将图片保存到相册
  16. UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
  17. } else {
  18. [MBProgressHUD showError:@"没有图片可保存"];
  19. }
  20. }
  21. - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
  22. {
  23. [MBProgressHUD showSuccess:@"保存成功"];
  24. }

转载于:https://www.cnblogs.com/Xfsrn/p/5000349.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值