动画, 画板

作者: LST程序员 | 来源:发表于2016-08-20 11:53 被阅读0次
    2016-08-20 11_25_22.gif
    属性
    /**贝塞尔曲线 */
    @property (nonatomic, retain) UIBezierPath *bezier;
    /**用来记录线的位置*/
    @property (nonatomic, retain) NSMutableArray *beziers;
    /** 子layer层*/
    @property (nonatomic, retain) CALayer *subLayer;```
    ######自定义初始化
    

    -(CALayer *)subLayer{
    if (!_subLayer) {
    self.subLayer = [[CALayer alloc] init];
    self.subLayer.backgroundColor = [UIColor redColor].CGColor;
    self.subLayer.bounds = CGRectMake(0, 0, 10, 10);
    self.subLayer.cornerRadius = 5;
    [self.layer addSublayer:_subLayer];
    }
    return _subLayer;
    }
    -(NSMutableArray *)beziers{
    if (!_beziers) {
    self.beziers = [NSMutableArray array]; }
    return _beziers; }
    -(CGPoint)getPointFromTouches:(NSSet<UITouch *> *)touches{
    UITouch * touch = [touches anyObject];
    CGPoint curP = [touch locationInView:self];
    return curP;
    }

    ###### 想实现画图的功能
    // 1 自定义View
    // 2 实现touch相关的方法
    

    -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    CGPoint curp =
    [self getPointFromTouches:touches];
    // 绘图相关的类( UIBezierPath 贝塞尔曲线)
    self.bezier = [UIBezierPath bezierPath];
    // 开始点
    [self.bezier moveToPoint:curp];
    }
    -(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

    CGPoint moveP = [self getPointFromTouches:touches];
    [self.bezier addLineToPoint:moveP];
    self.bezier.lineWidth = 5;
    // 手动调用
    [self setNeedsDisplay];
    // 把每次的贝塞尔曲线添加到数组里
    [self.beziers addObject:self.bezier];
    

    }
    -(void)start{
    // 关键帧动画
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animation];
    animation.keyPath = @"position";
    // 动画的路径为贝塞尔曲线
    animation.path = self.bezier.CGPath;
    animation.repeatCount = MAXFLOAT;
    animation.duration = 10;
    // 将动画添加到子layer层
    [self.subLayer addAnimation:animation forKey:nil];
    // CAReplicatorLayer 复制图层
    }

    /** 在UIView上绘图的系统方法*/
    

    -(void)drawRect:(CGRect)rect{
    for (UIBezierPath *path in self.beziers) {
    [path stroke];
    }
    }

    相关文章

      网友评论

        本文标题:动画, 画板

        本文链接:https://www.haomeiwen.com/subject/bskisttx.html