IOS_CABasicAnimation

来源:互联网 发布:法国斗牛犬 知乎 编辑:程序博客网 时间:2024/06/02 03:19
原文地址:IOS_CABasicAnimation作者:青鳥Ayukyo
structure field:




keypath:
我们可以通过animationWithKeyPath键值对的方式来改变动画
animationWithKeyPath的值:
 transform.scale = 比例轉換
 transform.scale.x = 闊的比例轉換
 transform.scale.y = 高的比例轉換
 transform.rotation.z = 平面圖的旋轉
opacity = 透明度
 margin
zPosition
 backgroundColor
 cornerRadius
 borderWidth
bounds
contents
contentsRect
cornerRadius
 frame
 hidden
mask
 masksToBounds
 opacity
 position
shadowColor
 shadowOffset
shadowOpacity
shadowRadius


//
// 
ViewController.m
//  CABasicAnimationDemo
//
//  Created by user on 13-1-23.
//  Copyright (c) 2013年 user. All rightsreserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)dealloc
{
   [_animationKeyPaths release];
   [_button release];
   [_imageView release];
   
   [super dealloc];
}

- (void)viewDidLoad
{
   [super viewDidLoad];
   
   
//   @"translation",
//   @"translation.x",
//   @"translation.y",
//   @"translation.z",
//   
//   @"rotation",
//   @"rotation.x",
//   @"rotation.y",
//   @"rotation.z",
   
//   @"scale",
//   @"scale.x",
//   @"scale.y",
//   @"scale.z",
   
   
   _animationKeyPaths = [[NSArray alloc]initWithObjects:                      
                         @"opacity",                         
                         @"cornerRadius",                   
                         @"borderWidth",
                         @"zPosition",
                         @"backgroundColor",
                         @"bounds",
                         @"margin",
                         @"contents",
                         @"contentsRect",
                         @"mask",
                         @"masksToBounds",
                         @"frame",
                         @"hidden",
                         @"shadowColor",
                         @"shadowOffset",
                         @"shadowOpacity",
                         @"shadowRadius",
                         
                         @"transform",          //旋转(CATransform3D)
                         @"transform.rotation.x",
                         @"transform.rotation.y",
                         @"transform.rotation.z",//旋转
                         @"transform.scale",                         
                         @"transform.translation",//点移动                         
                         
                         @"position.x",         //横向移动
                         @"position.y",
                         
                         //下面的不是CABasicAnimation里的keyPath                         
                         @"position",           //路径动画(CAKeyframeAnimation)                         
                         
                         nil];
}

- (void)viewDidUnload
{
   [super viewDidUnload];
}

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
   return (interfaceOrientation !=UIInterfaceOrientationPortraitUpsideDown);
}

- (IBAction)changeAnimationStyle:(UIButton *)button{   
   if (button == _button) {
       static int i = 0;
       [_imageView.layer removeAnimationForKey:_animationKeyPath];
       //[_imageView.layer removeAllAnimations];
       _animationKeyPath = [_animationKeyPaths objectAtIndex:i];
       NSLog(@"%@", _animationKeyPath);
       
       [_button setTitle:_animationKeyPathforState:UIControlStateNormal];
       [_imageView.layer addAnimation:[self generateAnimation:i]forKey:_animationKeyPath];       
       i++;
       if (i >= _animationKeyPaths.count) {
           i = 0;
       }
       
       
   } else if (button == _groupBtn) {
       [_imageView.layer removeAnimationForKey:_animationKeyPath];
       //[_imageView.layer removeAllAnimations];
       _animationKeyPath = @"GroupAnimation";
       NSLog(@"%@", _animationKeyPath);
       
       CAAnimationGroup *groupAnimation = [CAAnimationGroupanimation];
       groupAnimation.fillMode = kCAFillModeBackwards;
       groupAnimation.removedOnCompletion = NO;
       groupAnimation.duration = 10000.25f;
       //groupAnimation.repeatCount = FLT_MAX;
       //groupAnimation.autoreverses = YES;
       
       [groupAnimation setAnimations:
        [NSArray arrayWithObjects:
         [self generateAnimation:0],
         //[self generateAnimation:1],
         //[self generateAnimation:2],
         //[self generateAnimation:3],
         //[self generateAnimation:4],
         nil]];
       //_imageView.layer.position =CGPointMake(_imageView.frame.size.width,_imageView.frame.size.height*0.5f);
       
       
       
       groupAnimation = (CAAnimationGroup *)[selfgenerateGroupAnimation];
       
       [_imageView.layer addAnimation:groupAnimationforKey:_animationKeyPath];
   }
}

