关于CABasicAnimation 与 帧CAKeyframeAnimation 结合使用

 

 1, 使用动画组合 CAAnimationGroup 把帧 ,动画  放置一起播放。

- (IBAction)tranAction:(id)sender {
    CGPoint fromPoint = self.imageView.center;
    
    clickCount ++ ;
    //路径曲线
    UIBezierPath *movePath = [UIBezierPath bezierPath];
    [movePath moveToPoint:fromPoint];
    CGPoint toPoint = CGPointMake(300, [UIScreen mainScreen].bounds.size.height - 20);
    [movePath addQuadCurveToPoint:toPoint
                     controlPoint:CGPointMake(300,0)];
    //关键帧
    CAKeyframeAnimation *moveAnim = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    moveAnim.path = movePath.CGPath;
    moveAnim.removedOnCompletion = YES;
    
    //旋转变化
    CABasicAnimation *scaleAnim = [CABasicAnimation animationWithKeyPath:@"transform"];
    scaleAnim.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];
    //x,y轴缩小到0.1,Z 轴不变
    scaleAnim.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(0.10.11)];
    scaleAnim.removedOnCompletion = YES;
    
   //旋转
    CABasicAnimation* rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotationAnimation.toValue = [NSNumber numberWithFloat:(2 * M_PI) *1];
    rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    rotationAnimation.removedOnCompletion = YES;
    rotationAnimation.duration = 0.9;
    
    
 //缩放四周
//   CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
//   scaleAnimation.fromValue = [NSNumber numberWithFloat:1.0];
//   scaleAnimation.toValue = [NSNumber numberWithFloat:0.0];
//   scaleAnimation.duration = 1.0f;
//   scaleAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
//    
    
//透明度变化
    CABasicAnimation *opacityAnim = [CABasicAnimation animationWithKeyPath:@"alpha"];
    opacityAnim.fromValue = [NSNumber numberWithFloat:1.0];
    opacityAnim.toValue = [NSNumber numberWithFloat:0.1];
    opacityAnim.removedOnCompletion = YES;
 
    
    //关键帧,旋转,透明度组合起来执行
    CAAnimationGroup *animGroup = [CAAnimationGroup animation];
    if(clickCount%2 == 0){
        animGroup.animations = [NSArray arrayWithObjects:moveAnim, scaleAnim,opacityAnim,rotationAnimation, nil];
    }else{
         animGroup.animations = [NSArray arrayWithObjects:moveAnim, scaleAnim,opacityAnim, nil];
    }
    animGroup.duration = 1;
    [self.imageView.layer addAnimation:animGroup forKey:nil];

} 

 旋转360 度:

- (IBAction)Rotate360Action:(id)sender {
    //图片旋转360度
    CABasicAnimation *animation = [ CABasicAnimation
                                   animationWithKeyPath: @"transform" ];
    animation.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];
    
    //围绕Z轴旋转,垂直与屏幕
    animation.toValue = [ NSValue valueWithCATransform3D:
                         CATransform3DMakeRotation(M_PI, 001.0) ];
    animation.duration = 1.5;
    //旋转效果累计,先转180度,接着再旋转180度,从而实现360旋转
    animation.cumulative = YES;
    animation.repeatCount = 2;
    
    //在图片边缘添加一个像素的透明区域,去图片锯齿
    CGRect imageRrect = CGRectMake(00, self.imageView.frame.size.width, self.imageView.frame.size.height);
    UIGraphicsBeginImageContext(imageRrect.size);
    [self.imageView.image drawInRect:CGRectMake(1,1,self.imageView.frame.size.width-2,self.imageView.frame.size.height-2)];
    self.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    [self.imageView.layer addAnimation:animation forKey:nil];

} 

 

posted @ 2015-10-23 14:26  iOS秋云  阅读(686)  评论(1)    收藏  举报