【Objective-c】動(dòng)畫學(xué)習(xí)筆記(三)_CABasicAnimation

前言:iOS與MacOS的坐標(biāo)系不同,iOS使用的是左手坐標(biāo)系慌申,坐標(biāo)原點(diǎn)是在左上角,MacOS 使用的是右手坐標(biāo)系,原點(diǎn)是在左下角蹄溉。


簡單的動(dòng)畫實(shí)現(xiàn):矩形平移動(dòng)畫

- (void)viewDidLoad {
    [super viewDidLoad];
    //1.創(chuàng)建一個(gè)layer
    CALayer *mylayer = [CALayer layer];
    //2.設(shè)置layer屬性
    mylayer.bounds = CGRectMake(0, 0, 50, 80);
    mylayer.backgroundColor = [UIColor yellowColor].CGColor;
    mylayer.position = CGPointMake(50, 50);
    mylayer.anchorPoint = CGPointMake(0, 0);
    mylayer.cornerRadius = 20;
    //3.添加layer
    [self.view.layer addSublayer:mylayer];
    self.mylayer = mylayer;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    //1.創(chuàng)建核心動(dòng)畫(劇本)
    CABasicAnimation *anima = [CABasicAnimation animation];
    //  設(shè)置劇本具體動(dòng)畫內(nèi)容
    //  設(shè)置layer的position屬性咨油,即平移
    anima.keyPath = @"position";
    anima.fromValue = [NSValue valueWithCGPoint:CGPointMake(0, 0)];
    anima.toValue = [NSValue valueWithCGPoint:CGPointMake(200, 300)];
    //   設(shè)置動(dòng)畫執(zhí)行完畢之后不刪除動(dòng)畫
    anima.removedOnCompletion = NO;
    //   設(shè)置保存動(dòng)畫的最新狀態(tài)
    anima.fillMode = kCAFillModeForwards;
    
    //2.添加核心動(dòng)畫到layer(執(zhí)行劇本)
    [self.mylayer addAnimation:anima forKey:nil];

}

這里有一個(gè)重要的知識(shí)點(diǎn):position和anchorPoint,不懂的可以瀏覽這篇博客大頭青年柒爵,瞬間秒懂役电。這里就簡單講講我的理解:一個(gè)layer的位置frame是由position和anchorPoint共同決定的(不管layer之前的frame是多少都沒有影響)只要有一個(gè)改變了,那么layer的位置就會(huì)改變棉胀。第二了就是position和anchorPoint是不同坐標(biāo)系的重合點(diǎn)法瑟,即指同一個(gè)點(diǎn)。

監(jiān)聽動(dòng)畫的執(zhí)行過程唁奢,設(shè)置代理
在上方代碼的基礎(chǔ)上添加代理

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    //1.創(chuàng)建核心動(dòng)畫(劇本)
    CABasicAnimation *anima = [CABasicAnimation animation];
    //  設(shè)置劇本具體動(dòng)畫內(nèi)容
    //  設(shè)置layer的position屬性霎挟,即平移
    anima.keyPath = @"position";
    anima.fromValue = [NSValue valueWithCGPoint:CGPointMake(0, 0)];
    anima.toValue = [NSValue valueWithCGPoint:CGPointMake(200, 300)];
    //   設(shè)置動(dòng)畫執(zhí)行完畢之后不刪除動(dòng)畫
    anima.removedOnCompletion = NO;
    //   設(shè)置保存動(dòng)畫的最新狀態(tài)
    anima.fillMode = kCAFillModeForwards;
    //    設(shè)置代理
    anima.delegate = self;
    NSString *str = NSStringFromCGPoint(self.myLayer.position);
    NSLog(@"執(zhí)行前:%@",str);

    //2.添加核心動(dòng)畫到layer(執(zhí)行劇本)
    [self.mylayer addAnimation:anima forKey:nil];
}

- (void)animationDidStart:(CAAnimation*)anim{
  NSLog(@"開始執(zhí)行動(dòng)畫");
}

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
  NSString *str = NSStringFromCGPoint(self.myLayer.position);
  NSLog(@"執(zhí)行后:%@",str);
}
/*
 輸出結(jié)果: 
**2016-12-01 23:50:47.172 CAPropertyAnimationDemo[2829:99758] ****動(dòng)畫執(zhí)行前:****{50, 50}**
**2016-12-01 23:50:47.173 CAPropertyAnimationDemo[2829:99758] ****動(dòng)畫開始執(zhí)行了**
**2016-12-01 23:50:47.423 CAPropertyAnimationDemo[2829:99758] ****動(dòng)畫執(zhí)行后:****{50, 50}**
*/

