Xcode 自定义转场 - Cover Vertical 的对面(从上到下) - 完整的新手

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14081880/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 02:25:12  来源:igfitidea点击:

Xcode custom segue - Opposite of Cover Vertical (top to bottom) - COMPLETE NEWBIE

xcodesegue

提问by BrownEye

I have been trying for some time to create a custom segue that does the opposite of the cover vertical (to create a from top to bottom effect animation).I had looked through other questions, google and youtube but cannot get it working.I am completely new to the classes etc. so any help in a step by step guide or video would be so good. I believe that this URL is trying to do something similar to what I want:

我一直在尝试创建一个与垂直封面相反的自定义转场(以创建从上到下的效果动画)。我已经浏览了其他问题,谷歌和 youtube,但无法让它工作。我是对课程等完全陌生,因此分步指南或视频中的任何帮助都会非常好。我相信这个 URL 正在尝试做一些类似于我想要的事情:

http://jrwren.wrenfam.com/blog/2012/02/01/storyboard-custom-segue-for-custom-pushviewcontroller-animation/, I just cannot get it working.

http://jrwren.wrenfam.com/blog/2012/02/01/storyboard-custom-segue-for-custom-pushviewcontroller-animation/,我就是无法让它工作。

If any of you know a way to create a segue that is a top to bottom version of the cover vertical segue could you please help me out?

如果你们中的任何人知道一种创建从上到下版本的封面垂直转场的转场的方法,您能帮我吗?

采纳答案by dmason82

Well, you would want to create a subclass of UIStoryBoardSegue, like the walkthrough shows you, however under the Storyboard class' .m file(implementation) you would want the following code as your -(void)perform: method -

好吧,您可能想要创建 UIStoryBoardSegue 的子类,就像演练向您展示的那样,但是在 Storyboard 类的 .m 文件(实现)下,您需要以下代码作为您的 -(void)perform: 方法 -

-(void)perform{
UIViewController *sourceViewController = (UIViewController *) self.sourceViewController;
UIViewController *destinationViewController = (UIViewController *) self.destinationViewController;
[sourceViewController.view addSubview:destinationViewController.view];
[destinationViewController.view setFrame:sourceViewController.view.window.frame];
[destinationViewController.view setTransform:CGAffineTransformMakeTranslation(0, -sourceViewController.view.frame.size.height)];
[destinationViewController.view setAlpha:1.0];

[UIView animateWithDuration:0.75
                      delay:0.0
                    options:UIViewAnimationOptionTransitionFlipFromTop
                 animations:^{
                     [destinationViewController.view setTransform:CGAffineTransformMakeTranslation(0, 0)];
                     [destinationViewController.view setAlpha:1.0];
                 }
                 completion:^(BOOL finished){
                     [destinationViewController.view removeFromSuperview];
                     [sourceViewController presentViewController:destinationViewController animated:NO completion:nil];
                 }];}

Hopefully this is helpful.

希望这是有帮助的。

回答by coco

Building on @dmason82's answer, translating to Swift, and simplifying a little, this works for me:

以@dmason82 的回答为基础,转换为 Swift,并稍微简化一下,这对我有用:

class UIStoryboardSegueFromTop: UIStoryboardSegue {
    override func perform() {
        let src = self.sourceViewController as UIViewController
        let dst = self.destinationViewController as UIViewController

        src.view.superview?.insertSubview(dst.view, aboveSubview: src.view)
        dst.view.transform = CGAffineTransformMakeTranslation(0, -src.view.frame.size.height)

        UIView.animateWithDuration(1.0, animations: {
            dst.view.transform = CGAffineTransformMakeTranslation(0, 0)

            }) { (Finished) in
                src.presentViewController(dst, animated: false, completion: nil)
        }
    }
}

回答by Victor Carre?o

Here is my implementation of an Opposite Cover Vertical Segue using Storyboards

这是我使用 Storyboards 实现的 Opposite Cover Vertical Segue

https://github.com/viccarre/OppositeCoverVerticalSegue

https://github.com/viccarre/OppositeCoverVerticalSegue

回答by kanstraktar

To complete dmason's answer and have your controller unwind in the opposite direction - going back up from where it came, do this in the presented view controller:

要完成 dmason 的回答并使您的控制器向相反方向展开 - 从它来的地方返回,请在显示的视图控制器中执行此操作:

[UIView animateWithDuration:0.75
                      delay:0.0
                    options:UIViewAnimationOptionTransitionFlipFromBottom
                 animations:^{
                     [self.view setTransform:CGAffineTransformMakeTranslation(0, -self.view.frame.size.height)];
                 }
                 completion:^(BOOL finished){
                     [self dismissViewControllerAnimated:NO completion:nil];
                 }
 ];

Bergy's answer didn't work for me.

伯吉的回答对我不起作用。

回答by mouh_stach007

Hi I just tried the two other answer but it doesn't work as you ask. In order to create a custom segue looking the exact reverse of cover vertical (modal segue default) I created this code :

嗨,我刚刚尝试了另外两个答案,但没有按照您的要求工作。为了创建一个自定义的 segue,看起来与封面垂直(模式 segue 默认)完全相反,我创建了这个代码:

- (void)perform
{
UIViewController *sourceViewController = self.sourceViewController;
UIViewController *destinationViewController = self.destinationViewController;


[sourceViewController presentViewController:destinationViewController animated:NO completion:nil];
[destinationViewController.view addSubview:sourceViewController.view];
[sourceViewController.view setTransform:CGAffineTransformMakeTranslation(0, 0)];
[sourceViewController.view setAlpha:1.0];

[UIView animateWithDuration:0.75
                      delay:0.0
                    options:UIViewAnimationOptionTransitionFlipFromBottom
                 animations:^{
                     [sourceViewController.view setTransform:CGAffineTransformMakeTranslation(0,destinationViewController.view.frame.size.height)];
                     [sourceViewController.view setAlpha:1.0];
                 }
                 completion:^(BOOL finished){
                     [sourceViewController.view removeFromSuperview];
                     }];

}


@end

回答by fredericdnd

I advise you to use the Hero library to achieve this. It adds a lot of animations and transitions, notably between view controllers : https://github.com/lkzhao/Hero/blob/master/README.mdThere is an transition called "Cover", and you can choose the direction (left, top, right, bottom)

我建议您使用 Hero 库来实现这一点。它添加了很多动画和过渡,尤其是在视图控制器之间:https://github.com/lkzhao/Hero/blob/master/README.md有一个名为“Cover”的过渡,您可以选择方向(左,顶部,右侧,底部)

回答by bergy

dmason's answer is great, and if you want to add an unwind segue so your modal animates properly when it's closed, your perform action could look like this (in a new custom segue class)

dmason 的回答很好,如果你想添加一个 unwind segue 以便你的模态在关闭时正确动画,你的执行操作可能看起来像这样(在一个新的自定义 segue 类中)

- (void)perform
{
    UIViewController *sourceViewController = self.sourceViewController;
    UIViewController *destinationViewController = self.destinationViewController;

    [sourceViewController dismissViewControllerAnimated:NO completion:nil];
    [destinationViewController.view addSubview:sourceViewController.view];

    [UIView animateWithDuration:0.75
                     animations:^{
                         sourceViewController.view.center = CGPointMake(sourceViewController.view.center.x, sourceViewController.view.center.y-600);
                     }
                     completion:^(BOOL finished){
                         [sourceViewController.view removeFromSuperview];
                     }
     ];
}