- (CAAnimation *)generateGroupAnimation {
   CALayer *layer=[CALayer layer];
   layer.frame=CGRectMake(50, 200, 50, 50);
   layer.backgroundColor=[UIColor orangeColor].CGColor;
   layer.cornerRadius=8.0f;
   
   CABasicAnimation *animation=[CABasicAnimationanimationWithKeyPath:@"transform.translation.y"];
   animation.duration=4.0f;
   animation.autoreverses=NO;
   animation.repeatCount=1;
   animation.toValue=[NSNumber numberWithInt:-10];
   animation.fromValue=[NSNumber numberWithInt:200];
   animation.timingFunction = [CAMediaTimingFunctionfunctionWithName:kCAMediaTimingFunctionEaseInEaseOut];
   
   CABasicAnimation *animationZoomIn=[CABasicAnimationanimationWithKeyPath:@"transform.scale"];
   animationZoomIn.duration=2.0f;
   animationZoomIn.autoreverses = YES;
   //animationZoomIn.autoreverses=NO;
   animationZoomIn.repeatCount=1;
   animationZoomIn.toValue=[NSNumber numberWithFloat:1.56];
   animationZoomIn.timingFunction=[CAMediaTimingFunctionfunctionWithName:kCAMediaTimingFunctionEaseIn];
   CABasicAnimation *animationZoomOut=[CABasicAnimationanimationWithKeyPath:@"transform.scale"];
   //等待放大动画执行完
   animationZoomOut.beginTime=2.0f;
   animationZoomOut.duration=2.0f;
   animationZoomOut.autoreverses=NO;
   animationZoomOut.repeatCount=1;
   
   animationZoomOut.toValue=[NSNumber numberWithFloat:.01];
   animationZoomOut.timingFunction=[CAMediaTimingFunctionfunctionWithName:kCAMediaTimingFunctionEaseOut];
   
   CAAnimationGroup *groupAnimation = [CAAnimationGroupanimation];
   groupAnimation.duration=4.0f;
   groupAnimation.animations=[NSArray arrayWithObjects: animation,animationZoomIn, animationZoomOut,nil];
   groupAnimation.removedOnCompletion=NO;
   groupAnimation.fillMode=kCAFillModeForwards;
   //[layer addAnimation:groupAnimation forKey:nil];
   //[self.view.layer addSublayer:layer];
   
   return groupAnimation;
}

