一.iOS贝塞尔曲线与CAShapeLayer详解

实现效果图如下:


实现彩虹条动画显示效果

一.创建一个贝塞尔曲线

1.基础方法,先创建UIBezierPath对象,在添加路径

+ (instancetype)bezierPath;

- (instancetype)init NS_DESIGNATED_INITIALIZER;

+(instancetype)new;

这三个方法都可以得到一个UIBezierPath对象

然后通过一下方法添加路径

- (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)clockwiseNS_AVAILABLE_IOS(4_0);//添加弧线有某点为弧心,可设置半径以及起始和结束角度,顺逆时针方向

- (void)closePath;//使路径封闭

2.类方法直接创建指定路径

+ (instancetype)bezierPathWithRect:(CGRect)rect;//创建一个在一定范围内封闭边沿路径

+ (instancetype)bezierPathWithOvalInRect:(CGRect)rect;//在一定范围内封闭的最大的圆形路径

+ (instancetype)bezierPathWithRoundedRect:(CGRect)rect cornerRadius:(CGFloat)cornerRadius;// rounds all corners with the same horizontal and vertical radius;//在一定范围内以一个指定半径的封闭圆形路径

+ (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;//以某点为中心一个半径为多少,起始角度与结束角度的弧形不封闭区域,可指定顺时针和逆时针YES表示顺,可通过- (void)closePath;方法封闭路径

+ (instancetype)bezierPathWithCGPath:(CGPathRef)CGPath;//指定某个绘制路径为路径的路径

二.CAShapeLayer属性

@property(nullable) CGPathRef path;

定义要呈现的形状的路径。如果路径延伸在图层边界之外它不会自动被剪切,只有在谁知clibToBounds的时候才会裁剪。默认为空。可以做成动画。(注意,尽管路径属性是可动画的,但没有隐式动画)

@property(nullable) CGColorRef strokeColor;

填充路径的颜色,或无填充为nil。默认为不透明的黑色。可以做成动画

@property(copy) NSString *lineCap;

填充路径时使用的填充规则。

CA_EXTERN NSString *const kCALineCapButt(路径未封闭端为平的并与区域齐平)

CA_EXTERN NSString *const kCALineCapRound(路径未封闭端为圆角的并超出区域一部分)

CA_EXTERN NSString *const kCALineCapSquare(路径未封闭端为平的并超出区域一部分)

以上超出部分为lineWidth属性的一半

@property CGFloat lineWidth;

路径宽度

@property CGFloat strokeStart;

@property CGFloat strokeEnd;

路径后开始和结束,值都在0~1之间,可以做动画

@property(nullable) CGColorRef fillColor;

@property(copy) NSString *fillRule;

区域颜色,样式

三.附上自己写的一个效果测试两者结合做的简单动画


简单动画效果


测试代码如下:(直接创建一个工程在mainstoryboard创建一个按钮,替换掉.m,加上点击事件可看效果)

////  ViewController.m//  HudTest////  Created by iOS on 17/6/8.//  Copyright © 2017年 iOS. All rights reserved.//

#import "ViewController.h"

@interface ViewController (){

CAShapeLayer *maskLayer;

CABasicAnimation *_animation;

}

@end

@implementation ViewController

- (IBAction)show:(id)sender {

//只点一次看看效果就好,想自己怎么做都好,我比较懒就不做清除判断了

UIBezierPath *bezierPath = [UIBezierPath bezierPathWithArcCenter:self.view.center radius:80 startAngle:0 endAngle:M_PI clockwise:YES];

maskLayer = [CAShapeLayer layer];

maskLayer.backgroundColor = [UIColor clearColor].CGColor;

maskLayer.path = bezierPath.CGPath;

maskLayer.strokeColor = [UIColor greenColor].CGColor;

maskLayer.fillColor = [UIColor whiteColor].CGColor;

maskLayer.lineWidth = 20;

maskLayer.fillRule = kCAFillRuleEvenOdd;

maskLayer.lineCap = kCALineCapRound;

[self.view.layer addSublayer:maskLayer];

maskLayer.strokeStart = 0;

maskLayer.strokeEnd = 1;

CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];

animation.fromValue = @0;

animation.duration = 3.0f;

animation.delegate = self;

animation.repeatCount = 0;

[animation setValue:@"BasicAnimationEnd" forKey:@"animationName"];

[maskLayer addAnimation:animation forKey:@"BasicAnimationEnd"];

}

