#####摘要:執行動畫的本質是改變圖層的屬性git
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ //開啓動畫 CABasicAnimation *basic=[CABasicAnimation animation]; //描述哪一個屬性產生動畫 basic.keyPath=@"position"; //設置值 basic.toValue=[NSValue valueWithCGPoint:CGPointMake(200, 450)]; //設置動畫完成時候再也不移除動畫 basic.removedOnCompletion=NO; //設置動畫執行完成要保持最新的效果 basic.fillMode=kCAFillModeForwards; //添加動畫 [self.greenView.layer addAnimation:basic forKey:nil]; }
#####補充:github
#####2.CAKeyframeAnimation數組
- (void)drawRect:(CGRect)rect { [self.path stroke]; } -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ UITouch *touch=[touches anyObject]; CGPoint p=[touch locationInView:self]; self.path=[UIBezierPath bezierPath]; [self.path moveToPoint:p]; } -(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ UITouch *touch=[touches anyObject]; CGPoint p=[touch locationInView:self]; [self.path addLineToPoint:p]; [self setNeedsDisplay]; } -(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ //開啓動畫 // CAKeyframeAnimation *keyFrame=[[CAKeyframeAnimation alloc]init]; CAKeyframeAnimation *keyFrame=[CAKeyframeAnimation animation]; keyFrame.keyPath=@"position"; keyFrame.path=self.path.CGPath; keyFrame.duration=1; keyFrame.repeatCount=MAXFLOAT; //添加動畫 [self.subviews.firstObject.layer addAnimation:keyFrame forKey:nil]; }
#####3.CATransition #####3.1 #####3.2代碼架構
static int i=2; -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ if (i>3) { i=2; } NSString *imageName=[NSString stringWithFormat:@"%d",i]; self.imageView.image=[UIImage imageNamed:imageName]; CATransition *transaction=[CATransition animation]; transaction.type=@"pageCurl"; [self.imageView.layer addAnimation:transaction forKey:nil]; i++; }
#####4.CAAnimationGroup #####4.1注意併發
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ CAAnimationGroup *animGroups=[CAAnimationGroup animation]; CABasicAnimation *basic=[CABasicAnimation animation]; CABasicAnimation *basic2=[CABasicAnimation animation]; //removedOnCompletion無效果 basic.keyPath=@"position"; basic.toValue=[NSValue valueWithCGPoint:CGPointMake(150, 400)]; // basic.removedOnCompletion=NO; // basic.fillMode=kCAFillModeForwards; // basic2.keyPath=@"transform.scale"; basic2.toValue=@0.5; // basic2.removedOnCompletion=NO; // basic2.fillMode=kCAFillModeForwards; animGroups.animations=@[basic,basic2]; animGroups.removedOnCompletion=NO; animGroups.fillMode=kCAFillModeForwards; [self.redView.layer addAnimation:animGroups forKey:nil]; }
#####5.詳細的源碼地址(如下是github地址)動畫