UIKit封装的系统动画
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                UIKit封装的系统动画
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.                        
                                簡介
在UIKit中,對UIView封裝了很多類方法來進行簡單的動畫實現(xiàn),在動畫過程中,通過對屬性值的修改來完成一系列的效果。 在IOS4以前,主要通過 + beginAnimation + setAnimationDuration:設置動畫時長 + setAnimationDelay:設置延遲時間 + setAnimationDelegate:設置代理
code..... 寫入一些屬性改變例如仿射變換,透明度等
+ commitAnimation
代理可以監(jiān)聽一些事件,比如動畫結束后,可以調(diào)用代理方法進行一系列處理。
在IOS4以后,伴隨著Block語法,有了更好的方法 + animateWithDuration:delay:options:animations:completion:
前兩個屬性前面見過,第三個屬性主要設置動畫的速度效果,比如漸入漸出(EaseInOut),勻速(Linear)等
animations后面是一個塊語法,設置動畫的相關效果。 completion后面也是一個塊語法,設置動畫完成后執(zhí)行的代碼。
簡單的位移動畫
- (void)translateAnimation {[UIView animateWithDuration:1delay:1options:UIViewAnimationOptionCurveEaseInOutanimations:^{_imageView.center = CGPointMake(270, 410);} completion:^(BOOL finished) {NSLog(@"done");}]; }
這個方法實現(xiàn)了通過設置屬性的位移動畫
我們還可以通過這個類方法對透明度,大小等等幾乎所有屬性進行改變增加動畫效果
增加仿射變換
- (void)transformAnimation {[UIView animateWithDuration:3delay:1options:UIViewAnimationOptionCurveEaseInanimations:^{_imageView.center = CGPointMake(270, 410);_imageView.transform = CGAffineTransformRotate(CGAffineTransformScale(_imageView.transform, 0.6, 0.6), M_PI);_imageView.alpha = 0.0;} completion:^(BOOL finished) {NSLog(@"done");}]; }
在這個方法中,對UIImageView的transform屬性設置進行了嵌套,在旋轉180度的同時進行了縮放。由于設置了alpha,所以也還有一個漸漸消失的效果。 一般來說,如果通過設置alpha為0后,需要從父視圖中remove掉這個對象。
利用completion設置連續(xù)動畫
- (void)transformAnimation {[UIView animateWithDuration:3delay:1options:UIViewAnimationOptionCurveEaseInanimations:^{_imageView.center = CGPointMake(270, 410);_imageView.transform = CGAffineTransformRotate(CGAffineTransformScale(_imageView.transform, 0.6, 0.6), M_PI);_imageView.alpha = 0.0;} completion:^(BOOL finished) {NSLog(@"done");[UIView animateWithDuration:3delay:0options:UIViewAnimationOptionCurveEaseInanimations:^{_imageView.center = CGPointMake(50, 50);_imageView.transform = CGAffineTransformIdentity;_imageView.alpha = 1.0;} completion:nil];}]; }
我們在上個方法的基礎上進行了修改,在completion中又加入了一個動畫效果,使這個圖片恢復到最初的狀態(tài)。 這里面CGAffineTransformIdentity為單位矩陣,是他的transform屬性回復到原貌。
利用NSTimer完成連續(xù)動畫
我們也可以使用定時器來完成連續(xù)動畫效果
先增加兩個私有成員,并且可以根據(jù)調(diào)試效果來設置_step初值
@interface ViewController () {NSTimer *_timer;NSInteger _step; }
然后是方法
- (void)timerAnimation {_timer = [NSTimer scheduledTimerWithTimeInterval:0.05target:selfselector:@selector(animateWithTimer)userInfo:nilrepeats:YES]; }- (void)animateWithTimer {if (_step == 0) {[_timer invalidate];[_imageView removeFromSuperview];}_imageView.transform = CGAffineTransformRotate(CGAffineTransformScale(_imageView.transform, 0.98, 0.98), ((10 * M_PI) / 180.0));_imageView.alpha *= 0.98;_step--; }
雖然沒有使用UIView封裝的方法,但是也簡單的實現(xiàn)了一個動畫效果。
以上就是本篇博客全部內(nèi)容,歡迎指正和交流。轉載注明出處~
?
總結
以上是生活随笔為你收集整理的UIKit封装的系统动画的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: 梦到自己游泳代表什么
 - 下一篇: 梦到别人开枪打人代表什么意思