[英]Pass UILongPressGestureRecognizer between UIViewControllers
I've looked around on SO and other places online and haven't been able to find a solution yet. 我在网上等其他地方闲逛,还没有找到解决方案。 I have a UILongPressGestureRecognizer on one view controller which performs an animation on a UIView then presents another view controller (if the user does not move their finger during the animation).
我在一个视图控制器上有一个UILongPressGestureRecognizer,它在UIView上执行动画,然后显示另一个视图控制器(如果用户在动画过程中没有移动手指)。 I would like to dismiss the second view controller when the user lifts their finger, but I am having trouble.
当用户抬起手指时,我想关闭第二个视图控制器,但是我遇到了麻烦。
My current approach is adding another UILongPressGestureRecognizer to the second view controller and dismissing the view controller when its state is .ended; 我当前的方法是向第二个视图控制器中添加另一个UILongPressGestureRecognizer,并在结束其状态时关闭视图控制器。 however I don't know how to tie the two gesture recognizers together so that I can start the touch in one view controller and end in another.
但是我不知道如何将两个手势识别器绑定在一起,这样我才能在一个视图控制器中开始触摸,而在另一个视图控制器中结束。
I have attached the relevant code below 我已附上以下相关代码
In the first View Controller 在第一个View Controller中
@objc private func pinAnimation(sender: UILongPressGestureRecognizer) {
if let headerView = projectView.projectCollectionView.supplementaryView(forElementKind: UICollectionElementKindSectionHeader, at: IndexPath(row: 0, section: 0)) as? ProjectHeaderView {
switch sender.state {
case .began:
let frame = headerView.pinButton.frame
UIView.setAnimationCurve(.linear)
UIView.animate(withDuration: 1, animations: {
headerView.pinProgressView.frame = frame
}, completion: { (_) in
if (headerView.pinProgressView.frame == headerView.pinButton.frame) {
let notification = UINotificationFeedbackGenerator()
notification.notificationOccurred(.success)
let homeVC = HomeViewController()
self.present(homeVC, animated: true, completion: {
homeVC.longPress(sender)
let height = headerView.pinButton.frame.height
let minX = headerView.pinButton.frame.minX
let minY = headerView.pinButton.frame.minY
headerView.pinProgressView.frame = CGRect(x: minX, y: minY, width: 0, height: height)
})
}
})
case .ended:
//cancel current animation
let height = headerView.pinProgressView.frame.height
let minX = headerView.pinProgressView.frame.minX
let minY = headerView.pinProgressView.frame.minY
UIView.setAnimationCurve(.linear)
UIView.animate(withDuration: 0.5) {
headerView.pinProgressView.frame = CGRect(x: minX, y: minY, width: 0, height: height)
}
default:
break
}
}
}
In the second View Controller 在第二个View Controller中
override func viewDidLoad() {
super.viewDidLoad()
let longPressRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(longPress(_:)))
view.addGestureRecognizer(longPressRecognizer)
}
@objc func longPress(_ sender: UILongPressGestureRecognizer) {
switch sender.state {
case .ended:
self.dismiss(animated: true, completion: nil)
default:
break
}
}
Any help / guidance will be much appreciated! 任何帮助/指导将不胜感激!
I would recommend you using delegate pattern: 我建议您使用委托模式:
Create delegate for your first ViewController 为您的第一个ViewController创建委托
protocol FirstViewControllerDelegate {
func longPressEnded()
}
then add delegate
variable to your first ViewController 然后将
delegate
变量添加到您的第一个ViewController
class FirstViewController {
var delegate: FirstViewControllerDelegate?
...
}
next call method on delegate when long press ended 长按结束时代表上的下一个调用方法
@objc private func pinAnimation(sender: UILongPressGestureRecognizer) {
...
switch sender.state {
case .began:
...
case .ended:
...
delegate?.longPressEnded()
default:
break
}
}
after that when you're presenting second ViewController, set first ViewController's delegate
as homeVC
之后,当您展示第二个ViewController时,将第一个ViewController的
delegate
设置为homeVC
let homeVC = HomeViewController()
self.delegate = homeVC
then implement this delegate to second ViewController and define what should happen when long press ended 然后将该委托实现到第二个ViewController并定义长按结束时应该发生的情况
class HomeViewController: UIViewController, FirstViewControllerDelegate {
...
func longPressEnded() {
dismiss(animated: true, completion: nil)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.