UIBezierPath跟CAShapeLayer結(jié)合起來的用法

首先CAShapeLayer繼承于CALayer,這樣它就能夠在視圖上進(jìn)行畫圖,描繪什么的。UIBezierPath這個就是路徑,兩者結(jié)合起來就是把路徑描繪到視圖中。UIBezierPath的初始化方法有以下幾種:

+ (instancetype)bezierPath;

+ (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;//可以設(shè)置某個角的圓角

+ (instancetype)bezierPathWithArcCenter:(CGPoint)center radius:(CGFloat)radius startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle clockwise:(BOOL)clockwise;//畫弧

+ (instancetype)bezierPathWithCGPath:(CGPathRef)CGPath;



UIBezierPath添加描點的方法如下:(畫曲線)

- (void)addCurveToPoint:(CGPoint)endPoint controlPoint1:(CGPoint)controlPoint1 controlPoint2:(CGPoint)controlPoint2;

- (void)addQuadCurveToPoint:(CGPoint)endPoint controlPoint:(CGPoint)controlPoint;

其中endPoint: 終點。controlPoint: 控制點

用兩個方法前必須先設(shè)置moveToPoint;

ps,如果要設(shè)置控制點事圓滑的曲線還要多設(shè)置一個控制點,比如類似于聲波的曲線。

上代碼:




解釋下代碼的意思。

CAShapeLayer可以跟動畫一起使用,也就是CABasicAnimation;

CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];關(guān)鍵在這里設(shè)置KeyPath;

注意:CAAnimationGroup *groupAnimation = [CAAnimationGroup animation];

? ? ? ? ? ??groupAnimation.animations = @[animationStart, animationEnd];

也就是說可以多個動畫組合一起使用,也可以單獨使用,看具體的要求吧。

最下面的代碼類似下圖的效果:


?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容