- (void)animationDidStart:(CAAnimation *)anim{

if (_animation) {

UIBezierPath *bezierPath1 = [UIBezierPath bezierPathWithArcCenter:self.view.center radius:30 startAngle:M_PI endAngle:0 clockwise:NO];

maskLayer = [CAShapeLayer layer];

maskLayer.backgroundColor = [UIColor clearColor].CGColor;

maskLayer.path = bezierPath1.CGPath;

maskLayer.strokeColor = [UIColor redColor].CGColor;

maskLayer.fillColor = [UIColor whiteColor].CGColor;

maskLayer.lineWidth = 15;

maskLayer.fillRule = kCAFillRuleEvenOdd;

maskLayer.lineCap = kCALineCapRound;

[self.view.layer addSublayer:maskLayer];

maskLayer.strokeStart = 0;

maskLayer.strokeEnd = 1;

CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];

animation.fromValue = @0;

animation.duration = 0.8f;

animation.repeatCount = 0;

[animation setValue:@"BasicAnimationEnd" forKey:@"animationName"];

[maskLayer addAnimation:animation forKey:@"BasicAnimationEnd"];

}else{

UIBezierPath *bezierPath1 = [UIBezierPath bezierPathWithArcCenter:self.view.center radius:60 startAngle:M_PI endAngle:0 clockwise:NO];

maskLayer = [CAShapeLayer layer];

maskLayer.backgroundColor = [UIColor clearColor].CGColor;

maskLayer.path = bezierPath1.CGPath;

maskLayer.strokeColor = [UIColor yellowColor].CGColor;

maskLayer.fillColor = [UIColor whiteColor].CGColor;

maskLayer.lineWidth = 20;

maskLayer.fillRule = kCAFillRuleEvenOdd;

maskLayer.lineCap = kCALineCapRound;

[self.view.layer addSublayer:maskLayer];

maskLayer.strokeStart = 0;

maskLayer.strokeEnd = 1;

CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];

animation.fromValue = @0;

animation.duration = 2.0f;

animation.repeatCount = 0;

[animation setValue:@"BasicAnimationEnd" forKey:@"animationName"];

[maskLayer addAnimation:animation forKey:@"BasicAnimationEnd"];

}}

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{

if (!_animation) {

UIBezierPath *bezierPath2 = [UIBezierPath bezierPathWithArcCenter:self.view.center radius:45 startAngle:0 endAngle:M_PI clockwise:YES];

maskLayer = [CAShapeLayer layer];

maskLayer.backgroundColor = [UIColor clearColor].CGColor;

maskLayer.path = bezierPath2.CGPath;

maskLayer.strokeColor = [UIColor orangeColor].CGColor;

maskLayer.fillColor = [UIColor whiteColor].CGColor;

maskLayer.lineWidth = 15;

maskLayer.fillRule = kCAFillRuleEvenOdd;

maskLayer.lineCap = kCALineCapRound;

[self.view.layer addSublayer:maskLayer];

maskLayer.strokeStart = 0;

maskLayer.strokeEnd = 1;

_animation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];

_animation.fromValue = @0;

_animation.delegate = self;

_animation.duration = 1.0f;

[_animation setValue:@"BasicAnimationEnd" forKey:@"animationName"];

[maskLayer addAnimation:_animation forKey:@"BasicAnimationEnd"];

}

}

@end

Copyright © 2017年ZaneWangWang. All rights reserved.

持续更新中...

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 157,298评论 4 360
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 66,701评论 1 290
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 107,078评论 0 237
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,687评论 0 202
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,018评论 3 286
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,410评论 1 211
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,729评论 2 310
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,412评论 0 194
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,124评论 1 239
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,379评论 2 242
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,903评论 1 257
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,268评论 2 251
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,894评论 3 233
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,014评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,770评论 0 192
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,435评论 2 269
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,312评论 2 260

推荐阅读更多精彩内容