從上面的例子得出:即使保持了圖層在執(zhí)行完動(dòng)畫后的狀態(tài)麻掸,但是實(shí)質(zhì)上圖層的屬性值還是動(dòng)畫執(zhí)行前的初始值酥夭,并沒有真正被改動(dòng)。如上例中的position

CABasicAnimation 還有一組重要的屬性:autoreverses 和 repeatCount 脊奋。autoreverses 是否自動(dòng)逆向執(zhí)行動(dòng)畫到原先狀態(tài)熬北,執(zhí)行時(shí)間與正向動(dòng)畫持續(xù)時(shí)間一致。repeatCount 是否重復(fù)動(dòng)畫狂魔。這兩個(gè)屬性默認(rèn)值是NO蒜埋。

平移、縮放最楷、旋轉(zhuǎn)

#import "ViewController.h"
@interface ViewController ()<CAAnimationDelegate>

@property (nonatomic,strong) CALayer *mylayer;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //1.創(chuàng)建一個(gè)layer
    CALayer *mylayer = [CALayer layer];
    //2.設(shè)置layer屬性
    mylayer.bounds = CGRectMake(0, 0, 50, 80);
    mylayer.backgroundColor = [UIColor yellowColor].CGColor;
    mylayer.position = CGPointMake(50, 50);
    mylayer.anchorPoint = CGPointMake(0, 0);
    mylayer.cornerRadius = 20;
    //3.添加layer
    [self.view.layer addSublayer:mylayer];
    self.mylayer = mylayer;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//    [self keyPathWithPositionAnimation];
//    [self keyPathWithBoundsAnimation];
//    [self keyPathWithTransformAnimation];
    [self positionByTransformAnimation];
}

#pragma mark - 平移動(dòng)畫
- (void)keyPathWithPositionAnimation{
    //1.創(chuàng)建核心動(dòng)畫(劇本)
    CABasicAnimation *anima = [CABasicAnimation animation];
    //  設(shè)置劇本具體動(dòng)畫內(nèi)容
    //  設(shè)置layer的position屬性整份,即平移
    //  注意:如果沒有設(shè)置起始位置"fromValue"的值,那么會(huì)從當(dāng)前位置開始
    anima.keyPath = @"position";
    anima.fromValue = [NSValue valueWithCGPoint:CGPointMake(0, 0)];
    anima.toValue = [NSValue valueWithCGPoint:CGPointMake(200, 300)];
    anima.duration = 2;
    //   設(shè)置動(dòng)畫執(zhí)行完畢之后不刪除動(dòng)畫
    anima.removedOnCompletion = NO;
    //   設(shè)置保存動(dòng)畫的最新狀態(tài)
    anima.fillMode = kCAFillModeForwards;
    anima.delegate = self;
    NSString *string = NSStringFromCGRect(self.mylayer.frame);
    NSLog(@"動(dòng)畫執(zhí)行前:%@",string);
    
    //2.添加核心動(dòng)畫到layer(執(zhí)行劇本)
    [self.mylayer addAnimation:anima forKey:nil];
}

#pragma mark - 縮放
- (void)keyPathWithBoundsAnimation{
    //1.創(chuàng)建動(dòng)畫
    CABasicAnimation *anima = [CABasicAnimation animation];
    anima.keyPath = @"bounds";
    //1.1設(shè)置動(dòng)畫執(zhí)行時(shí)間
    anima.duration = 2.0;
    //1.2設(shè)置保存動(dòng)畫的最新狀態(tài)
    anima.removedOnCompletion = NO;
    anima.fillMode = kCAFillModeForwards;
    //1.3修改屬性,執(zhí)行動(dòng)畫
    anima.toValue = [NSValue valueWithCGRect:CGRectMake(0, 0, 200, 200)];
    //2.0添加動(dòng)畫到layer
    [self.mylayer addAnimation:anima forKey:nil];
}

#pragma mark - 旋轉(zhuǎn)
- (void)keyPathWithTransformAnimation{
    CABasicAnimation *anima = [CABasicAnimation animation];
    anima.keyPath = @"transform";
    anima.duration = 2.0;
    anima.removedOnCompletion = NO;
    anima.fillMode = kCAFillModeForwards;
    anima.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI_2+M_PI_4, 1, 1, 0)];
    [self.mylayer addAnimation:anima forKey:nil];
}

#pragma mark - 平移的第二種方式
- (void)positionByTransformAnimation{
    CABasicAnimation *anima = [CABasicAnimation animation];
    anima.keyPath = @"transform";
    anima.duration = 2.0;
    anima.removedOnCompletion = NO;
    anima.fillMode = kCAFillModeForwards;
    
    anima.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeTranslation(0, 100, 1)];
    
    [self.mylayer addAnimation:anima forKey:nil];
}

- (void)animationDidStart:(CAAnimation *)anim{
    NSLog(@"動(dòng)畫開始執(zhí)行了");
}

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
    NSString *string = NSStringFromCGRect(self.mylayer.frame);
    NSLog(@"動(dòng)畫執(zhí)行后:%@",string);
}

完整Demo

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末籽孙,一起剝皮案震驚了整個(gè)濱河市烈评,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌犯建,老刑警劉巖讲冠,帶你破解...
    沈念sama閱讀 222,681評(píng)論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異适瓦,居然都是意外死亡竿开,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,205評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門玻熙,熙熙樓的掌柜王于貴愁眉苦臉地迎上來否彩,“玉大人,你說我怎么就攤上這事嗦随×欣螅” “怎么了?”我有些...
    開封第一講書人閱讀 169,421評(píng)論 0 362
  • 文/不壞的土叔 我叫張陵,是天一觀的道長贴浙。 經(jīng)常有香客問我砂吞,道長,這世上最難降的妖魔是什么崎溃? 我笑而不...
    開封第一講書人閱讀 60,114評(píng)論 1 300
  • 正文 為了忘掉前任蜻直,我火速辦了婚禮,結(jié)果婚禮上袁串,老公的妹妹穿的比我還像新娘袭蝗。我一直安慰自己,他們只是感情好般婆,可當(dāng)我...
    茶點(diǎn)故事閱讀 69,116評(píng)論 6 398
  • 文/花漫 我一把揭開白布到腥。 她就那樣靜靜地躺著,像睡著了一般蔚袍。 火紅的嫁衣襯著肌膚如雪乡范。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,713評(píng)論 1 312
  • 那天啤咽,我揣著相機(jī)與錄音晋辆,去河邊找鬼。 笑死宇整,一個(gè)胖子當(dāng)著我的面吹牛瓶佳,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播鳞青,決...
    沈念sama閱讀 41,170評(píng)論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼霸饲,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了臂拓?” 一聲冷哼從身側(cè)響起厚脉,我...
    開封第一講書人閱讀 40,116評(píng)論 0 277
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎胶惰,沒想到半個(gè)月后傻工,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,651評(píng)論 1 320
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡孵滞,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,714評(píng)論 3 342
  • 正文 我和宋清朗相戀三年中捆,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片坊饶。...
    茶點(diǎn)故事閱讀 40,865評(píng)論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡泄伪,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出幼东,到底是詐尸還是另有隱情臂容,我是刑警寧澤,帶...
    沈念sama閱讀 36,527評(píng)論 5 351
  • 正文 年R本政府宣布根蟹,位于F島的核電站脓杉,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏简逮。R本人自食惡果不足惜球散,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,211評(píng)論 3 336
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望散庶。 院中可真熱鬧蕉堰,春花似錦、人聲如沸悲龟。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,699評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽须教。三九已至皿渗,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間轻腺,已是汗流浹背乐疆。 一陣腳步聲響...
    開封第一講書人閱讀 33,814評(píng)論 1 274
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留贬养,地道東北人挤土。 一個(gè)月前我還...
    沈念sama閱讀 49,299評(píng)論 3 379
  • 正文 我出身青樓,卻偏偏與公主長得像误算,于是被迫代替她去往敵國和親仰美。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,870評(píng)論 2 361

推薦閱讀更多精彩內(nèi)容

  • 在iOS中隨處都可以看到絢麗的動(dòng)畫效果儿礼,實(shí)現(xiàn)這些動(dòng)畫的過程并不復(fù)雜筒占,今天將帶大家一窺ios動(dòng)畫全貌。在這里你可以看...
    每天刷兩次牙閱讀 8,516評(píng)論 6 30
  • 在iOS中隨處都可以看到絢麗的動(dòng)畫效果蜘犁,實(shí)現(xiàn)這些動(dòng)畫的過程并不復(fù)雜翰苫,今天將帶大家一窺iOS動(dòng)畫全貌。在這里你可以看...
    F麥子閱讀 5,118評(píng)論 5 13
  • 在iOS實(shí)際開發(fā)中常用的動(dòng)畫無非是以下四種:UIView動(dòng)畫这橙,核心動(dòng)畫奏窑,幀動(dòng)畫,自定義轉(zhuǎn)場動(dòng)畫屈扎。 1.UIView...
    請(qǐng)叫我周小帥閱讀 3,114評(píng)論 1 23
  • Core Animation Core Animation埃唯,中文翻譯為核心動(dòng)畫,它是一組非常強(qiáng)大的動(dòng)畫處理API鹰晨,...
    45b645c5912e閱讀 3,034評(píng)論 0 21
  • "小畫板程序"完成"小畫板"程序墨叛。 下載地址:http://git.oschina.net/changyou/my...
    _淺墨_閱讀 695評(píng)論 0 5