2012-03-22 95 views
10

我幾乎是Xcode 4的新手。 有一種方法可以將自定義過渡動畫添加到Segue中,但它不在Interface Builder中呈現的四個故事板管理? 具體來說,我想要一個類似於普通「封面垂直」的動畫,但是要水平。我希望一個視圖可以傳遞到另一個從左到右(或從右到左)的滑動,而不是從上到下,因爲它發生在「覆蓋垂直」過渡中。 我嘗試了滑動手勢,但沒有財富:即使是從上到下過渡,無論如何,我不明白爲什麼默認轉換是從上到下,當所有的應用程序的默認轉換通常是正確的左或從左到右,尤其是在你刷卡的情況下...iOs將動畫從左向右(水平)

我也嘗試了以編程的方式,但沒有財產,即使在這種情況下,使用此代碼:

#import "JHCustomSegue.h" 

@implementation JHCustomSegue 
- (void) perform { 
    UIViewController *src = (UIViewController *) self.sourceViewController; 
    UIViewController *dst = (UIViewController *) self.destinationViewController; 

    [UIView transitionWithView:src.navigationController.view duration:0.5 options:UIViewAnimationOptionTransitionFlipFromLeft animations:^{ 
     [src presentModalViewController:dst animated:NO]; 
    } 
    completion:NULL]; 
} 

@end 

在界面生成器,我定義這作爲我的繼承課。使用斷點,我看到它進入函數perfom,但...不執行! 我以肖像模式封鎖了應用程序(不知道它是否是問題)。我試圖在真正的ipad和模擬iphone上運行應用程序。同樣的問題。

回答

19

您可以在自定義Segue中使用CATransition實現從左到右的過渡。此#進口「QuartzCore/QuartzCore.h」添加到您的自定義Segue公司

-(void)perform { 

__block UIViewController *sourceViewController = (UIViewController*)[self sourceViewController]; 
__block UIViewController *destinationController = (UIViewController*)[self destinationViewController];      

CATransition* transition = [CATransition animation]; 
transition.duration = .25; 
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; 
transition.type = kCATransitionPush; //kCATransitionMoveIn; //, kCATransitionPush, kCATransitionReveal, kCATransitionFade 
transition.subtype = kCATransitionFromLeft; //kCATransitionFromLeft, kCATransitionFromRight, kCATransitionFromTop, kCATransitionFromBottom 



[sourceViewController.navigationController.view.layer addAnimation:transition 
              forKey:kCATransition]; 

[sourceViewController.navigationController pushViewController:destinationController animated:NO];  


} 
+0

屏幕可能從底部向上推動?我使用這個非常自定義的代碼,這正是我所提到的。 – 2013-11-09 11:50:22

+0

我發現問題在哪裏。這是風景,它就像是肖像一樣。 – 2013-11-09 14:07:52

+0

你可以使用kCATransitionFromBottom作爲transition.subtype。請看這裏https://developer.apple.com/library/ios/documentation/GraphicsImaging/Reference/CATransition_Class/Introduction/Introduction.html – 2013-11-10 11:18:29

2

我試過用塞格也做同樣的效果,但沒有成功。相反,我使用了一種解決方法:

// get the view that's currently showing 
UIView *currentView = self.view; 
// get the the underlying UIWindow, or the view containing the current view 
UIView *theWindow = [currentView superview]; 

UIView *newView = aTwoViewController.view; 

// remove the current view and replace with myView1 
[currentView removeFromSuperview]; 
[theWindow addSubview:newView]; 

// set up an animation for the transition between the views 
CATransition *animation = [CATransition animation]; 
[animation setDuration:0.5]; 
[animation setType:kCATransitionPush]; 
[animation setSubtype:kCATransitionFromRight]; 
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]]; 

[[theWindow layer] addAnimation:animation forKey:@"SwitchToView2"]; 

您可以下載示例項目here。乾杯!

+0

哦,非常感謝!我會盡快嘗試! – 2012-06-01 08:52:38

+0

我錯過了補充的一點是,如果你確實找到了一種方法來處理塞格,請告訴我們。 =) – justinkoh 2012-06-04 01:28:04

+0

不,直到現在:)我認爲,但是,它可能更多地與導航控制器一起工作。只要我能,我會投入一些時間來這個。 – 2012-06-04 07:56:05

8

這裏不使用導航控制器時是定製塞克運行的代碼。

-(void)perform 
{ 

UIViewController *sourceViewController = (UIViewController*)[self sourceViewController]; 
UIViewController *destinationController = (UIViewController*)[self destinationViewController]; 

CATransition* transition = [CATransition animation]; 
transition.duration = 0.25; 
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; 
transition.type = kCATransitionMoveIn; //kCATransitionMoveIn; //, kCATransitionPush, kCATransitionReveal, kCATransitionFade 
transition.subtype = kCATransitionFromRight; //kCATransitionFromLeft, kCATransitionFromRight, kCATransitionFromTop, kCATransitionFromBottom 

[destinationController.view.layer addAnimation:transition forKey:kCATransition]; 
[sourceViewController presentViewController:destinationController animated:NO completion:nil]; 
} 
+0

這在Swift中會是什麼樣子? – 2016-05-05 00:01:25

9

基於扎基爾海德的提交,這裏是相同的功能轉換爲斯威夫特:

override func perform() { 

    var sourceViewController = self.sourceViewController as UIViewController 
    var destinationViewController = self.destinationViewController as UIViewController 

    var transition: CATransition = CATransition() 

    transition.duration = 0.25 
    transition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut) 
    transition.type = kCATransitionPush; //kCATransitionMoveIn; //, kCATransitionPush, kCATransitionReveal, kCATransitionFade 
    transition.subtype = kCATransitionFromLeft; //kCATransitionFromLeft, kCATransitionFromRight, kCATransitionFromTop, kCATransitionFromBottom 

    sourceViewController.navigationController?.view.layer.addAnimation(transition, forKey: "kCATransition") 
    sourceViewController.navigationController?.pushViewController(destinationViewController, animated: false)  

} 
+0

如果不使用導航控制器,Swift中的代碼會是什麼樣子? – 2016-05-05 00:01:39

3

試試下面這個例子在Github上:

https://github.com/itinance/iOSCustomSegue

它使用自定義SEGUE從右到左在一個示例應用程序中顯式沒有UINavigationController作爲線程開啓器的地方。

Sahil的回答中的代碼在iOS 8中不適用於我。所以我不得不在這個問題上多調查一下。

'UIView animateWithDuration'適用於iOS 8,而'destinationController.view.layer addAnimation:transition'與presentViewController結合不起作用。