日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

使用Core Animation对象来实现动画

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

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

在iOS中如果使用普通的動畫則可以使用UIKit提供的動畫方式來實現,如果想實現更復雜的效果,則需要使用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];??
  • ????/*??
  • ?????*要進行動畫設置的地方??
  • ?????*/??
  • ??????
  • ????redView.backgroundColor=[UIColor?blueColor];??
  • ????redView.frame=CGRectMake(50,?50,?200,?200);??
  • ????redView.alpha=0.5;??
  • ??????
  • ??????
  • ????//動畫結束??
  • ????[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:^{??
  • ???????????????????????????
  • ?????????????????????????//動畫設置區(qū)域??
  • ?????????????????????????redView.backgroundColor=[UIColor?blueColor];??
  • ?????????????????????????redView.frame=CGRectMake(50,?50,?200,?200);??
  • ?????????????????????????redView.alpha=0.5;??
  • ???????????????????????????
  • ?????????????????????}?completion:^(BOOL?finish){??
  • ???????????????????????//動畫結束時調用??
  • ???????????????????????//............??
  • ?????????????????????}];??
  • ??????
  • ??????
  • }??
  • ?

    ?

    使用Core Animation對象來實現動畫

    ?

    在Core Animation中我們經常使用的是

    • CABasicAnimation
    • CAKeyframeAnimation
    • CATransitionAnimation

    其中CABasicAnimationCAKeyframeAnimation是對圖層中的不同屬性進行動畫的。

    如果要多整個圖層進行動畫,則應該使用CATransitionAnimation

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

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

    1、自定義動畫:CABasicAnimation

    ?

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

    ?

    2、關鍵幀動畫:CAKeyframeAnimation

    ?

    1. path

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

    2. values

    一個數組,提供了一組關鍵幀的值, ?當使用path的 時候 values的值自動被忽略。

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

    ?

    [plain]?view plaincopy
  • -(void)animationOfCAKeyframeAnimation??
  • {??
  • ????CAKeyframeAnimation?*animation=[CAKeyframeAnimation?animation];??
  • ????//設置屬性值??
  • ????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;??
  • ????//把關鍵幀添加到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;??
  • ????//設置為漸出??
  • ????ani.timingFunction=[CAMediaTimingFunction?functionWithName:kCAMediaTimingFunctionEaseIn];??
  • ????//自動旋轉方向??
  • ????ani.rotationMode=@"auto";??
  • ??????
  • ????[redView.layer?addAnimation:ani?forKey:@"position"];??
  • } ?
  • 轉載于:https://www.cnblogs.com/liuting-1204/p/5748021.html

    總結

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

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。