// ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
// ViewController.m
#import "ViewController.h"
// 角度轉(zhuǎn)弧度
#define angle2Radio(angle) ((angle) * M_PI / 180.0)
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIView *redView;
@end
@implementation ViewController
/***
UIView與核心動(dòng)畫(huà)區(qū)別?
1.核心動(dòng)畫(huà)只作用在layer.
2.核心動(dòng)畫(huà)看到的一切都是假像.真實(shí)值并沒(méi)有被修改.
什么時(shí)候使用UIVeiw,什么時(shí)候使用核心動(dòng)畫(huà)
1. 什么時(shí)候使用UIVeiw:當(dāng)與用戶進(jìn)行交互時(shí),使用UIView.不須要與用戶進(jìn)行交互時(shí), 使用兩個(gè)都可以.
2.什么時(shí)候使用核心動(dòng)畫(huà):當(dāng)做幀動(dòng)畫(huà)時(shí),轉(zhuǎn)場(chǎng)動(dòng)畫(huà).
*/
- (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];
}
// 動(dòng)畫(huà)組
- (void)startGroupAnimation:(UIView*)view {
CAAnimationGroup *groupA = [CAAnimationGroup animation];
// 移動(dòng)
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];
// 自動(dòng)執(zhí)行數(shù)組當(dāng)中的每一個(gè)動(dòng)畫(huà)對(duì)象
groupA.animations = @[anim, anim2];
groupA.removedOnCompletion = NO;
groupA.fillMode = kCAFillModeForwards;
[view.layer addAnimation:groupA forKey:nil];
}
// 轉(zhuǎn)場(chǎng)動(dòng)畫(huà)
- (void)startTransitionAnimation:(UIView*)view {
// 添加轉(zhuǎn)場(chǎng)動(dòng)畫(huà)
CATransition *anim = [CATransition animation];
// 設(shè)置動(dòng)畫(huà)持續(xù)時(shí)間
anim.duration = 1;
//設(shè)置轉(zhuǎn)場(chǎng)類型
anim.type = @"suckEffect";
[view.layer addAnimation:anim forKey:nil];
}
// 按繪制路線移動(dòng)
- (void)startDrawedRouteAnimation:(UIView*)view {
//幀動(dòng)畫(huà)
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];
}
// 圖片抖動(dòng)
- (void)startShakeAnimation:(UIView*)view {
//幀動(dòng)畫(huà)
CAKeyframeAnimation *anim = [CAKeyframeAnimation animation];
anim.keyPath = @"transform.rotation";
anim.values = @[@(angle2Radio(-5)),@(angle2Radio(5)),@(angle2Radio(-5))];
//設(shè)置執(zhí)行次數(shù)
anim.repeatCount = MAXFLOAT;
anim.autoreverses = YES;
[view.layer addAnimation:anim forKey:nil];
}
// 心跳效果
- (void)startHeartBeatAnimation:(UIView*)view {
// 創(chuàng)建動(dòng)畫(huà)對(duì)象
CABasicAnimation *anim = [CABasicAnimation animation];
anim.keyPath = @"transform.scale";
// 復(fù)位
anim.toValue = @0;
// 設(shè)置動(dòng)畫(huà)的執(zhí)行次數(shù)
anim.repeatCount = MAXFLOAT;
// 設(shè)置動(dòng)畫(huà)的執(zhí)行時(shí)長(zhǎng)
anim.duration = 0.2;
// 設(shè)置自動(dòng)反轉(zhuǎn)
anim.autoreverses = YES;
// 添加動(dòng)畫(huà)
[view.layer addAnimation:anim forKey:nil];
}
// 縮放
- (void)startScaleAnimation:(UIView*)view {
//初始化動(dòng)畫(huà)對(duì)象
CABasicAnimation *anim = [CABasicAnimation animation];
//設(shè)置屬性值
anim.keyPath = @"transform.scale.x";
anim.toValue = @0.5;
// 動(dòng)畫(huà)完成時(shí)會(huì)自動(dòng)刪除動(dòng)畫(huà),下面兩段代碼一起使用区匣,效果才會(huì)出現(xiàn)
anim.removedOnCompletion = NO;
anim.fillMode = @"forwards";
//添加核心動(dòng)畫(huà)
[self.redView.layer addAnimation:anim forKey:nil];
}
// 開(kāi)始旋轉(zhuǎn)
- (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
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者