iOS中 Animation 動畫大全 韓俊強的博客

每日更新關注:http://weibo.com/hanjunqiang? 新浪微博!

iOS開發者交流QQ群: 446310206

1.iOS中我們能看到的控件都是UIView的子類,比如UIButton UILabel UITextField UIImageView等等

2.UIView能夠在屏幕的顯示是因為在創建它的時候內部自動添加一個CALayer圖層,通過這個圖層在屏幕上顯示的時候會調用一個drawRect: 的方法,完成繪圖,才能在屏幕上顯示

3.CALayer 本身就具有顯示功能,但是它不能響應用戶的交互事件,如果只是單純的顯示一個圖形,此時你可以使用CALayer創建或者是使用UIView創建,但是如果這個圖形想響應用戶交互事件,必須使用UIView或者子類

動畫知識框圖如下:


#import "ViewController.h"
#import "UITextField+Shake.h"
@interface ViewController ()
@property (retain, nonatomic) IBOutlet UIImageView *balloon;
@property (retain, nonatomic) IBOutlet UITextField *TF;
@property (retain, nonatomic) IBOutlet UIButton *bounces;
@property (retain, nonatomic) IBOutlet UIView *animationView;
@property (retain, nonatomic) IBOutlet UIImageView *cloud;
@end<span style="font-family: 'STHeiti Light';">@implementation ViewController</span>
- (void)viewDidLoad {[super viewDidLoad];//取到當前視圖控制器自帶view的圖層CALayer *layer = self.view.layer;
//    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
//    button.layer //button的圖層//layer 的color必須是CGColorself.animationView.layer.backgroundColor = [UIColor greenColor].CGColor;
}

//給TF創建一個類目:

UITextField+Shake.h
#import <UIKit/UIKit.h>
@interface UITextField (Shake)
- (void)shake;
@endUITextField+Shake.m
#import "UITextField+Shake.h"@implementation UITextField (Shake)
//震動的方法
- (void)shake{CAKeyframeAnimation *keyFrame = [CAKeyframeAnimation animationWithKeyPath:@"position.x"];keyFrame.values = @[@(self.center.x + 10),@(self.center.x),@(self.center.x - 10)];keyFrame.repeatCount = 10;keyFrame.duration = 0.03;[self.layer addAnimation:keyFrame forKey:nil];}
@end


開始動畫按鈕點擊事件

