iOs Segue动画从左到右(水平)



我几乎是Xcode 4的新手。有一种方法可以将自定义过渡动画添加到片段中,但它不在界面生成器在情节板管理中提供的四个动画中?具体来说,我想要一个类似于正常的"垂直覆盖",但水平的动画。我希望一个视图从左到右(或从右到左)传递给另一个滑动,而不是在"覆盖垂直"过渡中从上到下。我尝试了滑动手势,但没有成功:即使是从上到下的过渡,无论如何,我不明白为什么所有应用程序的默认过渡通常是从右到左或从左到右,而实际上的过渡是从上到底的,尤其是在你确实滑动的情况下。。。

我也尝试了一种程序化的方法,但即使在这种情况下也没有成功,使用了以下代码:

#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

在接口生成器中,我将这个类定义为我的segue的类。使用断点,我看到它进入函数perfom,但。。。不要表演!我已经在人像模式下阻止了该应用程序(不知道是否有问题)。我试着在真实的ipad和模拟的iphone上运行这个应用程序。同样的问题。

您可以在自定义Segue中使用CATransition来实现从左到右的转换。将#import"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];    

}

根据Zakir Hyder的提交,以下是翻译为Swift的相同功能:

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)     
}

以下是不使用导航控制器时自定义序列的工作代码。

-(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];
}

在Github:上查看此示例

https://github.com/itinance/iOSCustomSegue

它在一个示例应用程序中使用了一个从右到左的自定义segue,显式地不使用UINavigationController作为请求的线程开启器。

Sahil的答案中的代码在iOS 8上对我不起作用。所以我不得不对这个问题进行更多的调查。

"UIView animateWithDuration"适用于iOS 8,而"destinationController.view.layer addAnimation:transition"与presentViewController组合使用时不起任何作用。

我也尝试过使用Segue来达到同样的效果,但没有成功。相反,我使用了一种变通方法:

// 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"];

您可以在此处下载示例项目。干杯

最新更新