Core Animation | 核心动画简介
核心动画位于UIKit的下一层,相比UIView动画,它可以实现更复杂的动画效果,Core Animation是一组功能强大、效果华丽的动画API,无论在iOS系统或者在你开发的App中,都会有大量的应用场景
-
核心动画位置
-
核心动画表现得两张形式
-
核心动画中可以直接使用的类
- CABasicAnimation (基本动画)
- CAKeyframeAnimation (关键帧动画)
- CATransition (转场动画)
- CAAnimationGroup (动画组)
- CASpringAnimation (弹簧动画)
效果演示One / 抖动
点击屏幕触发抖动效果-
需要定义的角度宏
#define angleToRadians(angle) ((angle) / 180.0 *M_PI)
-
核心代码
- (void)ShakeDemo {
CAKeyframeAnimation *anim = [CAKeyframeAnimation animation];
anim.keyPath = @"transform.rotation";
anim.values = @[@angleToRadians(-6), @angleToRadians(6)];
anim.repeatCount = MAXFLOAT;
// anim.duration = 2;
anim.autoreverses = YES;
anim.speed = 2;
[_imgView.layer addAnimation:anim forKey:nil];
}
说明
- keyPath是设置动画的type,大家可以根据自己的需求设置
- repeatCount是重复的意思,如果不设置的话需要在values中在次添加6到-6的值,否知-6到6有动画,反过来就没有动画了
- 如果设置repeatCount属性,那么speed就是设置频率,默认是0.5
- repeatCount是重复次数,默认我们设置最大即可
效果演示Two / 过山车动画并随机改变颜色和大小
-
效果演示
-
核心代码
- (void)Demo{
// 曲线
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(50, 200)];
[path addCurveToPoint:CGPointMake(300, 200) controlPoint1:CGPointMake(180, 100) controlPoint2:CGPointMake(200, 300)];
//需要添加在layer上
CAShapeLayer *shapeLayer = [CAShapeLayer layer];
shapeLayer.path = path.CGPath;
shapeLayer.fillColor = nil;
shapeLayer.strokeColor = [UIColor redColor].CGColor;
[self.view.layer addSublayer:shapeLayer];
CALayer *colorLayer = [CALayer layer];
colorLayer.frame = CGRectMake(0, 0, 60, 60);
colorLayer.position = CGPointMake(50, 200);
colorLayer.backgroundColor = [UIColor blueColor].CGColor;
[self.view.layer addSublayer:colorLayer];
// 过山车的动画
CAKeyframeAnimation *anim = [CAKeyframeAnimation animation];
anim.keyPath = @"position";
anim.path = path.CGPath;
// anim.duration = 4.0f;
// anim.removedOnCompletion = NO;
// anim.fillMode = kCAFillModeForwards;
// anim.rotationMode = kCAAnimationRotateAuto;
// [colorLayer addAnimation:anim forKey:nil];
// 改变大小
CABasicAnimation *sizeAnim = [CABasicAnimation animation];
sizeAnim.keyPath = @"transform.scale";
sizeAnim.toValue = @.5;
// sizeAnim.duration = 4.0;
// sizeAnim.fillMode = kCAFillModeForwards;
// sizeAnim.removedOnCompletion = NO;
//
// [colorLayer addAnimation:sizeAnim forKey:nil];
// 修改颜色
CGFloat red = arc4random() / (CGFloat)INT_MAX;
CGFloat green = arc4random() / (CGFloat)INT_MAX;
CGFloat blue = arc4random() / (CGFloat)INT_MAX;
UIColor *color = [UIColor colorWithRed:red green:green blue:blue alpha:1];
CABasicAnimation *colorAnim = [CABasicAnimation animation];
colorAnim.keyPath = @"backgroundColor";
colorAnim.toValue = (id)color.CGColor;
// colorAnim.duration = 4.0f;
// colorAnim.fillMode = kCAFillModeForwards;
// colorAnim.removedOnCompletion = NO;
// [colorLayer addAnimation:colorAnim forKey:nil];
CAAnimationGroup *group = [CAAnimationGroup animation];
group.animations = @[anim, sizeAnim, colorAnim];
group.duration = 4.0f;
group.fillMode = kCAFillModeForwards;
group.removedOnCompletion = NO;
[colorLayer addAnimation:group forKey:nil];
}
代码说明/逻辑可以使用2中方法实现
- 不使用CAAnimationGroup动画组的形式,把屏蔽的代码打开即可
- 使用CAAnimationGroup动画组形式,把重复的动画设置全部都到动画组中处理,即把屏蔽的代码删除即可
- 这是核心动画几个类的综合使用,提供大家参考
网友评论