- (IBAction)handleAnimation:(UIButton *)sender {//UIView的屬性動畫[self handlePropertyAnimation];//UIView的屬性動畫 Block形式[self handlePrepertyAnimationBlock];//UIView的過渡動畫[self handleTrabsitionAnimation];//CALayer動畫[self handleCALayer];//CALayer 的基礎動畫[self handleBasicAnimation];//CALayer的關鍵幀動畫[self handleKeyFrameAnimation];//UITextField 調用輸入震動框方法[self.TF shake];//CALayer的過渡動畫[self handleLayerCATransactionAnimation];//CAAinmationGroup 分組動畫[self handleAnimatonGroup];}

每日更新關注:http://weibo.com/hanjunqiang? 新浪微博!


//UIView的屬性動畫 可動畫的屬性 : frame center bounds alpha backgroundColor transfrom

//修改屬性做動畫,動畫結束后屬性修改的結果是真實的作用到動畫的視圖上,不能恢復到之前的樣子

- (void)handlePropertyAnimation{//iOS4.0之前必須把要修改的可動畫屬性寫在begin 和 commit 之間//開始動畫[UIView beginAnimations:nil context:nil];//指定代理 動畫的代理不需要遵循協議,因為此代理就沒有制定協議[UIView setAnimationDelegate:self];//設置動畫的持續時間[UIView setAnimationDuration:3.0];//設置動畫的重復次數 給重復效果旋轉效果立即消失[UIView setAnimationRepeatCount:3.0];//設置動畫的反轉效果[UIView setAnimationRepeatAutoreverses:YES];//設置動畫的變化速度[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];//如果要實現這個方法必須設置代理,此方法在動畫結束后觸發[UIView setAnimationDidStopSelector:@selector(makeAnimationBack)];//修改屬性做動畫//1.center 修改中心點CGPoint center = self.animationView.center;center.y += 10;self.animationView.center =center;//2.修改透明度 alphaself.animationView.alpha = 0.0;//3.變形 tranform//<#CGAffineTransform t#> 之前形變量//旋轉的角度180/4self.animationView.transform = CGAffineTransformRotate(self.animationView.transform, M_PI_4);self.animationView.transform = CGAffineTransformScale(self.animationView.transform, 0.5, 0.5);//提交動畫[UIView commitAnimations];
}

//恢復到視圖之前的狀態

- (void)makeAnimationBack{//self.animationView.center = self.view.center;self.animationView.alpha = 1.0;//恢復到tranform最初狀態,最初狀態就在CGAffineTransformIdentity記錄self.animationView.transform = CGAffineTransformIdentity;}//UIView的屬性動畫 Block形式
- (void)handlePrepertyAnimationBlock{//iOS4.0之后使用block的形式做動畫__block typeof(self)weakSelf = self;//1.block 的第一種形式//01.動畫的持續時間
//    [UIView animateWithDuration:2 animations:^{
//        //1.修改中心點
//        CGPoint center = weakSelf.animationView.center;
//        center.y += 50;
//        weakSelf.animationView.center = center;
//        //2.透明度
//        weakSelf.animationView.alpha = 0.0;
//        //3.變形
//        weakSelf.animationView.transform = CGAffineTransformRotate(weakSelf.animationView.transform, M_PI_4);
//}];//2.block的第二種形式[UIView animateWithDuration:2 animations:^{//1.獲得中心點CGPoint center = weakSelf.animationView.center;//改變中心點center.y += 50;weakSelf.animationView.center =center;//2.透明度weakSelf.animationView.alpha = 0.0;//3.形變修改transformweakSelf.animationView.transform = CGAffineTransformScale(weakSelf.animationView.transform, 0.5, 0.2);} completion:^(BOOL finished) {//返回動畫之前的狀態[weakSelf makeAnimationBack];}];//3.block的第三種形式//01:持續時間//02:動畫執行的延遲時間//03:設置動畫的特效//04:修好的動畫屬性//05:動畫執行結束后的block塊[UIView animateWithDuration:3 delay:1 options:UIViewAnimationOptionAllowAnimatedContent animations:^{//1.獲得中心點CGPoint center = weakSelf.animationView.center;//改變中心點center.y += 50;weakSelf.animationView.center =center;//2.透明度weakSelf.animationView.alpha = 0.0;//3.形變修改transformweakSelf.animationView.transform = CGAffineTransformScale(weakSelf.animationView.transform, 0.5, 0.2);} completion:^(BOOL finished) {//返回動畫之前的狀態[weakSelf makeAnimationBack];}];//block的第四種形式//參數1:動畫持續時間//參數2:動畫的延遲時間//參數3:設置彈簧的強度 范圍(0.0~1.0)//參數4:設置彈簧的速度//參數5:動畫效果//參數6:改變動畫的屬性寫在這里//參數7:結束動畫的時候調用的block[UIView animateWithDuration:2 delay:1 usingSpringWithDamping:0.5 initialSpringVelocity:500 options:UIViewAnimationOptionCurveEaseInOut animations:^{CGPoint center = weakSelf.bounces.center;center.y += 10;weakSelf.bounces.center = center;//transformweakSelf.bounces.transform = CGAffineTransformScale(weakSelf.bounces.transform, 1.2, 1.2);} completion:^(BOOL finished) {CGPoint center = weakSelf.bounces.center;center.y -= 10;weakSelf.bounces.center = center;weakSelf.bounces.transform = CGAffineTransformIdentity;}];
}

每日更新關注:http://weibo.com/hanjunqiang? 新浪微博!

//UIView的過渡動畫

- (void)handleTrabsitionAnimation{__block typeof(self)weakSelf = self;//01:對哪個視圖添加過渡動畫//02:動畫時常//03:動畫效果[UIView transitionWithView:self.animationView duration:2 options:UIViewAnimationOptionAllowAnimatedContent animations:^{weakSelf.animationView.transform = CGAffineTransformRotate(weakSelf.animationView.transform, M_PI_4);} completion:nil];}

//CALayer動畫,修改layer層的屬性做動畫并沒有真實的作用到這個視圖上,動畫知識一種假象

- (void)handleCALayer{//CALyer 動畫就是對layer做動畫//邊框的寬self.animationView.layer.borderWidth = 10.0;//邊框顏色self.animationView.layer.borderColor = [UIColor redColor].CGColor;//切圓角
//    self.animationView.layer.cornerRadius = 100;//取出layer多余的部分
//    self.animationView.layer.masksToBounds = YES;//減掉layer多出的部分
//    self.animationView.clipsToBounds = YES;//背景圖片self.animationView.layer.contents = (id)[UIImage imageNamed:@"WDGJ785Q{`CKL4J}1{_4{(Y.jpg"].CGImage;//視圖一創建出來的時候  錨點 基準點  中心點三個點是重合的//錨點 anchorPoint  決定layer層上的哪個點是position 錨點默認是(0.5,0.5),跟視圖的中心點重合self.animationView.layer.anchorPoint = CGPointMake(0.5, 0);self.animationView.transform = CGAffineTransformRotate(self.animationView.transform, M_PI_4);//基準點 Position 決定當前視圖的layer,在父視圖的位置,它以父視圖的坐標系為準self.animationView.layer.position = CGPointMake(160, 184);
}

//CALayer 的動畫基類:CAAnimation

//CABasicAnimation? 基礎動畫

- (void)handleBasicAnimation{//CA動畫是根據KVC的原理,就修改layer的屬性,以達到做動畫的效果CABasicAnimation *basic = [CABasicAnimation animationWithKeyPath:@"position.x"];basic.fromValue = @(-80);basic.toValue = @(400);//設置動畫持續的時間basic.duration = 5.0;//設置動畫重復的次數basic.repeatCount = 1000;[self.cloud.layer addAnimation:basic forKey:nil];
}

//CAKeyFrameAnimation 關鍵幀動畫

- (void)handleKeyFrameAnimation{CAKeyframeAnimation *keyFrame = [CAKeyframeAnimation animationWithKeyPath:@"position"];CGPoint point1 = self.cloud.center;CGPoint point2 = CGPointMake(160, 100);CGPoint point3 = CGPointMake(270, self.cloud.center.y);//把一組要播放的動畫需求的數值,按順序放到數組中,此時動畫執行的效果,就會按照數組中數據的順序發生變化;//轉化point結構體類型 轉化成對象類型NSValue *value1 = [NSValue valueWithCGPoint:point1];NSValue *value2 = [NSValue valueWithCGPoint:point2];NSValue *value3 = [NSValue valueWithCGPoint:point3];keyFrame.repeatCount = 1000;keyFrame.duration = 15.0;keyFrame.values = @[value1,value2,value3,value1];[self.cloud.layer addAnimation:keyFrame forKey:nil];
}

//CATransition CALayer 的過度動畫

- (void)handleLayerCATransactionAnimation{/*各種動畫效果  其中除了'fade', `moveIn', `push' , `reveal' ,其他屬于似有的API(我是這么認為的,可以點進去看下注釋).*  ↑↑↑上面四個可以分別使用'kCATransitionFade', 'kCATransitionMoveIn', 'kCATransitionPush', 'kCATransitionReveal'來調用.*  @"cube"                     立方體翻滾效果*  @"moveIn"                   新視圖移到舊視圖上面*  @"reveal"                   顯露效果(將舊視圖移開,顯示下面的新視圖)*  @"fade"                     交叉淡化過渡(不支持過渡方向)             (默認為此效果)*  @"pageCurl"                 向上翻一頁*  @"pageUnCurl"               向下翻一頁*  @"suckEffect"               收縮效果,類似系統最小化窗口時的神奇效果(不支持過渡方向)*  @"rippleEffect"             滴水效果,(不支持過渡方向)*  @"oglFlip"                  上下左右翻轉效果*  @"rotate"                   旋轉效果*  @"push"*  @"cameraIrisHollowOpen"     相機鏡頭打開效果(不支持過渡方向)*  @"cameraIrisHollowClose"    相機鏡頭關上效果(不支持過渡方向)*///創建過渡動畫對象CATransition *transition = [CATransition animation];//配置動畫過渡的樣式transition.type = @"cameraIrisHollowClose";//將過渡動畫添加到layer上[self.view.layer  addAnimation:transition forKey:nil];}

//CAAinmationGroup 分組動畫

- (void)handleAnimatonGroup{//1.創建第一個關鍵幀動畫,給熱氣球一個運動軌跡CAKeyframeAnimation *keyframePath = [CAKeyframeAnimation animationWithKeyPath:@"position"];//貝塞爾曲線//1.指定貝塞爾曲線的半徑CGFloat  radius = [UIScreen mainScreen].bounds.size.height / 2.0;//01:圓心//02:半徑//03:開始的角度//04:結束的角度//05:旋轉方向 (YES表示順時針 NO表示逆時針)UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(0, radius) radius:radius startAngle:-M_PI_2 endAngle:M_PI_2 clockwise:YES];//將貝塞爾曲線作為運動軌跡keyframePath.path = path.CGPath;//2.創建第二組關鍵幀動畫,讓熱氣球在運動的時候  由小--->大--->小   ;CAKeyframeAnimation *keyFrameScale = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];//通過一組數據修改熱氣球的大小keyFrameScale.values = @[@1.0,@1.2,@1.4,@1.6,@1.8,@1.6,@1.4,@1.2,@1.0];//創建動畫分組對象CAAnimationGroup *group = [CAAnimationGroup animation];//將兩個動畫效果添加到分組動畫中group.animations = @[keyframePath,keyFrameScale];group.duration = 8;group.repeatCount = 1000;[self.balloon.layer addAnimation:group forKey:nil];}

最終效果:


每日更新關注:http://weibo.com/hanjunqiang? 新浪微博!

iOS開發者交流QQ群: 446310206




本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/287211.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/287211.shtml
英文地址,請注明出處:http://en.pswp.cn/news/287211.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

IROS 2017上,這些廠商將會給我們展示什么樣的黑科技?

相比起大多數AI學術會議&#xff0c;機器人領域最具影響力的學術會議IROS要“好看”得多。在這個學術會議上不僅會有AI和機器人領域最新的研究成果的論文展示&#xff0c;更有不少來自于科研機構和機器人領域公司機器人&#xff0c;向我們展示著展示機器之美。 比如&#xff0c…

《看聊天記錄都學不會C#?太菜了吧》(3)變量:我大哥呢?$:小弟我罩著你!

本系列文章將會以通俗易懂的對話方式進行教學&#xff0c;對話中將涵蓋了新手在學習中的一般問題。此系列將會持續更新&#xff0c;包括別的語言以及實戰都將使用對話的方式進行教學&#xff0c;基礎編程語言教學適用于零基礎小白&#xff0c;之后實戰課程也將會逐步更新。 若…

linux block設備,Linux I/O Block--塊設備的表示

塊設備的分區信息由struct hd_struct結構描述&#xff0c;其中最重要的信息就是分區的起始扇區號和分區的大小。所有分區信息都一起保存在gendisk的part_tbl結構中&#xff0c;同時每個分區的block_device也可以通過bd_part來查詢對應的分區信息。下圖描述了block_device,gendi…

【搶鮮版】ArcGIS 10.7手把手經典圖文安裝教程(附安裝包下載地址)

軟件更新真是個快,ArcGIS10.7已經亮相了!回頭想想,作者追隨ArcGIS已經有11個年頭了(從ArcGIS 9.2到ArcGIS10.7,每個版本都搶鮮使用,先睹為快),本文演示10.7完美安裝過程(附下載地址),親測可用! 目 錄 一、系統環境要求 二、軟件安裝過程 三、軟件下載地址 一、…

Android之解決ViewPager2+PhotoView滑動圖片花屏問題

1 問題 用ViewPager2和開源框架PhotoView(com.github.chrisbanes.photoview.PhotoView)組合實現滑動預覽圖片, 但是部分機型出現花屏效果 2 原因 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas…

請來圍觀:WPF開發的微信客戶端!!!

本文經原作者授權以原創方式二次分享&#xff0c;歡迎轉載、分享。原文作者&#xff1a;眾尋原文鏈接&#xff1a;https://www.cnblogs.com/ZXdeveloper/p/6058206.html公司的同事離職了&#xff0c;接下來的日子可能會忙碌&#xff0c;能完善DEMO的時間也會少了&#xff0c;因…

C#字符串、字節數組和內存流間的相互轉換 - IT浪潮之巔

定義string變量為str,內存流變量為ms,比特數組為bt 1.字符串>比特數組 (1)byte[] btSystem.Text.Encoding.Default.GetBytes("字符串");(2)byte[] btConvert.FromBase64String("字符串"); 補充&#xff1a; System.Text.Encoding.Unicode.GetBytes(str)…

ios-新浪微博-下拉刷新獲取最新的消息(解決消息重復的問題)(五)

2019獨角獸企業重金招聘Python工程師標準>>> 第一步 在上一篇博文的基礎上&#xff0c;利用新浪提供的since_id進行判斷&#xff0c;在刷新監聽的方法中&#xff0c;引入下面的代碼 結果如下圖 轉載于:https://my.oschina.net/iOSliuhui/blog/520495

sqlserver快速查找所有存儲過程中是否包含某字符

--將text替換成你要查找的內容 select name from sysobjects o, syscomments s where o.id s.id and text like %text% and o.xtype P --將text替換成你要查找的內容 SELECT ROUTINE_NAME, ROUTINE_DEFINITION FROM INFORMATION_SCHEMA.ROUTINES WHERE ROUTINE_DEFINITION LI…

《看聊天記錄都學不會Python到游戲實戰?太菜了吧》(7)我用函數寫了個特洛伊木馬

本系列文章將會以通俗易懂的對話方式進行教學&#xff0c;對話中將涵蓋了新手在學習中的一般問題。此系列將會持續更新&#xff0c;包括別的語言以及實戰都將使用對話的方式進行教學&#xff0c;基礎編程語言教學適用于零基礎小白&#xff0c;之后實戰課程也將會逐步更新。 若…

【經典回放】多種語言系列數據結構算法:隊列(C版)

一、隊列ADT以及C語言實現 1 隊列的原理以及ADT分析 隊列是說:把一些數據按先進先出來組織,如同日常生活中的排隊過程。 隊列最主要的操作是 <1> 數據加入隊列;<2> 從隊列中取出數據; 加入隊列只能加入到隊列尾巴上,而從隊列中取出數據、則只能是取出隊列…

linux修改windows注冊表,妙招:讓修改的注冊表立即生效的幾種方法

建站學院(LieHuo.Net)Windows文檔Windows操作系統是全球最廣泛&#xff0c;使用者最多的軟件&#xff0c;熟悉Windows軟件成了電腦操作者必不可少的功課&#xff0c;注冊表作為“Windows的神經系統”非常重要&#xff0c;相信很多朋友都非常熟悉注冊&#xff0c;在開始&#xf…

Android之通過用戶名和密碼連接指定wifi熱點(兼容Android9.0和Android10.0和addNetwork(wifiNewConfiguration)返回-1問題)

1 需求 通過用戶名和密碼連接指定wifi熱點,網上的代碼亂七八糟,沒幾個可以用,我這邊整理了下,測試了華為Android9.0和小米Android9.0和10.0和OPPO Android9.0 都沒問題,直接回調結果就行。 2 問題 在Android10.0手機上 mWifiManager.addNetwork(wifiNewConfiguration);…

hdu 4530(數學)

小Q系列故事——大笨鐘 Time Limit: 600/200 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 1458 Accepted Submission(s): 734 Problem Description飽嘗情感苦惱的小Q本打算隱居一段時間&#xff0c;但僅僅在3月25號一天沒有出現&am…

WPF怎么做新手引導界面?

本文經原作者授權以原創方式二次分享&#xff0c;歡迎轉載、分享。原文作者&#xff1a;眾尋原文鏈接&#xff1a;https://www.cnblogs.com/ZXdeveloper/p/8391864.html這兩天不忙&#xff0c;所以&#xff0c;做了一個簡易的新手引導小Demo。因為&#xff0c;不是項目上應用&a…

最全js表單驗證

/***************************************************************** 表單校驗工具類 (linjq) *****************************************************************//** * 判斷整數num是否等于0 * * param num * return * author jiqinlin */function isIntEqZero(num){ r…

《看聊天記錄都學不會C語言?太菜了吧》(19)鞏固開始,數字1、2、3、4能夠組成多少個 3 位數的不同的排列

若是大一學子或者是真心想學習剛入門的小伙伴可以私聊我&#xff0c;若你是真心學習可以送你書籍&#xff0c;指導你學習&#xff0c;給予你目標方向的學習路線&#xff0c;無套路&#xff0c;博客為證。 本系列文章將會以通俗易懂的對話方式進行教學&#xff0c;對話中將涵蓋…

阿里云MaxCompute香港開服 將引入更多人工智能服務

9月18日&#xff0c;阿里云宣布大數據計算服務MaxCompute在香港正式開服。通過MaxCompute強大的計算能力&#xff0c;阿里云將為香港市場提供更多的人工智能產品&#xff0c;助力當地企業智能化升級。據了解&#xff0c;MaxCompute向用戶提供了完善的數據導入方案以及多種經典的…

【經典回放】多種語言系列數據結構算法:串(C版)

我們這里說的串、就是標準的C語言的串,這點,和我們教材中另行定義的串并不一致。我們這里強調僅僅是按C語言的標準處理串,是因為你會按C語言的標準構造串、而不是按其它的模式定義的。在我們的教材上,串相當與一個: struct ElemType {char *str; }; 構造的順序表、或者是…

Android之解決開啟熱點后跳轉頁面不穩定問題

1 問題 在Android8.0版本以后,開啟熱點我們采用的下面這種方式,但是跳轉頁面后熱點會斷開,手機不能互相傳文件了 權限說明:Android8.0需要位置權限和GPS權限,同時手機熱點還不能是開啟狀態。 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {try {mWifiManag…