iPhone動畫實現(xiàn)兩種表現(xiàn)方法
iPhone動畫實現(xiàn)兩種表現(xiàn)方法是本文要介紹的內容,實現(xiàn)iphone漂亮的動畫效果主要有兩種方法,一種是UIView層面的,一種是使用CATransition進行更低層次的控制,
第一種是UIView,UIView方式可能在低層也是使用CATransition進行了封裝,它只能用于一些簡單的、常用的效果展現(xiàn),這里寫一個常用的示例代碼,供大家參考。
- [UIView beginAnimations:@"Curl"context:nil];//動畫開始
- [UIView setAnimationDuration:0.75];
- [UIView setAnimationDelegate:self];
- [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:myview cache:YES];
- [myview removeFromSuperview];
- [UIView commitAnimations];
第二種方式相對復雜一些,但如果更好的進行控制,還是使用這種方法吧,基本使用方法可以看一下如下例子:
- CATransition *animation = [CATransition animation];
- [animation setDuration:1.25f];
- [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];
- [animation setType:kCATransitionReveal];
- [animation setSubtype: kCATransitionFromBottom];
- [self.view.layer addAnimation:animation forKey:@"Reveal"];
這里使用了setType與setSubtype組合,這使用個比較保險,因為他的參數(shù)就是官方API里定義的,他們的參數(shù)說明可以參考如下:
setType:可以返回四種類型:
- kCATransitionFade淡出
- kCATransitionMoveIn覆蓋原圖
- kCATransitionPush推出
- kCATransitionReveal底部顯出來
setSubtype:也可以有四種類型:
- kCATransitionFromRight;
- kCATransitionFromLeft(默認值)
- kCATransitionFromTop;
- kCATransitionFromBottom
還有一種設置動畫類型的方法,不用setSubtype,只用setType
- [animation setType:@"suckEffect"];
這里的suckEffect就是效果名稱,可以用的效果主要有:
pageCurl 向上翻一頁 pageUnCurl 向下翻一頁 rippleEffect 滴水效果 suckEffect 收縮效果,如一塊布被抽走 cube 立方體效果 oglFlip 上下翻轉效果
最后再給出一種常用代碼供大家參考。
- // Curl the image up or down CATransition *animation = [CATransition animation];
- [animation setDuration:0.35];
- [animation setTimingFunction:UIViewAnimationCurveEaseInOut];
- if (!curled){
- //animation.type = @"mapCurl";
- animation.type = @"pageCurl";
- animation.fillMode = kCAFillModeForwards;
- animation.endProgress = 0.99;
- } else {
- //animation.type = @"mapUnCurl";
- animation.type = @"pageUnCurl";
- animation.fillMode = kCAFillModeBackwards;
- animation.startProgress = 0.01;
- }
- [animation setRemovedOnCompletion:NO];
- [view exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
- [view addAnimation:animation forKey"pageCurlAnimation"];
- // Disable user interaction where necessary if (!curled) { }
- else {
- }
- curled = !curled;
小結:iPhone動畫實現(xiàn)兩種表現(xiàn)UIView和CATransition方法的內容介紹完了,希望通過本文的學習對你有所幫助!