繁体   English   中英

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

[英]iOs Segue animation left to right (horizontal)

我几乎是Xcode 4的新手。有一种方法可以在segue中添加一个自定义过渡动画,它不是故事板管理中Interface Builder提供的四种动画之一吗? 具体来说,我想要一个类似于普通“封面垂直”的动画,但是水平。 我希望一个视图从左到右(或从右到左)传递到另一个视图,而不是从“从垂直”过渡到最后到底部。 我尝试了滑动手势,但没有运气:即使那是从上到下的转换,无论如何,我不明白为什么默认转换是从上到下当所有应用程序的默认转换通常是从右到左或从左到右向右,特别是在你刷卡的情况下......

我尝试了一种编程方式,但即使在这种情况下也没有财富,使用此代码:

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

}

这是不使用导航控制器时自定义seque的工作代码。

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

它在一个示例app中从右到左使用自定义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"];

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM