日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

使用Core Animation对象来实现动画

發(fā)布時間:2025/7/25 56 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用Core Animation对象来实现动画 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

轉(zhuǎn)載保留原文地址:http://blog.csdn.net/kqjob/article/details/10417461,轉(zhuǎn)載的

在iOS中如果使用普通的動畫則可以使用UIKit提供的動畫方式來實現(xiàn),如果想實現(xiàn)更復(fù)雜的效果,則需要使用Core Animation了。

下面詳解各種類型動畫的使用方式

1、通過動畫上下文使用UIKit動畫

?

[plain]?view plaincopy
  • -(void)animationOfUIKit??
  • {??
  • ????UIView?*redView=[[UIView?alloc]initWithFrame:CGRectMake(10,?10,?100,?100)];??
  • ????redView.backgroundColor=[UIColor?redColor];??
  • ??????
  • ????[self.view?addSubview:redView];??
  • ????//開始動畫??
  • ????[UIView?beginAnimations:@"test"?context:nil];??
  • ????//動畫時長??
  • ????[UIView?setAnimationDuration:1];??
  • ????/*??
  • ?????*要進(jìn)行動畫設(shè)置的地方??
  • ?????*/??
  • ??????
  • ????redView.backgroundColor=[UIColor?blueColor];??
  • ????redView.frame=CGRectMake(50,?50,?200,?200);??
  • ????redView.alpha=0.5;??
  • ??????
  • ??????
  • ????//動畫結(jié)束??
  • ????[UIView?commitAnimations];??
  • }??
  • ?

    ?

    2、通過代碼塊使用UIKit動畫

    ?

    [plain]?view plaincopy
  • -(void)animationOfBlock??
  • {??
  • ????//初始化一個View,用來顯示動畫??
  • ????UIView?*redView=[[UIView?alloc]initWithFrame:CGRectMake(10,?10,?100,?100)];??
  • ????redView.backgroundColor=[UIColor?redColor];??
  • ??????
  • ????[self.view?addSubview:redView];??
  • ??
  • ????[UIView?animateWithDuration:1?//時長??
  • ??????????????????????????delay:0?//延遲時間??
  • ????????????????????????options:UIViewAnimationOptionTransitionFlipFromLeft//動畫效果??
  • ?????????????????????animations:^{??
  • ???????????????????????????
  • ?????????????????????????//動畫設(shè)置區(qū)域??
  • ?????????????????????????redView.backgroundColor=[UIColor?blueColor];??
  • ?????????????????????????redView.frame=CGRectMake(50,?50,?200,?200);??
  • ?????????????????????????redView.alpha=0.5;??
  • ???????????????????????????
  • ?????????????????????}?completion:^(BOOL?finish){??
  • ???????????????????????//動畫結(jié)束時調(diào)用??
  • ???????????????????????//............??
  • ?????????????????????}];??
  • ??????
  • ??????
  • }??
  • ?

    ?

    使用Core Animation對象來實現(xiàn)動畫

    ?

    在Core Animation中我們經(jīng)常使用的是

    • CABasicAnimation
    • CAKeyframeAnimation
    • CATransitionAnimation

    其中CABasicAnimationCAKeyframeAnimation是對圖層中的不同屬性進(jìn)行動畫的。

    如果要多整個圖層進(jìn)行動畫,則應(yīng)該使用CATransitionAnimation

    如果要使用組合動畫,例如要改變圖層的大小和透明度,則可以先為每個屬性創(chuàng)建一個CABasicAnimation對象,再把他們組合到CAAnimationGroup中,最后把這個組合添加到要進(jìn)行動畫的CALayer中。

    注:CAAnimation(以及CAAnimation的子類),全部都是顯式動畫,這樣動畫播放后,表現(xiàn)層回恢復(fù)到模型層的原始狀態(tài),這就意味著,如果動畫播放完后,會恢復(fù)到原來的樣子,所以在動畫播放完后要對模型層進(jìn)行修改,例如:self.view.layer.backgroundColor=[UIColor blueColor].CGColor;

    1、自定義動畫:CABasicAnimation

    ?

    [plain]?view plaincopy
  • -(void)animationOfCABasicAnimation??
  • {??
  • ????//創(chuàng)建一個CABasicAnimation對象??
  • ????CABasicAnimation?*animation=[CABasicAnimation?animation];??
  • ????//設(shè)置顏色??
  • ????animation.toValue=(id)[UIColor?blueColor].CGColor;??
  • ????//動畫時間??
  • ????animation.duration=1;??
  • ????//是否反轉(zhuǎn)變?yōu)樵瓉淼膶傩灾??
  • ????animation.autoreverses=YES;??
  • ????//把a(bǔ)nimation添加到圖層的layer中,便可以播放動畫了。forKey指定要應(yīng)用此動畫的屬性??
  • ????[self.view.layer?addAnimation:animation?forKey:@"backgroundColor"];??
  • ??????
  • }??
  • ?

    ?

    2、關(guān)鍵幀動畫:CAKeyframeAnimation

    ?

    1. path

    這是一個 CGPathRef ?對象,默認(rèn)是空的,當(dāng)我們創(chuàng)建好CAKeyframeAnimation的實例的時候,可以通過制定一個自己定義的path來讓 ?某一個物體按照這個路徑進(jìn)行動畫。這個值默認(rèn)是nil ?當(dāng)其被設(shè)定的時候 ?values ?這個屬性就被覆蓋?

    2. values

    一個數(shù)組,提供了一組關(guān)鍵幀的值, ?當(dāng)使用path的 時候 values的值自動被忽略。

    下面是改變依次改變view的顏色

    ?

    [plain]?view plaincopy
  • -(void)animationOfCAKeyframeAnimation??
  • {??
  • ????CAKeyframeAnimation?*animation=[CAKeyframeAnimation?animation];??
  • ????//設(shè)置屬性值??
  • ????animation.values=[NSArray?arrayWithObjects:??
  • ??????????????????????(id)self.view.backgroundColor,??
  • ??????????????????????(id)[UIColor?yellowColor].CGColor,??
  • ??????????????????????(id)[UIColor?greenColor].CGColor,??
  • ??????????????????????(id)[UIColor?blueColor].CGColor,nil];??
  • ????animation.duration=3;??
  • ????animation.autoreverses=YES;??
  • ????//把關(guān)鍵幀添加到layer中??
  • ????[self.view.layer?addAnimation:animation?forKey:@"backgroundColor"];??
  • }??


  • ?

    3、使用路徑制作動畫:CAKeyframeAnimation

    ?

    [plain]?view plaincopy
  • -(void)animationOfCAKeyframeAnimationPath??
  • {??
  • ????//初始化一個View,用來顯示動畫??
  • ????UIView?*redView=[[UIView?alloc]initWithFrame:CGRectMake(10,?10,?20,?20)];??
  • ????redView.backgroundColor=[UIColor?redColor];??
  • ??????
  • ????[self.view?addSubview:redView];??
  • ??????
  • ????CAKeyframeAnimation?*ani=[CAKeyframeAnimation?animation];??
  • ????//初始化路徑??
  • ????CGMutablePathRef?aPath=CGPathCreateMutable();??
  • ????//動畫起始點??
  • ????CGPathMoveToPoint(aPath,?nil,?20,?20);??
  • ????CGPathAddCurveToPoint(aPath,?nil,???
  • ??????????????????????????160,?30,//控制點??
  • ??????????????????????????220,?220,//控制點???
  • ??????????????????????????240,?380);//控制點??
  • ??????
  • ????ani.path=aPath;??
  • ????ani.duration=10;??
  • ????//設(shè)置為漸出??
  • ????ani.timingFunction=[CAMediaTimingFunction?functionWithName:kCAMediaTimingFunctionEaseIn];??
  • ????//自動旋轉(zhuǎn)方向??
  • ????ani.rotationMode=@"auto";??
  • ??????
  • ????[redView.layer?addAnimation:ani?forKey:@"position"];??
  • } ?
  • 轉(zhuǎn)載于:https://www.cnblogs.com/liuting-1204/p/5748021.html

    總結(jié)

    以上是生活随笔為你收集整理的使用Core Animation对象来实现动画的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。