• 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. }
  • 相关阅读:
    uniapp
    vue -element admin 修改request,headers添加参数
    uniapp
    css
    uniapp
    uniapp
    vue
    vue
    vue -element 修复select下拉框在移动端需要点击两次才能选中的问题
    vue
  • 原文地址:https://www.cnblogs.com/Xfsrn/p/5000349.html
Copyright © 2020-2023  润新知