轉(zhuǎn)帖|使用教程|編輯:我只采一朵|2014-09-01 10:12:28.000|閱讀 1779 次
概述:在 iOS 系統(tǒng)中,Core Animation提供了內(nèi)置的動畫支持,創(chuàng)建動畫不需要任何繪圖的代碼,你要做的只是激發(fā)指定的動畫, 接下來就交給 Core Animation 來渲染??傊?,復(fù)雜的動畫只需要幾行代碼就可以了。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
動畫為用戶界面的狀態(tài)轉(zhuǎn)換提供了流暢的可視化效果,在iOS中大量使用了動畫效果, 包括改變視圖位置、大小、從可視化樹中刪除視圖,隱藏視圖等。你可以考慮用動畫效果給用戶提供反饋或者用來實(shí)現(xiàn)有趣的特效。
在 iOS 系統(tǒng)中,Core Animation提供了內(nèi)置的動畫支持,創(chuàng)建動畫不需要任何繪圖的代碼,你要做的只是激發(fā)指定的動畫, 接下來就交給 Core Animation 來渲染??傊瑥?fù)雜的動畫只需要幾行代碼就可以了。
根據(jù) 中說明, UIView 內(nèi)置支持為下列屬性添加動畫效果:
為了給屬性的變化添加動畫效果, 需要把修改這些屬性的代碼放到指定的動畫代碼段 (animation block) 中。 只有在動畫代碼段中修改支持動畫的屬性, 才能添加動畫效果。
在 iOS 3.0 以及之前的系統(tǒng)中, 必須使用 UIView 的類方法 和 來定義動畫代碼段, 在 begin 和 commit 之間的代碼會在特殊的動畫線程中運(yùn)行, 因此不會阻塞主線程, 比如說要切換兩個視圖, 代碼應(yīng)該是這樣子的:
[UIView beginAnimations:@"ToggleViews" context:nil]; [UIView setAnimationDuration:1.0]; // Make the animatable changes. firstView.alpha = 0.0; secondView.alpha = 1.0; // Commit the changes and perform the animation. [UIView commitAnimations];
在 Xamarin.iOS (MonoTouch) 平臺下, begin/end 方法對應(yīng)的綁定為:
public static void BeginAnimations (string animation) public static void BeginAnimations (string animationID, IntPtr context) public static void CommitAnimations ()
上面的切換視圖的 C# 版本代碼為:
UIView.BeginAnimations("ToggleViews"); UIView.SetAnimationDuration(1.0) this.FirstView.Alpha = 0.0; this.SecondView.Alpha = 1.0; UIView.CommitAnidations();
在 Begin/Commit 函數(shù)之間, 可以通過下面的方法設(shè)置動畫的參數(shù)和選項(xiàng):
setAnimationStartDate: setAnimationDelay: setAnimationDuration: setAnimationCurve: setAnimationRepeatCount: setAnimationRepeatAutoreverses: setAnimationDelegate: setAnimationWillStartSelector: setAnimationDidStopSelector: setAnimationBeginsFromCurrentState:
注意: 如果不是為了支持很舊的設(shè)備,則推薦使用下面的 lambda (block based method) 來實(shí)現(xiàn)動畫效果, 雖然 begin/commit 還能夠使用, 按照官方的說法, 對新系統(tǒng)來說是不推薦的了。
在 iOS 4.0 以后, 引入了代碼塊 (code block) 的概念, 可以使用代碼塊來初始化動畫, 這也是在 iOS 4.0 之后蘋果推薦的做法, iOS SDK 提供的 API 如下:
animateWithDuration:animations: animateWithDuration:animations:completion: animateWithDuration:delay:options:animations:completion:
而在 Xamarin.iOS (MonoTouch) 平臺下, 這些方法被綁定為下列方法:
public static void Animate(double duration, NSAction animation) public static void Animate (double duration, NSAction animation, NSAction completion) public static void Animate (double duration, double delay, UIViewAnimationOptions options, NSAction animation, NSAction completion)
還是切換視圖的動畫, 如果用 objective-c 的代碼塊來實(shí)現(xiàn), 則應(yīng)該是這樣子的:
[UIView animateWithDuration:1.0 animations:^{ self.firstView.alpha = 0.0; self.secondView.alpha = 1.0; }];
如果用 C# 來實(shí)現(xiàn)的話, 應(yīng)該是這樣:
UIView.Animate(1.0, () => { this.FirstView.Alpha = 0.0f; this.SecondView.Alpha = 1.0f; });
這樣就實(shí)現(xiàn)了一個簡單的漸變動畫, 并且只能運(yùn)行一次, 通常不能滿足需求, 再來一個復(fù)雜點(diǎn)兒的:
[UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationOptionCurveEaseIn animations:^{ self.firstView.alpha = 0.0; } completion:^(BOOL finished){ [UIView animateWithDuration:1.0 delay:1.0 options:UIViewAnimationOptionCurveEaseOut animations:^{ self.firstView.alpha = 1.0; } completion:nil]; }];
對應(yīng)的 C# 代碼如下:
UIView.Animate( 1.0, 0.0, UIViewAnimationOptions.CurveEaseIn, () => this.FirstView.Alpha = 0.0f, () => { UIView.Animate( 1.0, 1.0, UIViewAnimationOptions.CurveEaseOut, () => this.FirstView.Alpha = 1.0f, null ); } );
iOS 支持嵌套的動畫, 也就是說在一個動畫代碼段中, 可以再開始另外一個動畫代碼段, 而不必等當(dāng)前動畫完成, 嵌套的動畫會同時開始運(yùn)行, 默認(rèn)繼承原來動畫的延時、 時間長度、 加速曲線等, 不過這些選項(xiàng)也能被覆蓋。 例如:
[UIView animateWithDuration:1.0 delay:1.0 options:UIViewAnimationOptionCurveEaseOut animations:^{ self.firstView.alpha = 0.0f; // 這里開始一個新的動畫 [UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationOptionOverrideInheritedCurve | UIViewAnimationOptionCurveLinear | UIViewAnimationOptionOverrideInheritedDuration | UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse animations:^{ [UIView setAnimationRepeatCount:2.5]; self.secondView.alpha = 0.0f; } completion:nil]; } completion:nil ];
對應(yīng)的 C# 代碼如下:
UIView.Animate( 1.0, 1.0, UIViewAnimationOptions.CurveEaseIn, () => { this.FirstView.Alpha = 0.0; UIView.Animate( 1.0, 1.0, UIViewAnimationOptions.OverrideInheritedCurve | UIViewAnimationOptions.CurveLinear | UIViewAnimationOptions.OverrideInheritedDuration | UIViewAnimationOptions.Repeat | UIViewAnimationOptions.Autoreverse, () => { UIView.SetAnimationRepeatCount(2.f); this.SecondView.Alpha = 0.0; }, null ); }, null );
對于使用 Begin/Commit 方法的動畫, 也可以嵌套調(diào)用 Begin/Commit 方法來實(shí)現(xiàn)嵌套的動畫, 例如:
UIView.BeginAnimations("Animation1"); // Animation code goes here // Start another animation UIView.BeginAnimations("Nested animation"); // nested animations code goes here. UIView.CommitAnimations(); // other code UIView.CommitAnimations();
這段 C# 代碼對應(yīng)的 ObjC 代碼很簡單, 就不寫出來了。
當(dāng)創(chuàng)建自動翻轉(zhuǎn)指定次數(shù)的動畫時, 考慮將重復(fù)次數(shù)設(shè)置為非整數(shù)值。 因?yàn)閷τ谧詣臃D(zhuǎn)的動畫來說, 每次循環(huán)都是從原始值變化到目標(biāo)值再變化回原始值, 如果希望動畫結(jié)束之后停留在目標(biāo)值, 需要將重復(fù)次數(shù)設(shè)置加上 0.5 , 否則, 動畫回慢慢變回原始值, 再迅速變化到目標(biāo)值, 這可能不是原來期望的動畫效果。
視圖切換動畫可以減少修改可視化樹時引起的界面上的突變, iOS 系統(tǒng)中大量使用了視圖切換動畫, 視圖切換動畫主要有下面兩種場景:
注意: 不要把視圖切換和視圖控制器的切換混淆(顯示一個模式對話框、將視圖控制器推入導(dǎo)航堆棧等), 視圖切換改變的僅僅是視圖的可視化樹, 視圖控制器是不變的, 更多信息可以參考。
可以修改子視圖的可見性用來表示當(dāng)前視圖的不同的狀態(tài), 看下面的兩個視圖切換的例子,在 iOS 4.0 之前, 需要將視圖切換動畫添加到 Begin/Commit 動畫之間, 代碼如下:
在 iOS 4.0 之后, 可以使用
[UIView transitionWithView:self.view duration:1.0 options:UIViewAnimationOptionTransitionCurlUp animations:^{ self.currentView.hidden = YES; self.swapView.hidden = NO; } completion:^(BOOL finished) { UIView *tmp = self.currentView; self.currentView = self.swapView; self.swapView = tmp; } ];
在 iOS 4.0 之前需要用到的函數(shù)是 對應(yīng)的代碼如下:
[UIView beginAnimations:@"toggleView" context:nil]; [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view cache:YES]; [UIView setAnimationDuration:1.0]; // animation goes here self.currentView.hidden = YES; self.swapView.hidden = NO; [UIView commitAnimations];
這里只有動畫部分的代碼, 動畫完成之后請參考 方法設(shè)置并實(shí)現(xiàn) UIAnimationDelegate 。
要進(jìn)行子視圖的替換, 需要用到 方法, 示例代碼如下:
UIView *fromView = (self.displayPrimary ? self.view : self.secondView); UIView *toView = (self.displayPrimary ? self.secondView : self.view); UIViewAnimationOptions option = (self.displayPrimary ? UIViewAnimationOptionTransitionFlipFromRight : UIViewAnimationOptionTransitionFlipFromLeft); [UIView transitionFromView:fromView toView:toView duration:1.0 options:option completion:^(BOOL finished) { if (finished) { self.displayPrimary = !self.displayPrimary; } } ];
有了上面的知識, 鏈接多個動畫就非常簡單了:
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn