屬性動畫 CAPropertyAnimation 基類 不能直接使用
子類:
1.CABasicAnimation 基礎動畫
2.CAKeyframeAnimation 關鍵幀動畫
通過改變圖層或者視圖上面的屬性值(支持動畫的屬性)產生的動畫
屬性動畫的常用方法屬性
1.初始化
+ (instancetype)animationWithKeyPath:(nullable NSString *)path
path:需要產生動畫的屬性 如:中心點 -> 移動
2.KeyPath 描述 動畫的屬性
可以通過改變animationWithKeyPath來改變動畫的屬性:
改變動畫的屬性:
transform.scale = 比例轉換
transform.scale.x
transform.scale.y
transform.rotation.z
opacity 透明度
zPosition
backgroundColor 背景顏色
cornerRadius 拐角
borderWidth 邊框的寬度
bounds
contents 內容
contentsRect
frame
hidden
masksToBounds
opacity
position
shadowColor
shadowOffset
shadowOpacity
shadowRadius
基礎動畫 CABasicAnimation
介紹:通過改變某個屬性的值 到某個值 只能設置兩個值 產生的動畫
fromValue 開始值 如果不設置不會返回到初始位置
toValue 結束值
byValue 通過哪個值
心跳效果.gif
#import "ViewController.h"
@interface ViewController ()
//背景
@property(nonatomic,strong)CALayer *layer;
//花瓣
@property(nonatomic,strong)CALayer *petalLayer;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor blackColor];
[self.view.layer addSublayer:self.layer];
// [self.view.layer addSublayer:self.petalLayer];
}
-(CALayer *)layer{
if (_layer) {
return _layer;
}
_layer = [CALayer layer];
_layer.position= CGPointMake(self.view.center.x, self.view.center.y+100);
UIImage *image = [UIImage imageNamed:@"4"];
_layer.bounds = CGRectMake(0, 0, image.size.width/2, image.size.height/2);
_layer.contents = (id)image.CGImage;
return _layer;
}
-(CALayer *)petalLayer{
if (_petalLayer) {
return _petalLayer;
}
_petalLayer = [CALayer layer];
_petalLayer.position= CGPointMake(self.view.center.x, 50);
UIImage *image = [UIImage imageNamed:@"2"];
_petalLayer.bounds = CGRectMake(0, 0, image.size.width, image.size.height);
_petalLayer.contents = (id)image.CGImage;
return _petalLayer;
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
// [self demo1:[[touches anyObject] locationInView:self.view]];
[self demo2];
}
//移動中心點
-(void)demo1:(CGPoint)toValue{
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
//CGPoint -> 轉id類型
//CGPoint -> NSValue
animation.fromValue = [NSValue valueWithCGPoint:self.petalLayer.position];
//動畫的持續時間
animation.duration = 3;
//動畫執行的總時間 受動畫速度的影響
// animation.speed = 2;
//設置動畫完成的時候 固定在完成的狀態
//這個屬性必須把removedOnCompletion 設置為NO 這個屬性 才可以有效果
animation.removedOnCompletion = NO;
animation.toValue = [NSValue valueWithCGPoint:toValue];
animation.fillMode = kCAFillModeBoth;
//timingFunction可以控制動畫的速度
//快進慢出
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
//layer上添加動畫效果 addAnimation: forKey:
//forKey 表示動畫的字符串 可以通過key 來找到這個動畫
[self.petalLayer addAnimation:animation forKey:@"可以通過這個key,找到此動畫"];
//查找某個key對應的動畫
// CABasicAnimation *an = (CABasicAnimation *)[self.petalLayer animationForKey:@"可以通過這個key,找到此動畫"];
}
//心跳
-(void)demo2{
self.view.backgroundColor = [UIColor whiteColor];
UIImage *image = [UIImage imageNamed:@"心跳"];
self.layer.contents = (id)image.CGImage;//為了方便,直接更換self.layer內容
self.layer.bounds = CGRectMake(0, 0, image.size.width/10, image.size.height/10);
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"bounds"];
/*
目標
1.放大后還原到原來的位置 以動畫的方法
2.先慢后快
3.一直循環
*/
animation.fromValue = [NSValue valueWithCGRect:self.layer.bounds];
animation.toValue = [NSValue valueWithCGRect:CGRectMake(0, 0, image.size.width/5, image.size.height/5)];
animation.repeatCount = HUGE;//無限循環
animation.duration = 0.5;
animation.autoreverses = YES;//以動畫的效果返回到開始的狀態
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
[self.layer addAnimation:animation forKey:@"heartJamp"];
}
花瓣飄落,心跳的效果
Demo:https://github.com/AmazingWow/CAPropertyAnimation--DEMO