美文网首页iOS开发iOS 开发动画
iOS动画篇_UIBezierPath(贝塞尔曲线)

iOS动画篇_UIBezierPath(贝塞尔曲线)

作者: 穿山甲救蛇精 | 来源:发表于2017-03-28 17:48 被阅读4023次

    很多人都说我动画做的贼6,但是不知道CALayer的原理,也不知道CALyer常用的几个子类,也并不知道贝塞尔曲线到底是怎么拐弯的,但是......我动画做的真的特别牛逼。

    UIBezierPath

    注:当贝塞尔曲线于CAShapeLayer混合使用时,共同作用的属性只遵循CAShapeLayer的设置!!!


    有时候,看到一些陌生的东西会有莫名的抵触感,不知道怎么入手,其实最简单的,学习一个东西,我们就要先看系统提供的API,首先,创建贝塞尔曲线的几个方法和属性:

    + (instancetype)bezierPath;   //初始化贝塞尔曲线(无形状)
    + (instancetype)bezierPathWithRect:(CGRect)rect;  //绘制矩形贝塞尔曲线
    + (instancetype)bezierPathWithOvalInRect:(CGRect)rect;  //绘制椭圆(圆形)曲线
    + (instancetype)bezierPathWithRoundedRect:(CGRect)rect cornerRadius:(CGFloat)cornerRadius; // 绘制含有圆角的贝塞尔曲线
    + (instancetype)bezierPathWithRoundedRect:(CGRect)rect byRoundingCorners:(UIRectCorner)corners cornerRadii:(CGSize)cornerRadii;  //绘制可选择圆角方位的贝塞尔曲线
    + (instancetype)bezierPathWithArcCenter:(CGPoint)center radius:(CGFloat)radius startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle clockwise:(BOOL)clockwise;   //绘制圆弧曲线
    + (instancetype)bezierPathWithCGPath:(CGPathRef)CGPath; //根据CGPathRef绘制贝塞尔曲线
    
    - (void)moveToPoint:(CGPoint)point;  //贝塞尔曲线开始的点
    - (void)addLineToPoint:(CGPoint)point;  //添加直线到该点
    - (void)addCurveToPoint:(CGPoint)endPoint controlPoint1:(CGPoint)controlPoint1 controlPoint2:(CGPoint)controlPoint2;  //添加二次曲线到该点
    - (void)addQuadCurveToPoint:(CGPoint)endPoint controlPoint:(CGPoint)controlPoint; //添加曲线到该点
    - (void)addArcWithCenter:(CGPoint)center radius:(CGFloat)radius startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle clockwise:(BOOL)clockwise NS_AVAILABLE_IOS(4_0);  //添加圆弧 注:上一个点会以直线的形式连接到圆弧的起点
    - (void)closePath;  //闭合曲线
    
    - (void)removeAllPoints; //去掉所有曲线点
    
    @property(nonatomic) CGFloat lineWidth;  //边框宽度
    @property(nonatomic) CGLineCap lineCapStyle;  //端点类型
    @property(nonatomic) CGLineJoin lineJoinStyle;  //线条连接类型
    @property(nonatomic) CGFloat miterLimit;  //线条最大宽度最大限制
    - (void)setLineDash:(nullable const CGFloat *)pattern count:(NSInteger)count phase:(CGFloat)phase;  //虚线类型
    - (void)fill;  //填充贝塞尔曲线内部
    - (void)stroke; //绘制贝塞尔曲线边框
    

    我们创建一个自定义的View,在drawRect:中进行各种贝塞尔曲线绘制:

    - (void)drawRect:(CGRect)rect {
        //矩形贝塞尔曲线
        UIBezierPath* bezierPath_rect = [UIBezierPath bezierPathWithRect:CGRectMake(30, 50, 100, 100)];
        [bezierPath_rect moveToPoint:CGPointMake(60, 60)];
        [bezierPath_rect addLineToPoint:CGPointMake(80, 80)];
        [bezierPath_rect addLineToPoint:CGPointMake(60, 90)];
        //[bezierPath_rect closePath];
        //[bezierPath_rect removeAllPoints];
        bezierPath_rect.lineCapStyle = kCGLineCapButt;  //端点类型
        bezierPath_rect.lineJoinStyle = kCGLineJoinMiter;  //线条连接类型
        bezierPath_rect.miterLimit = 1;
        CGFloat dash[] = {20,1};
        [bezierPath_rect setLineDash:dash count:2 phase:0];
        bezierPath_rect.lineWidth = 10;
        //圆形 椭圆贝塞尔曲线
        UIBezierPath *bezierPath_oval = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(200, 50, 150, 100)];
        bezierPath_oval.lineWidth = 10;
        //还有圆角的贝塞尔曲线
        UIBezierPath *bezierPath_RoundedRect = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(30, 200, 100, 100) cornerRadius:20];
        bezierPath_RoundedRect.lineWidth = 10;
        //绘制可选择圆角方位的贝塞尔曲线
        UIBezierPath *bezierPath_RoundedCornerRect = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(200, 200, 100, 100) byRoundingCorners:UIRectCornerTopLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(20, 20)];
        bezierPath_RoundedCornerRect.lineWidth = 10;
        //绘制圆弧曲线
        UIBezierPath *bezierPath_ArcCenter = [UIBezierPath bezierPathWithArcCenter:CGPointMake(0, 400) radius:50 startAngle:M_PI / 2 * 3 endAngle:M_PI / 3 clockwise:YES];
        bezierPath_ArcCenter.lineWidth = 10;
        //添加二次 三次贝塞尔曲线
        UIBezierPath *bezierPath = [UIBezierPath bezierPath];
        bezierPath.lineWidth = 2;
        [bezierPath moveToPoint:CGPointMake(10, 520)];
        [bezierPath addLineToPoint:CGPointMake(50, 530)];
        [bezierPath addQuadCurveToPoint:CGPointMake(100, 510) controlPoint:CGPointMake(80, 650)];
        [bezierPath addCurveToPoint:CGPointMake(200, 530) controlPoint1:CGPointMake(130, 600) controlPoint2:CGPointMake(170, 400)];
        [bezierPath addArcWithCenter:CGPointMake(300, 400) radius:50 startAngle:0 endAngle:M_PI * 2 clockwise:YES];
        
        [bezierPath moveToPoint:CGPointMake(20, 520)];
        [bezierPath addLineToPoint:CGPointMake(40, 520)];
        //根据CGPathRef绘制贝塞尔曲线
        CGMutablePathRef path = CGPathCreateMutable();
        CGPathMoveToPoint(path, NULL, 10, 640);
        CGPathAddCurveToPoint(path, NULL, 100, 700, 250, 550, 350, 650);
        UIBezierPath *bezierPath_CGPath = [UIBezierPath bezierPathWithCGPath:path];
        bezierPath_CGPath.lineWidth = 4;
        //选择填充颜色
        [[UIColor redColor] set];
        [bezierPath_rect fill];
        [bezierPath_oval fill];
        [bezierPath_RoundedRect fill];
        [bezierPath_RoundedCornerRect fill];
        //[bezierPath_ArcCenter fill];
        //[bezierPath_CGPath fill];
    
        //选择线条颜色
        [[UIColor blackColor] set];
        [bezierPath_rect stroke];
        [bezierPath_oval stroke];
        [bezierPath_RoundedRect stroke];
        [bezierPath_RoundedCornerRect stroke];
        [bezierPath_ArcCenter stroke];
        [bezierPath stroke];
        [bezierPath_CGPath stroke];
        //
        CALayer* aniLayer = [CALayer layer];
        aniLayer.backgroundColor = [UIColor redColor].CGColor;
        aniLayer.position = CGPointMake(10, 520);
        aniLayer.bounds = CGRectMake(0, 0, 8, 8);
        aniLayer.cornerRadius = 4;
        [self.layer addSublayer:aniLayer];
        //
        CAKeyframeAnimation* keyFrameAni = [CAKeyframeAnimation animationWithKeyPath:@"position"];
        keyFrameAni.repeatCount = NSIntegerMax;
        keyFrameAni.path = bezierPath.CGPath;
        keyFrameAni.duration = 15;
        keyFrameAni.beginTime = CACurrentMediaTime() + 1;
        [aniLayer addAnimation:keyFrameAni forKey:@"keyFrameAnimation"];
    }
    

    运行结果:


    看完是不是记不住?记了半天是不是明天就忘了?哦吼吼!!!

    这些基本包含了系统UIBezierPath类所有可以创建的曲线类型,当然如果你通过CGPathRef来创建曲线的话,就拥有了无限可能。看到这就完了,是不是觉得太少了,是不是没有你想要的东西,那就对了,欲知后事如何,且听下回分解!

    相关文章

      网友评论

      本文标题:iOS动画篇_UIBezierPath(贝塞尔曲线)

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