現在開發ios項目有兩種或者可以說三種方式:
(1)純代碼
純代碼加Xib
(2)storyboard
so,自定義過渡動畫的方式也有兩種,但是動畫的實現都是一樣的。
先說一下過渡動畫的實現方式:一共有三種說白了都是控制View的CATransition動畫。
第一種:
代碼如下:
NKYellowViewController *dst = [[NKYellowViewController alloc]init];
[UIView transitionWithView:self.navigationController.view
duration:1
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{
[self.navigationController pushViewController:dst animated:NO];
}
completion:nil];
注意:第一個參數爲self.navigationController.view
第二種:
代碼如下:
[UIView transitionFromView:self.view toView:dst.view duration:1 options:UIViewAnimationOptionTransitionFlipFromTop completion:^(BOOL finished) {
[self.navigationController pushViewController:dst animated:NO];
}];
注意:第一個參數爲self.view
第三種:
代碼如下:
CATransition *transition = [CATransition animation];
transition.duration = 1.0f;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
transition.type = kCATransitionMoveIn;
transition.subtype = kCATransitionFromTop;
[self.navigationController.view.layer addAnimation:transition forKey:nil];
[self.navigationController pushViewController:dst animated:YES];
動畫的類型有一下幾種,效果自行嘗試
/*
animation.type = kCATransitionFade;
animation.type = kCATransitionPush;
animation.type = kCATransitionReveal;
animation.type = kCATransitionMoveIn;
animation.type = @"cube";
animation.type = @"suckEffect";
// 頁面旋轉
animation.type = @"oglFlip";
//水波紋
animation.type = @"rippleEffect";
animation.type = @"pageCurl";
animation.type = @"pageUnCurl";
animation.type = @"cameraIrisHollowOpen";
animation.type = @"cameraIrisHollowClose";
*/
接下來說一下純代碼和storyboard的分別實現
(1)純代碼
如果用代碼實現的話,大部分都是寫在事件處理的函數裏,如一下代碼下載button的事件處理函數裏
push:
- (void)pushViewController:(UIButton *)button
{
NKYellowViewController *dst = [[NKYellowViewController alloc]init];
[UIView transitionWithView:self.navigationController.view
duration:1
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{
[self.navigationController pushViewController:dst animated:NO];
}
completion:nil];
}
pop:
- (void)back {
[UIView transitionWithView:self.navigationController.view
duration:1
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{
[self.navigationController popViewControllerAnimated:NO];
}
completion:nil];
}
***動畫代碼可以任意替換,push和pop不對應也可以。
(2)storyboard
界面設計如下:
具體實現:
自定義segue,繼承於UIStoryBoardSegue並重寫perform方法。
<1>繼承關係
@interface NKSegue : UIStoryboardSegue
<2>重寫方法
- (void)perform
{
UIViewController *src = self.sourceViewController;
UIViewController *dst = self.destinationViewController;
[UIView transitionWithView:src.navigationController.view
duration:1
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{
[src.navigationController pushViewController:dst animated:NO];
}
completion:nil];
}
<3>設置segue,在右側面板的屬性檢查器中segue Class設置爲自定義的Segue類