iOs将动画从左向右(水平)

问题描述:

我几乎是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上运行应用程序。同样的问题。

您可以在自定义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

我试过用塞格也做同样的效果,但没有成功。相反,我使用了一种解决方法:

// 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

这里不使用导航控制器时是定制塞克运行的代码。

-(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

基于扎基尔海德的提交,这里是相同的功能转换为斯威夫特:

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

试试下面这个例子在Github上:

https://github.com/itinance/iOSCustomSegue

它使用自定义SEGUE从右到左在一个示例应用程序中显式没有UINavigationController作为线程开启器的地方。

Sahil的回答中的代码在iOS 8中不适用于我。所以我不得不在这个问题上多调查一下。

'UIView animateWithDuration'适用于iOS 8,而'destinationController.view.layer addAnimation:transition'与presentViewController结合不起作用。