簡體   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