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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

iOS动画:UIView动画和CALayer动画(CABasicAnimation、CAKeyframeAnimation的使用)

發布時間:2024/4/15 编程问答 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 iOS动画:UIView动画和CALayer动画(CABasicAnimation、CAKeyframeAnimation的使用) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

iOS中的動畫有兩種實現方式,一種是UIView來實現動畫,另一種動畫是通過CALayer來實現,下面介紹兩種動畫的簡單實現:



一、UIView動畫的實現

? ?UIView使用Context來實現動畫

關鍵代碼:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 //參數1?動畫名稱?參數2?要實現動畫的對象上下文 ????? ????[UIView?beginAnimations:@"attribute"?context:_showImageView]; ????? ????//設置動畫的時間 ????[UIView?setAnimationDuration:1.0f]; ????? ????//設置動畫延遲時間 //????[UIView?setAnimationDelay:2]; ????? ????//設置視圖center?實現試圖移動動畫 ????_showImageView.center?=?CGPointMake(100,?100); ????? ????//設置alpha值:視圖透明度 ????_showImageView.alpha?=?0.2f; ????? ????//設置背景顏色 ????_showImageView.backgroundColor?=?[UIColor?greenColor]; ????? ????//UIView動畫?設置代理 ????[UIView?setAnimationDelegate:self]; ????? ????//動畫將要開始代理方法 ????[UIView?setAnimationWillStartSelector:@selector(animationWillStart:context:)]; ????? ????//動畫已經結束代理方法 ????[UIView?setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)]; ????? ????//提交動畫設置,執行動畫 ????[UIView?commitAnimations];



使用Block實現的動畫:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 //UIView動畫,?使用Block實現 ????[UIView?animateWithDuration:1.0f?animations:^{ ????????? ????????//通過設置translation?實現視圖的偏移 ????????if?([self.mySwitch?isOn])?{ ????????????? ????????????//基于上一次的translation ????????????_showImageView.transform?=?CGAffineTransformTranslate(_showImageView.transform,?50,?0); ????????}?else?{ ????????????? ????????????//基于原始的translation ????????????_showImageView.transform?=?CGAffineTransformMakeTranslation(-50,?0); ????????} ????}];



二、CALayer動畫的實現

CABasic動畫的實現:根據初始位置和結束位置確定動畫

1 2 3 4 5 6 //CABasic?有兩個屬性?fromValue?動畫開始值,toValue動畫結束值 ????CABasicAnimation?*animation1?=?[CABasicAnimation?animationWithKeyPath:@"position"]; ????[animation1?setDuration:2]; ????animation1.fromValue?=?[NSValue?valueWithCGPoint:CGPointMake(150,?150)]; ????animation1.toValue?=?[NSValue?valueWithCGPoint:CGPointMake(200,?200)]; ????[_imageView.layer?addAnimation:animation1?forKey:@"position"];



創建一組動畫:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 //創建組動畫對象 ????CAAnimationGroup?*group?=?[CAAnimationGroup?animation]; ????? ????//CABasic動畫 ????CABasicAnimation?*animation1?=?[CABasicAnimation?animationWithKeyPath:@"transform.scale.y"]; ????animation1.fromValue?=?@1.5; ????animation1.toValue?=?@0.5; ????? ????//關鍵幀動畫 ????CAKeyframeAnimation?*animation2?=?[CAKeyframeAnimation?animationWithKeyPath:@"position"]; ????animation2.values?=?@[[NSValue?valueWithCGPoint:CGPointMake(100,?100)], ?????????????????????????[NSValue?valueWithCGPoint:CGPointMake(200,?150)], ?????????????????????????[NSValue?valueWithCGPoint:CGPointMake(100,?200)], ?????????????????????????[NSValue?valueWithCGPoint:CGPointMake(200,?250)]]; ????? ????//group添加動畫數組,group中動畫對象并發執行 ????[group?setAnimations:@[animation1,?animation2]]; ????[group?setDuration:4.0f]; ????[_imageView.layer?addAnimation:group?forKey:@"group"];


總結

以上是生活随笔為你收集整理的iOS动画:UIView动画和CALayer动画(CABasicAnimation、CAKeyframeAnimation的使用)的全部內容,希望文章能夠幫你解決所遇到的問題。

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