iOS核心动画-CABasicAnimation

1: 什么是CABasicAnimation

CABasicAnimation是核心动画中的一个类,其父类是CAPropertyAnimation,其子类是CASpringAnimation,它的祖父是CAAnimation。它主要用于制作比较单一的动画,例如,平移、缩放、旋转、颜色渐变、边框的值的变化等,也就是将layer的某个属性值从一个值到另一个值的变化。

2: 属性说明

属性 说明
duration 动画的时长
beginTime 指定动画开始的时间
repeatCount 重复的次数(PS:一直重复为 HUGE_VALF)
repeatDuration 设置动画的时间
timingFunction 动画的速度变化[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]
autoreverses 动画结束时是否执行逆动画
fromValue 属性的起始值
toValue 属性的结束时的值
byValue 所改变属性相同起始值的改变量

3: 实例化

/* Creates a new animation object with its `keyPath' property set to
 * 'path'. 
创建一个新的动画,其“keyPath”为“路径”。
*/
+ (instancetype)animationWithKeyPath:(nullable NSString *)path;

下面是对path比较全面的总结,每个keypath对应一种属性值的变化,其中涉及到颜色变化的都必须使用CGColor,因为对应是对layer的属性。

#ifndef AnimationKeyPathName_h
#define AnimationKeyPathName_h
#import 

#pragma mark- CATransform3D Key Paths
/** 
 *  @brief  旋转x,y,z分别是绕x,y,z轴旋转 
 */
static NSString *kCARotation = @"transform.rotation";
static NSString *kCARotationX = @"transform.rotation.x";
static NSString *kCARotationY = @"transform.rotation.y";
static NSString *kCARotationZ = @"transform.rotation.z";

/** 
 *  @brief  缩放x,y,z分别是对x,y,z方向进行缩放
 */
static NSString *kCAScale = @"transform.scale";
static NSString *kCAScaleX = @"transform.scale.x";
static NSString *kCAScaleY = @"transform.scale.y";
static NSString *kCAScaleZ = @"transform.scale.z";

/** 
 *  @brief  平移x,y,z同上
 */
static NSString *kCATranslation = @"transform.translation";
static NSString *kCATranslationX = @"transform.translation.x";
static NSString *kCATranslationY = @"transform.translation.y";
static NSString *kCATranslationZ = @"transform.translation.z";

#pragma mark- 平面
/** 
 *  @brief  CGPoint中心点改变位置,针对平面 
 */
static NSString *kCAPosition = @"position";
static NSString *kCAPositionX = @"position.x";
static NSString *kCAPositionY = @"position.y";

/** 
 *  @brief  CGRect 
 */
static NSString *kCABoundsSize = @"bounds.size";
static NSString *kCABoundsSizeW = @"bounds.size.width";
static NSString *kCABoundsSizeH = @"bounds.size.height";
static NSString *kCABoundsOriginX = @"bounds.origin.x";
static NSString *kCABoundsOriginY = @"bounds.origin.y";

/** 
 *  @brief  透明度 
 */
static NSString *kCAOpacity = @"opacity";
/** 
 *  @brief  背景色 
 */
static NSString *kCABackgroundColor = @"backgroundColor";
/** 
 *  @brief  圆角 
 */
static NSString *kCACornerRadius = @"cornerRadius";
/** 
 *  @brief   边框 
 */
static NSString *kCABorderWidth = @"borderWidth";
/** 
 *  @brief  阴影颜色
 */
static NSString *kCAShadowColor = @"shadowColor";
/** 
 *  @brief  偏移量CGSize
 */
static NSString *kCAShadowOffset = @"shadowOffset";
/** 
 *  @brief  阴影透明度
 */
static NSString *kCAShadowOpacity = @"shadowOpacity";
/** 
 *  @brief  阴影圆角 
 */
static NSString *kCAShadowRadius = @"shadowRadius";

#endif /* AnimationKeyPathName_h */

4:简单用法

4.1 位移动画

 //使用CABasicAnimation创建基础动画
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
    animation.fromValue = [NSValue valueWithCGPoint:CGPointMake(0, SCREEN_HEIGHT/2-75)];
    animation.toValue = [NSValue valueWithCGPoint:CGPointMake(SCREEN_WIDTH, SCREEN_HEIGHT/2-75)];
    animation.duration = 2.0f;
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
    [_demoView.layer addAnimation:animation forKey:@"positionAnimation"];
ps:
    //如果fillMode=kCAFillModeForwards和removedOnComletion=NO,那么在动画执行完毕后,图层会保持显示动画执行后的状态。但在实质上,图层的属性值还是动画执行前的初始值,并没有真正被改变。
    //animation.duration.fillMode = kCAFillModeForwards;
    //animation.duration.removedOnCompletion = NO;

4.2 透明度动画

    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"opacity"];
    animation.fromValue = [NSNumber numberWithFloat:1.0f];
    animation.toValue = [NSNumber numberWithFloat:0.2f];
    animation.duration = 1.0f;
    [_demoView.layer addAnimation:animation forKey:@"opacityAniamtion"];

4.3 缩放动画

    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];//同上
    animation.toValue = [NSNumber numberWithFloat:2.0f];
    animation.duration = 1.0f;
    [_demoView.layer addAnimation:animation forKey:@"scaleAnimation"];

4.4 旋转动画

CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];//绕着z轴为矢量,进行旋转(@"transform.rotation.z"==@@"transform.rotation")
    animation.toValue = [NSNumber numberWithFloat:M_PI];
    animation.duration = 1.0f;
    [_demoView.layer addAnimation:animation forKey:@"rotateAnimation"];

4.5 背景色变化动画

CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"backgroundColor"];
    animation.toValue =(id) [UIColor greenColor].CGColor;
    animation.duration = 1.0f;
    [_demoView.layer addAnimation:animation forKey:@"backgroundAnimation"];

4.6 组动画

CABasicAnimation *positionAnima = [CABasicAnimation animationWithKeyPath:@"position.y"];
positionAnima.fromValue = @(self.imageView.center.y);
positionAnima.toValue = @(self.imageView.center.y-30);
positionAnima.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];

CABasicAnimation *transformAnima = [CABasicAnimation animationWithKeyPath:@"transform.rotation.y"];
transformAnima.fromValue = @(0);
transformAnima.toValue = @(M_PI);
transformAnima.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];

CAAnimationGroup *animation = [CAAnimationGroup animation];
animaGroup.duration = 2.0f;
animaGroup.fillMode = kCAFillModeForwards;
animaGroup.removedOnCompletion = NO;
animaGroup.animations = @[positionAnima,transformAnima];

[_demoView.layer addAnimation:animaGroup forKey:@"Animation"];

5: 获取动画开始和结束时的事件

添加代理事件

animation.delegate = self;
ps:会有循环引用,暂未解决!

实现代理方法

//动画开始时
- (void)animationDidStart:(CAAnimation *)anim
{
    if ([anim isEqual:[self.imageView.layer animationForKey:@"Animation"]]) {
        NSLog(@"动画组执行了");
    }
}
//动画结束时
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
    //方法中的flag参数表明了动画是自然结束还是被打断,比如调用了removeAnimationForKey:方法或removeAnimationForKey方法,flag为NO,如果是正常结束,flag为YES。
    NSLog(@"结束了");
}

你可能感兴趣的:(iOS核心动画-CABasicAnimation)