此代码可以正常使用向下动画进行自定义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)
}
}
}
答案 0 :(得分:6)
动画方向
您只需更改初始帧和目标帧即可。目前secondVCView
的初始帧的X,Y原点0.0
,screenHeight
,意味着它位于屏幕底部的正下方。如果您希望它从右侧显示,则需要将其更改为screenWidth
,0.0
。
然后firstVCView
的目标框架会发生变化,因此原点为-screenWidth
,0.0
,secondVCView
的目标框架会发生变化,原点为{{1} },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
为state
时,如果您想要在用户抬起手指时触发segue,请触发segue。
如果您想要更精细的控制,例如当他们滑动一定距离时,您需要检查UIGestureRecognizerStateEnded
的状态并监控触摸的位置。我会把它留作读者的练习,因为有很多例子可以快速搜索谷歌:)