// ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
// ViewController.m
#import "ViewController.h"
// 角度转弧度
#define angle2Radio(angle) ((angle) * M_PI / 180.0)
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIView *redView;
@end
@implementation ViewController
/***
UIView与核心动画区别?
1.核心动画只作用在layer.
2.核心动画看到的一切都是假像.真实值并没有被修改.
什么时候使用UIVeiw,什么时候使用核心动画
1. 什么时候使用UIVeiw:当与用户进行交互时,使用UIView.不须要与用户进行交互时, 使用两个都可以.
2.什么时候使用核心动画:当做帧动画时,转场动画.
*/
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
// [self startTransformAnimation:self.redView];
// [self startScaleAnimation:self.redView];
// [self startHeartBeatAnimation:self.redView];
// [self startShakeAnimation:self.redView];
// [self startDrawedRouteAnimation:self.redView];
// [self startTransitionAnimation:self.redView];
[self startGroupAnimation:self.redView];
}
// 动画组
- (void)startGroupAnimation:(UIView*)view {
CAAnimationGroup *groupA = [CAAnimationGroup animation];
// 移动
CABasicAnimation *anim = [CABasicAnimation animation];
anim.keyPath = @"position.y";
anim.toValue = @300;
anim.removedOnCompletion = NO;
anim.fillMode = kCAFillModeForwards;
[view.layer addAnimation:anim forKey:nil];
// 缩放
CABasicAnimation *anim2 = [CABasicAnimation animation];
anim2.keyPath = @"transform.scale";
anim2.toValue = @0.5;
anim2.removedOnCompletion = NO;
anim2.fillMode = kCAFillModeForwards;
[view.layer addAnimation:anim2 forKey:nil];
// 自动执行数组当中的每一个动画对象
groupA.animations = @[anim, anim2];
groupA.removedOnCompletion = NO;
groupA.fillMode = kCAFillModeForwards;
[view.layer addAnimation:groupA forKey:nil];
}
// 转场动画
- (void)startTransitionAnimation:(UIView*)view {
// 添加转场动画
CATransition *anim = [CATransition animation];
// 设置动画持续时间
anim.duration = 1;
//设置转场类型
anim.type = @"suckEffect";
[view.layer addAnimation:anim forKey:nil];
}
// 按绘制路线移动
- (void)startDrawedRouteAnimation:(UIView*)view {
//帧动画
CAKeyframeAnimation *anim = [CAKeyframeAnimation animation];
anim.keyPath = @"position";
anim.duration = 3;
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(50, 50)];
[path addLineToPoint:CGPointMake(200, 50)];
[path addLineToPoint:CGPointMake(200, 300)];
anim.path = path.CGPath;
[view.layer addAnimation:anim forKey:nil];
}
// 图片抖动
- (void)startShakeAnimation:(UIView*)view {
//帧动画
CAKeyframeAnimation *anim = [CAKeyframeAnimation animation];
anim.keyPath = @"transform.rotation";
anim.values = @[@(angle2Radio(-5)),@(angle2Radio(5)),@(angle2Radio(-5))];
//设置执行次数
anim.repeatCount = MAXFLOAT;
anim.autoreverses = YES;
[view.layer addAnimation:anim forKey:nil];
}
// 心跳效果
- (void)startHeartBeatAnimation:(UIView*)view {
// 创建动画对象
CABasicAnimation *anim = [CABasicAnimation animation];
anim.keyPath = @"transform.scale";
// 复位
anim.toValue = @0;
// 设置动画的执行次数
anim.repeatCount = MAXFLOAT;
// 设置动画的执行时长
anim.duration = 0.2;
// 设置自动反转
anim.autoreverses = YES;
// 添加动画
[view.layer addAnimation:anim forKey:nil];
}
// 缩放
- (void)startScaleAnimation:(UIView*)view {
//初始化动画对象
CABasicAnimation *anim = [CABasicAnimation animation];
//设置属性值
anim.keyPath = @"transform.scale.x";
anim.toValue = @0.5;
// 动画完成时会自动删除动画,下面两段代码一起使用,效果才会出现
anim.removedOnCompletion = NO;
anim.fillMode = @"forwards";
//添加核心动画
[self.redView.layer addAnimation:anim forKey:nil];
}
// 开始旋转
- (void)startTransformAnimation:(UIView*)view {
CABasicAnimation* rotationAnimation;
rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0 ];
rotationAnimation.duration = 0.5;
rotationAnimation.cumulative = YES;
rotationAnimation.repeatCount = MAXFLOAT;
[view.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
}
@end