節(jié)選自:http://www.cnblogs.com/lee0oo0/p/4225730.html
設定動畫的屬性愉镰。以下是屬性及其對應的說明:
C221A1BE-B020-41BF-B75F-4324E1A0011E.png
animation.duration = 2.5; // 動畫持續(xù)時間
animation.repeatCount = 1; // 不重復
animation.beginTime = CACurrentMediaTime() + 2; // 2秒后執(zhí)行
animation.autoreverses = YES; // 結束后執(zhí)行逆動畫
// 動畫先加速后減速
animation.timingFunction = [CAMediaTimingFunction functionWithName: kCAMediaTimingFunctionEaseInEaseOut];
設定動畫開始和結束幀時的狀態(tài)肝集。設定的值會變?yōu)镵eyPath所指定的屬性的值囊嘉。
3F3702E4-5460-4680-B21F-FA95056C96DE.png
// 指定position屬性(移動)
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
// 設定動畫起始幀和結束幀
animation.fromValue = [NSValue valueWithCGPoint:CGPointMake(0, 0)]; // 起始點
animation.toValue = [NSValue valueWithCGPoint:CGPointMake(320, 480)]; // 終了點
-
縮放動畫(不停止)
@property (weak, nonatomic) IBOutlet UIView *redView;
@property (weak, nonatomic) IBOutlet UIImageView *imageV;
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// 創(chuàng)建動畫
CABasicAnimation *anim = [CABasicAnimation animation];// 描述下修改哪個屬性產生動畫 // anim.keyPath = @"position"; // 只能是layer屬性 anim.keyPath = @"transform.scale"; // 設置值 // anim.toValue = [NSValue valueWithCGPoint:CGPointMake(250, 500)]; anim.toValue = @0.5; // 設置動畫執(zhí)行次數(shù) anim.repeatCount = MAXFLOAT; // 取消動畫反彈 // 設置動畫完成的時候不要移除動畫 anim.removedOnCompletion = NO; // 設置動畫執(zhí)行完成要保持最新的效果 anim.fillMode = kCAFillModeForwards; [_imageV.layer addAnimation:anim forKey:nil]; }
-
手指畫一條線,讓view隨著這條線走
@interface DrawView ()@property (nonatomic, strong) UIBezierPath *path; @end @implementation DrawView - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { // touch UITouch *touch = [touches anyObject]; // 獲取手指的觸摸點 CGPoint curP = [touch locationInView:self]; // 創(chuàng)建路徑 UIBezierPath *path = [UIBezierPath bezierPath]; _path = path; // 設置起點 [path moveToPoint:curP]; } - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { // touch UITouch *touch = [touches anyObject]; // 獲取手指的觸摸點 CGPoint curP = [touch locationInView:self]; [_path addLineToPoint:curP]; [self setNeedsDisplay]; } - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { // 給imageView添加核心動畫 // 添加核心動畫 CAKeyframeAnimation *anim = [CAKeyframeAnimation animation]; anim.keyPath = @"position"; // anim.values = @[@(angle2Radion(-10)),@(angle2Radion(10)),@(angle2Radion(-10))]; anim.path = _path.CGPath; anim.duration = 1; anim.repeatCount = 1; //給這個view的圖層加一個動畫 [[[self.subviews firstObject] layer] addAnimation:anim forKey:nil]; } - (void)drawRect:(CGRect)rect { //根據(jù)_path畫一條線 [_path stroke]; } @end
-
轉場動畫
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageView;@end @implementation ViewController static int i = 1; - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { // 轉場代碼 if (i == 4) { i = 1; } // 加載圖片名稱 NSString *imageN = [NSString stringWithFormat:@"%d",i++]; _imageView.image = [UIImage imageNamed:imageN]; // 轉場動畫 CATransition *anim = [CATransition animation]; anim.type = @"pageCurl"; anim.duration = 0.5; [_imageView.layer addAnimation:anim forKey:nil]; } @end
-
動畫組
@property (weak, nonatomic) IBOutlet UIView *redView;
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// 同時縮放樱拴,平移,旋轉
CAAnimationGroup *group = [CAAnimationGroup animation];CABasicAnimation *scale = [CABasicAnimation animation]; scale.keyPath = @"transform.scale"; scale.toValue = @0.5; CABasicAnimation *rotation = [CABasicAnimation animation]; rotation.keyPath = @"transform.rotation"; rotation.toValue = @(arc4random_uniform(M_PI)); CABasicAnimation *position = [CABasicAnimation animation]; position.keyPath = @"position"; position.toValue = [NSValue valueWithCGPoint:CGPointMake(arc4random_uniform(200), arc4random_uniform(200))]; group.animations = @[scale,rotation,position]; [_redView.layer addAnimation:group forKey:nil]; }
- 折疊(根據(jù)x旋轉)動畫

#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *topView;
@property (weak, nonatomic) IBOutlet UIImageView *bottomView;
// 添加手勢的View
@property (weak, nonatomic) IBOutlet UIView *dragView;
@property (nonatomic, weak) CAGradientLayer *gradientL;
@end
@implementation ViewController
// 一張圖片必須要通過兩個控件展示,旋轉的時候,只旋轉上部分控件
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// 通過設置contentsRect可以設置圖片顯示的尺寸整陌,取值0~1
//topView和bottomView的frame一樣 通過設置鉚點anchorPoint來調整
_topView.layer.contentsRect = CGRectMake(0, 0, 1, 0.5);
_topView.layer.anchorPoint = CGPointMake(0.5, 1);
_bottomView.layer.contentsRect = CGRectMake(0, 0.5, 1, 0.5);
_bottomView.layer.anchorPoint = CGPointMake(0.5, 0);
// 添加手勢
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
[_dragView addGestureRecognizer:pan];
// 漸變圖層
CAGradientLayer *gradientL = [CAGradientLayer layer];
// 注意圖層需要設置尺寸
gradientL.frame = _bottomView.bounds;
gradientL.opacity = 0;
gradientL.colors = @[(id)[UIColor clearColor].CGColor,(id)[UIColor blackColor].CGColor];
_gradientL = gradientL;
[_bottomView.layer addSublayer:gradientL];
}
// 拖動的時候旋轉上部分內容,200(垂直(Y)偏移量) M_PI(旋轉180度)
- (void)pan:(UIPanGestureRecognizer *)pan
{
// 獲取偏移量
CGPoint transP = [pan translationInView:_dragView];
NSLog(@"%@", NSStringFromCGPoint(transP));
// 旋轉角度,往下逆時針旋轉
CGFloat angle = -transP.y / 200.0 * M_PI;
CATransform3D transfrom = CATransform3DIdentity;
// 增加旋轉的立體感瞎领,近大遠小,d:距離圖層的距離
transfrom.m34 = -1 / 500.0;
transfrom = CATransform3DRotate(transfrom, angle, 1, 0, 0);
_topView.layer.transform = transfrom;
// 設置陰影效果
_gradientL.opacity = transP.y * 1 / 200.0;
if (pan.state == UIGestureRecognizerStateEnded) { // 反彈
/**
* 彈簧效果的動畫
* usingSpringWithDamping 參數(shù): usingSpringWithDamping的范圍為0.0f到1.0f泌辫,數(shù)值越小「彈簧」的振動效果越明顯。
* initialSpringVelocity 參數(shù): initialSpringVelocity則表示初始的速度九默,數(shù)值越大一開始移動越快震放。
* http://www.renfei.org/blog/ios-8-spring-animation.html
*/
[UIView animateWithDuration:0.6 delay:0 usingSpringWithDamping:0.2 initialSpringVelocity:10 options:UIViewAnimationOptionCurveEaseInOut animations:^{
_topView.layer.transform = CATransform3DIdentity;
} completion:^(BOOL finished) {
}];
}
}
@end
- 音量震動
E9E73E8C-2511-4656-94FC-9D9179D452B4.png
F8B483F8-D235-4CD6-A220-1BFBEF203356.png
#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIView *lightView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// CAReplicatorLayer復制圖層,可以把圖層里面所有子層復制
// 創(chuàng)建復制圖層
CAReplicatorLayer *repL = [CAReplicatorLayer layer];
repL.frame = _lightView.bounds;
[_lightView.layer addSublayer:repL];
//設置一個layer震動條
CALayer *layer = [CALayer layer];
layer.anchorPoint = CGPointMake(0.5, 1);
layer.position = CGPointMake(15, _lightView.bounds.size.height);
layer.bounds = CGRectMake(0, 0, 30, 150);
layer.backgroundColor = [UIColor whiteColor].CGColor;
[repL addSublayer:layer];
//設置圖層的動畫
CABasicAnimation *anim = [CABasicAnimation animation];
anim.keyPath = @"transform.scale.y";
anim.toValue = @0.1;
anim.duration = 0.5;
anim.repeatCount = MAXFLOAT;
// 設置動畫反轉 動畫結束時是否執(zhí)行逆動畫
anim.autoreverses = YES;
[layer addAnimation:anim forKey:nil];
// 復制層中子層總數(shù)
// instanceCount:表示復制層里面有多少個子層荤西,包括原始層
repL.instanceCount = 3;
// 設置復制子層偏移量,不包括原始層,相對于原始層x偏移
repL.instanceTransform = CATransform3DMakeTranslation(45, 0, 0);
// 設置復制層動畫延遲時間
repL.instanceDelay = 0.1;
// 如果設置了原始層背景色伍俘,就不需要設置這個屬性
repL.instanceColor = [UIColor redColor].CGColor;
// 顏色值遞減
repL.instanceRedOffset = -0.3;
}
@end
- 菊花
45593372-4178-4EB6-925C-04765F9387AE.png
#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIView *redView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
CAReplicatorLayer *repL = [CAReplicatorLayer layer];
repL.frame = _redView.bounds;
[_redView.layer addSublayer:repL];
CALayer *layer = [CALayer layer];
layer.transform = CATransform3DMakeScale(0, 0, 0);
layer.position = CGPointMake(_redView.bounds.size.width / 2, 20);
layer.bounds = CGRectMake(0, 0, 10, 10);
layer.cornerRadius = 10.0;
layer.masksToBounds = YES;
layer.backgroundColor = [UIColor purpleColor].CGColor;
[repL addSublayer:layer];
// 設置縮放動畫
CABasicAnimation *anim = [CABasicAnimation animation];
anim.keyPath = @"transform.scale";
anim.fromValue = @1;
anim.toValue = @0;
anim.repeatCount = MAXFLOAT;
CGFloat duration = 1;
anim.duration = duration;
[layer addAnimation:anim forKey:nil];
int count = 30;
CGFloat angle = M_PI * 2 / count;
// 設置子層總數(shù)
repL.instanceCount = count;
repL.instanceTransform = CATransform3DMakeRotation(angle, 0, 0, 1);
repL.instanceDelay = duration / count;
}
@end