簡介
注意事項
values(NSArray *)
path(CGPathRef)
keyTimes(NSArray
timingFunctions(NSArray
calculationMode(NSString *)
rotationMode(NSString *)
效果圖
實現思路
實現步驟(自定義UIView的子類)
@property (nonatomic, strong) UIBezierPath *path;
監聽觸摸事件的狀態,繪制貝瑟爾曲線
//確定起點- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ //獲取當前觸摸點 UITouch *touch = [touches anyObject]; CGPoint curretnPoint = [touch locationInView:self]; //創建路徑 UIBezierPath *path = [UIBezierPath bezierPath]; [path moveToPoint:curretnPoint]; //保存路徑 self.path = path;}
//添加線條- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{ //獲取當前觸摸點 UITouch *touch = [touches anyObject]; CGPoint currentPoint = [touch locationInView:self]; //添加線條 [self.path addLineToPoint:currentPoint]; //重繪,將曲線顯示到圖層上 [self setNeedsDisplay];}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{ //創建動畫 CAKeyframeAnimation *animation = [CAKeyframeAnimation animation]; //指定執行動畫的屬性, animation.keyPath = @"position"; //設置動畫的執行路徑 animation.path = self.path.CGPath; //設置動畫的執行時間 animation.duration = 1; //設置動畫的重復次數 animation.repeatCount = MAXFLOAT; //將動畫添加到對應的圖層上 [[[self.subviews firstObject] layer] addAnimation:animation forKey:nil];}
將路徑顯示到圖層上
//繪制路徑- (void)drawRect:(CGRect)rect{ [self.path stroke];}
|
新聞熱點
疑難解答