简体   繁体   English

如何使这个segue动画退出到右侧或左侧? - 斯威夫特

[英]How to make this segue animation exit to right or left side? - Swift

This code works fine to make my custom segue transition with a downwards animation. 此代码可以正常使用向下动画进行自定义segue过渡。 How would I make the animation look as if it is transitioning to the left or right? 如何使动画看起来像是向左或向右转换?

Also - I am using a UISwipeGestureRecognizer to trigger the segue. 另外 - 我正在使用UISwipeGestureRecognizer来触发segue。 It works fine except that the segue occurs as soon as I do the smallest swipe. 它工作正常,除了我做最小的滑动时发生segue。 How would I allow the segue to not occur untill a user has lifted their finger off or atleast done a larger swipe. 如果用户已将手指抬起或至少进行了较大的滑动,我将如何允许不发生segue。 Would I need to use a scrollView for this? 我需要使用scrollView吗?

Below is my code for my UISwipeGestureRecognizer. 下面是我的UISwipeGestureRecognizer的代码。

override func viewDidLoad() {

    var swipeGestureRecognizer: UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: "showSecondViewController")
    swipeGestureRecognizer.direction = UISwipeGestureRecognizerDirection.Up
    self.view.addGestureRecognizer(swipeGestureRecognizer)

}    

The code below is my animation code 下面的代码是我的动画代码

class FirstCustomSegue: UIStoryboardSegue {

override func perform() {

    // Specify the initial position of the destination view.
    secondVCView.frame = CGRectMake(0.0, screenHeight, screenWidth, screenHeight)

    // Access the app's key window and insert the destination view above the current (source) one.
    let window = UIApplication.sharedApplication().keyWindow
    window?.insertSubview(secondVCView, aboveSubview: firstVCView)

    // Animate the transition.
    UIView.animateWithDuration(0.5, animations: { () -> Void in
        firstVCView.frame = CGRectOffset(firstVCView.frame, 0.0, -screenHeight)
        secondVCView.frame = CGRectOffset(secondVCView.frame, 0.0, -screenHeight)

        }) { (Finished) -> Void in

                    self.sourceViewController.presentViewController(self.destinationViewController as! UIViewController,
                        animated: false,
                        completion: nil)
            }
    }
}

Animation Direction 动画方向

You will simply need to change the initial and destination frames. 您只需更改初始帧和目标帧即可。 Currently the secondVCView 's initial frame's X,Y origin 0.0 , screenHeight , meaning it sits just below the bottom of the screen. 目前第二个secondVCView的初始帧的X,Y原点为0.0screenHeight ,意味着它位于屏幕底部正下方。 If you want it to appear from the right you would need to change it to screenWidth , 0.0 . 如果您希望它从右侧出现,则需要将其更改为screenWidth0.0

Then the destination frame of firstVCView would change so the origin is -screenWidth , 0.0 and the destination frame of secondVCView would change so the origin is 0.0 , 0.0 然后目的地帧firstVCView将改变所以原点是-screenWidth0.0和目的地帧secondVCView将改变所以原点是0.00.0

override func perform() {

    // Specify the initial position of the destination view.
    secondVCView.frame = CGRectMake(screenWidth, 0.0, screenWidth, screenHeight)

    // Access the app's key window and insert the destination view above the current (source) one.
    let window = UIApplication.sharedApplication().keyWindow
    window?.insertSubview(secondVCView, aboveSubview: firstVCView)

    // Animate the transition.
    UIView.animateWithDuration(0.5, animations: { () -> Void in
        firstVCView.frame = CGRectOffset(firstVCView.frame, -screenWidth, 0.0)
        secondVCView.frame = CGRectOffset(secondVCView.frame, -screenWidth, 0.0)

        }) { (Finished) -> Void in

                    self.sourceViewController.presentViewController(self.destinationViewController as! UIViewController,
                        animated: false,
                        completion: nil)
            }
    }
}

Delayed swipe 延迟滑动

Modify your showSecondViewController method to accept a UISwipeGestureRecognizer parameter and inspect it's state property. 修改showSecondViewController方法以接受UISwipeGestureRecognizer参数并检查其state属性。 If you want to trigger the segue when the user has lifted their finger trigger the segue when the state is UIGestureRecognizerStateEnded . 如果你想在用户举起手指时触发segue,当stateUIGestureRecognizerStateEnded时触发segue。

If you want more fine grained control such as when they have swiped a certain distance you will need to check the state for UIGestureRecognizerStateChanged and monitor the location of the touch. 如果您想要更精细的控制,例如当他们滑动一定距离时,您需要检查UIGestureRecognizerStateChanged的状态并监控触摸的位置。 I'll leave that as an exercise for the reader as there are plenty of examples a quick Google search away :) 我会把它留作读者练习,因为有很多例子可以快速搜索到谷歌:)

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

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