- (CAAnimation *)generateAnimation:(int)i {
   CABasicAnimation *animation = [CABasicAnimationanimationWithKeyPath:[_animationKeyPaths objectAtIndex:i]];
   
   //执行时间
   
   //反转动画
   animation.autoreverses = YES;
   //执行次数
   animation.repeatCount = FLT_MAX;
   animation.removedOnCompletion = NO;
   //animation.fillMode = kCAFillModeForwards;
   //动画开始结束的快慢,设置为加速
   animation.timingFunction = [CAMediaTimingFunctionfunctionWithName:kCAMediaTimingFunctionEaseInEaseOut];
   
   if ([_animationKeyPath isEqualToString:@"opacity"]) {
       animation.fromValue   = [NSNumber numberWithFloat:1.0f];
       animation.toValue     = [NSNumber numberWithFloat:0.0f];
       //animation.duration  = 1.0f;
   } else if ([_animationKeyPathisEqualToString:@"transform.rotation.z"]) {
       animation.fromValue   = [NSNumber numberWithFloat:0.0f];
       animation.toValue     = [NSNumber numberWithFloat:M_PI_2];
       animation.duration    = 1.0f;
       animation.autoreverses = NO;
       animation.timingFunction = [CAMediaTimingFunctionfunctionWithName:kCAMediaTimingFunctionLinear];
   } else if ([_animationKeyPath isEqualToString:@"position"]){
       CAKeyframeAnimation *keyFrameAnimation = [CAKeyframeAnimationanimationWithKeyPath:_animationKeyPath];
       
       
       keyFrameAnimation.repeatCount = FLT_MAX;
       keyFrameAnimation.autoreverses = NO;
       keyFrameAnimation.duration = 4.0f;
       //坐标累加(可以实现前进一段距离再后退一段距离,然后重复)
       keyFrameAnimation.cumulative = YES;
       //CGPoint p1 = _imageView.center;
       CGPoint p1 = CGPointMake(0, 0);
       CGPoint p2 = CGPointMake(30, 30);
       CGPoint p3 = CGPointMake(60, 60);
       CGPoint p4 = CGPointMake(90, 90);
       CGPoint p5 = CGPointMake(120, 120);
       CGPoint p6 = CGPointMake(90, 90);
       CGPoint p7 = CGPointMake(60, 60);
       
       [keyFrameAnimation setValues:[NSArrayarrayWithObjects:                             
                             [NSValuevalueWithCGPoint:p1],                             
                             [NSValuevalueWithCGPoint:p2],                             
                             [NSValuevalueWithCGPoint:p3],                             
                             [NSValuevalueWithCGPoint:p4],                             
                             [NSValuevalueWithCGPoint:p5],                             
                             [NSValuevalueWithCGPoint:p6],                             
                             [NSValuevalueWithCGPoint:p7],                             
                             nil]];
       
       [keyFrameAnimation setKeyTimes:[NSArrayarrayWithObjects:                               
                               [NSNumbernumberWithFloat:0.0],                               
                               [NSNumbernumberWithFloat:0.3],                               
                               [NSNumbernumberWithFloat:0.4],                               
                               [NSNumbernumberWithFloat:0.5],                               
                               [NSNumbernumberWithFloat:0.6],                               
                               [NSNumbernumberWithFloat:0.7],                               
                               [NSNumbernumberWithFloat:0.8],                               
                               nil]];
       
       return keyFrameAnimation;
   } else if ([_animationKeyPathisEqualToString:@"transform.translation"]) {
       //point貌似是相对_imageView;
       animation.toValue=[NSValue valueWithCGPoint:CGPointMake(200,-200)];
   } else if ([_animationKeyPath isEqualToString:@"transform"]){
       CATransform3D rotationTransform  =CATransform3DMakeRotation(45, 0, 0,2);     
       animation.toValue= [NSValuevalueWithCATransform3D:rotationTransform];       
       animation.duration = 2.0f;
       animation.autoreverses = NO;
       //角度累加
       animation.cumulative = YES;
       animation.timingFunction = [CAMediaTimingFunctionfunctionWithName:kCAMediaTimingFunctionLinear];
   } else if ([_animationKeyPath isEqualToString:@"borderWidth"]){
       animation.fromValue   = [NSNumber numberWithFloat:0.0f];  
       animation.toValue     = [NSNumber numberWithFloat:4.0f];
       animation.duration    = 3.0f;
   } else if ([_animationKeyPath isEqualToString:@"cornerRadius"]){
#pragma mark M
       _imageView.layer.masksToBounds = YES;
       _imageView.layer.cornerRadius = 10.0f;
       _imageView.layer.borderWidth = 2.0f;
       _imageView.layer.borderColor = [UIColor orangeColor].CGColor;
       animation.fromValue   = [NSNumber numberWithFloat:0.0f];
       animation.toValue     = [NSNumber numberWithFloat:20.0f];
       animation.duration    = 1.0f;
   } else if ([_animationKeyPath isEqualToString:@"bounds"]) {
       animation.fromValue   = [NSValue valueWithCGRect:CGRectMake(0,0,10,10)];
       animation.toValue     = [NSValue valueWithCGRect:CGRectMake(10,10,200,200)];
       //animation.byValue     = [NSValue valueWithCGRect:self.view.bounds];
       animation.duration    = 3.0f;
   } else if ([_animationKeyPath isEqualToString:@"contents"]) {
       animation.fromValue = (id)[UIImageimageNamed:@"home_module_apps_normal@2x.png"].CGImage;
       animation.toValue = (id)[UIImageimageNamed:@"disk_scan_center@2x.png"].CGImage;
       //无效果
       //animation.toValue = (id)[UIColor redColor].CGColor;
       //animation.byValue     = (id)[UIImage imageNamed:@"GuideArrow.png"].CGImage;
       
       animation.duration    = 3.0f;
       
   } else if ([_animationKeyPath isEqualToString:@"shadowColor"]){
       animation.fromValue   = (id)[UIColor redColor].CGColor;
       animation.toValue     = (id)[UIColor blackColor].CGColor;
       animation.duration    = 1.0f;
   } else if ([_animationKeyPath isEqualToString:@"shadowOffset"]){
       animation.fromValue = [NSValuevalueWithCGSize:CGSizeMake(0,0)];
       animation.toValue = [NSValuevalueWithCGSize:CGSizeMake(3,3)];
       animation.duration    = 2.0f;
   } else if ([_animationKeyPath isEqualToString:@"shadowOpacity"]){
       animation.fromValue   = [NSNumber numberWithFloat:0.5f];
       animation.toValue     = [NSNumber numberWithFloat:1.0f];
       animation.duration    = 1.0f;
   } else if ([_animationKeyPath isEqualToString:@"shadowRadius"]){
       animation.fromValue   = [NSNumber numberWithFloat:10.f];
       animation.toValue     = [NSNumber numberWithFloat:5.0f];
       animation.duration    = 1.0f;
       
   } else {
       animation.fromValue   = [NSNumber numberWithFloat:0.0f];
       animation.toValue     = [NSNumber numberWithFloat:400.0f];
       animation.duration    = 3.0f;
       //从当前位置开始
       animation.additive    = YES;
   }
   
   return animation;
}

@end
0 0