// 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
最后編輯于 :
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。