iPhone開發中各種動畫實現效果是本文要介紹的內容,iphone中存在很多好看的動畫效果,用於頁面的切換等。其中某些是apple私人的,據說私人的無法通過apple的審批。最近工作中剛好用到過其中的某些動畫,所以在網上搜了下資料,瞭解了下這些動畫。這裡就自己的理解做一下總結,如有錯誤或遺漏,盡請諒解。
1、UIView 動畫
官方API中,使用UIView可以設定5個動畫效果,分別為:
- UIViewAnimationTransitionNone 不使用動畫
-
- UIViewAnimationTransitionFlipFromLeft 從左向右旋轉翻頁
-
- UIViewAnimationTransitionFlipFromRight 從右向左旋轉翻頁,與UIViewAnimationTransitionFlipFromLeft相反
-
- UIViewAnimationTransitionCurlUp 捲曲翻頁,從下往上
-
- UIViewAnimationTransitionCurlDown 捲曲翻頁,從上往下
-
- 詳細請參見UIViewAnimationTransition
例子:
- [UIView beginAnimations:@"animationID" context:nil];//開始一個動畫塊,第一個參數為動畫塊標識
-
- [UIView setAnimationDuration:0.5f];//設定動畫的期間
-
- [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
- //設定動畫塊中的動畫屬性變化的曲線,此方法必須在beginAnimations方法和commitAnimations,預設即為UIViewAnimationCurveEaseInOut效果。
- 詳細請參見UIViewAnimationCurve
-
- [UIView setAnimationRepeatAutoreverses:NO];//設定是否自動反轉當前的動畫效果
-
- [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.view cache:YES];
- //設定過渡的動畫效果,此處第一個參數可使用上面5種動畫效果
-
- [self.view exchangeSubviewAtIndex:1 withSubviewAtIndex:0];//頁面翻轉
-
- [UIView commitAnimations];//提交動畫
2、公用動畫效果
使用CATransiton可以設定4種動畫效果,分別為:
- NSString * const kCATransitionFade;//漸漸消失
-
- NSString * const kCATransitionMoveIn;//覆蓋進入
-
- NSString * const kCATransitionPush;//推出
-
- NSString * const kCATransitionReveal;//與MoveIn相反
例子:
- CATransition *animation = [CATransition animation];
-
- animation.duration = 0.5f;
-
- animation.timingFunction = UIViewAnimationCurveEaseInOut;
-
- animation.type = kCATransitionPush;//設定上面4種動畫效果
-
- animation.subtype = kCATransitionFromTop;//設定動畫的方向,有四種,
-
- 分別為kCATransitionFromRight、kCATransitionFromLeft、kCATransitionFromTop、kCATransitionFromBottom
-
- [self.view.layer addAnimation:animation forKey:@"animationID"];
3、私人動畫
iphone種還有很多動畫是蘋果私人的,例如刪除照片的動畫等,
私人動畫可以直接在animation.type中傳入動畫的字串即可。動畫有以下幾種:
- cube:像立方體一樣翻轉
-
- suckEffect:漸漸縮小,與刪除照片動畫一樣
-
- oglFlip:上下旋轉,當subType為fromLeft或者fromRight時,
- 與UIViewAnimationTransitionFlipFromLeft和UIViewAnimationTransitionFlipFromRight一樣
-
- rippleEffect:水波效果
-
- pageCurl:與UIViewAnimationTransitionCurlUp一樣
-
- pageUnCurl:與UIViewAnimationTransitionCurlDown一樣
-
- cameraIrisHollowOpen:First half of cameraIris.
-
- cameraIrisHollowClose:Second half of cameraIris
以上所有動畫效果的demo請見http://www.cocoachina.com/bbs/read.php?tid-11820.html,在此感謝樓主的分享,給我的學習帶來很到的協助。
UIViewAnimationState描述:http://www.iphonedevwiki.net/index.php/UIViewAnimationState
同時,本人在使用UIView實現suckEffect縮小的效果過程中遇到一個問題不知道如何定位),經過搜尋終覓得解決方案,分享如下:
- [UIView beginAnimations:@"suck" context:NULL];
- [UIView setAnimationTransition:103 forView:self.view cache:YES];
- [UIView setAnimationDuration:0.5f];
- if (self.interfaceOrientation == UIInterfaceOrientationPortrait || self.interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
- {
- [UIView setAnimationPosition:CGPointMake(44, 42)];
- }else {
- [UIView setAnimationPosition:CGPointMake(320 , 42)];
- }
- [UIView commitAnimations];
其中setAnimationPosition方法就是用於設定縮小點的位置的,此處雖然會報一個警告,但是結果還是正確的。
小結:詳解iPhone開發中各種動畫實現效果的內容介紹完了,希望通過本文的學習能對你有所協助!