[英]How to make this segue animation exit to right or left side? - Swift
此代码可以正常使用向下动画进行自定义segue过渡。 如何使动画看起来像是向左或向右转换?
另外 - 我正在使用UISwipeGestureRecognizer来触发segue。 它工作正常,除了我做最小的滑动时发生segue。 如果用户已将手指抬起或至少进行了较大的滑动,我将如何允许不发生segue。 我需要使用scrollView吗?
下面是我的UISwipeGestureRecognizer的代码。
override func viewDidLoad() {
var swipeGestureRecognizer: UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: "showSecondViewController")
swipeGestureRecognizer.direction = UISwipeGestureRecognizerDirection.Up
self.view.addGestureRecognizer(swipeGestureRecognizer)
}
下面的代码是我的动画代码
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)
}
}
}
动画方向
您只需更改初始帧和目标帧即可。 目前第二个secondVCView
的初始帧的X,Y原点为0.0
, screenHeight
,意味着它位于屏幕底部正下方。 如果您希望它从右侧出现,则需要将其更改为screenWidth
, 0.0
。
然后目的地帧firstVCView
将改变所以原点是-screenWidth
, 0.0
和目的地帧secondVCView
将改变所以原点是0.0
, 0.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)
}
}
}
延迟滑动
修改showSecondViewController
方法以接受UISwipeGestureRecognizer
参数并检查其state
属性。 如果你想在用户举起手指时触发segue,当state
为UIGestureRecognizerStateEnded
时触发segue。
如果您想要更精细的控制,例如当他们滑动一定距离时,您需要检查UIGestureRecognizerStateChanged
的状态并监控触摸的位置。 我会把它留作读者练习,因为有很多例子可以快速搜索到谷歌:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.