核心动画二

2015-12-28  本文已影响311人  coma

6> CoreAnimation

    //创建动画对象
    CABasicAnimation *anim = [CABasicAnimation animation];
    //设置属性值.
    anim.keyPath = @"transform.rotation";
    anim.toValue = @(M_PI);
    //动画完成时会自动的删除动画.
    //动画完成时要不要删除动画
    anim.removedOnCompletion = NO;
    //让动画始终保存最前面的效果.
    anim.fillMode = @"forwards" 
    //添加动画
    [self.redView.layer addAnimation:anim forKey:nil];
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
  //添加动画
    CABasicAnimation *anim = [CABasicAnimation animation];
    //设置属性值.
    anim.keyPath = @"transform.scale";
    anim.toValue = @0;
  //想要动画执行多次
    anim.repeatCount = MAXFLOAT;
 //设置动画的执行时长
    anim.duration = 0.5;
  //反转,怎么去的,怎么样回来.
    anim.autoreverses = YES;
//添加动画
    [self.imageV.layer addAnimation:anim forKey:nil];    
}
    //创建动画对象
    CABasicAnimation *anim = [CABasicAnimation animation];
    //设置属性值.
    anim.keyPath = @"transform.rotation";
    anim.toValue = @(M_PI);
    //动画完成时会自动的删除动画.
    //动画完成时要不要删除动画
    anim.removedOnCompletion = NO;
  //让动画始终保存最前面的效果.
    anim.fillMode = @"forwards";
   //添加动画
    [self.redView.layer addAnimation:anim forKey:nil];
* 总结CABasicAnimation只能在两个值之间做动画,怎么多个值做动画,用CAKeyframeAnimation

7> CAKeyframeAnimation

  //添加抖动的动画.
    CAKeyframeAnimation *anim = [CAKeyframeAnimation animation];
    //设置属性值.
    anim.keyPath = @"transform.rotation";
    anim.values = @[@(angle2Rad(-5)),@(angle2Rad(5))];
   anim.autoreverses = YES;
   //动画一直重复执行.
    anim.repeatCount = MAXFLOAT;
 anim.duration = 0.5;
 //添加动画
    [self.iconView.layer addAnimation:anim forKey:nil];
   //添加抖动的动画.
    CAKeyframeAnimation *anim = [CAKeyframeAnimation animation];
    //设置属性值.
    anim.keyPath = @"position";
    
  //根据路径做动画
//    UIBezierPath *path = [UIBezierPath bezierPath];
//    [path moveToPoint:CGPointMake(50, 50)];
//    [path addLineToPoint:CGPointMake(200, 400)];
    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(50, 50, 300, 300)];
    anim.path = path.CGPath;    
//    anim.autoreverses = YES;
    //动画一直重复执行.
    anim.repeatCount = MAXFLOAT;
  anim.duration = 0.5;
  //添加动画
    [self.iconView.layer addAnimation:anim forKey:nil];

8> * 图标抖动

9> * CATransition
过度动画又叫转场动画 -> type(转场类型) -> subtype(方向) -> 动画进度
注意:转场动画必须和过度代码放在一块,否则没有效果

static int _i = 1;
   //转场代码必须得要和转场动画在同一个方法当中.,并没有要求他们的上下顺序.
    //转场代码.
    _i++;
    if (_i > 3) {
        _i = 1;
    }
    NSString *imageName = [NSString stringWithFormat:@"%d",_i];
    self.imageV.image = [UIImage imageNamed:imageName];  
    //添加转场动画
    CATransition *anim = [CATransition animation]; 
    anim.duration = 1;
    //动画从哪个点开始.
    anim.startProgress = 0.3;
    //动画从哪个点结束.
    anim.endProgress = 0.5;  
    //转场类型.
    anim.type = @"pageCurl";  
    [self.imageV.layer addAnimation:anim forKey:nil];

10> * CAAnimationGroup
同时进行多种动画 -> 平移,旋转,缩放 -> 取消反弹

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ 
    CAAnimationGroup *animGroup = [CAAnimationGroup animation];
    //animations数组当中存入的都是animation对象
    //平移
    CABasicAnimation *anim = [CABasicAnimation animation];
    anim.keyPath = @"position.y";
    anim.toValue = @(400); 
    //缩放
    CABasicAnimation *anim2 = [CABasicAnimation animation];
    anim2.keyPath = @"transform.scale";
    anim2.toValue = @(0.5);
    //会自动执行数组当中所有的动画对象.
    animGroup.animations = @[anim,anim2];
    //动画完成不要删除动画
    animGroup.removedOnCompletion = NO;
    //始终保持最前面的效果
    animGroup.fillMode = kCAFillModeForwards;
    [self.redView.layer addAnimation:animGroup forKey:nil];   
}

11> * UIView封装的动画
单视图

+ (void)transitionWithView:(UIView *)view duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion;

参数说明:
duration:动画的持续时间
view:需要进行转场动画的视图
options:转场动画的类型
animations:将改变视图属性的代码放在这个block中
completion:动画结束后,会自动调用这个block
双视图
+ (void)transitionFromView:(UIView *)fromView toView:(UIView *)toView duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options completion:(void (^)(BOOL finished))completion;

参数说明:
duration:动画的持续时间
options:转场动画的类型
animations:将改变视图属性的代码放在这个block中
completion:动画结束后,会自动调用这个block
* 什么时候用核心动画,什么时候用UIView动画
* 核心动画的缺点:都是假象,不能真正改变图层属性的值
* 展示和真实的位置不同
* 如果改变位置就用UIView的动画
* 转场动画就用核心动画
* 为什么转场用核心动画?因为UIView的转场动画太少。
* 演示UIView的转场动画
* touchBegin,切换图片

上一篇下一篇

猜你喜欢

热点阅读