将手势识别器添加到屏幕外的元素
问题描述:
我有一个视图,它的宽度是我的设备屏幕宽度的两倍,并具有向左或向右移动视图的滑动手势识别器。但是我已经认识到,如果在原本不在屏幕上的部分视图上执行轻扫,视图将不会执行手势操作。将手势识别器添加到屏幕外的元素
// View twice as wide as the device screen
let masterView = UILabel(frame: CGRect(x: 0, y: 0, width: self.view.frame.width * 2, height: 100))
let swipeLeftGesture = UISwipeGestureRecognizer(target: self, action: #selector(ThisViewController.swipeLeft))
let swipeRightGesture = UISwipeGestureRecognizer(target: self, action: #selector(ThisViewController.swipeRight))
swipeLeftGesture.direction = UISwipeGestureRecognizerDirection.left
swipeRightGesture.direction = UISwipeGestureRecognizerDirection.right
masterView.addGestureRecognizer(swipeLeftGesture)
masterView.addGestureRecognizer(swipeRightGesture)
self.addSubview(masterView)
func swipeLeft() {
// Moves masterView to the left equal to that of the width of the screen (or half of masterView's total width)
let moveLeftFrame = CGRect(x: (masterView.frame.width/2) * -1, y: masterView.frame.minY, width: masterView.frame.width, height: masterView.frame.height)
UIView.animateKeyframes(withDuration: 0.5, delay: 0, options: .calculationModeCubic, animations: {
UIView.addKeyframe(withRelativeStartTime: 0.0, relativeDuration: 0.4, animations: {
masterView.frame = moveLeftFrame
})
}, completion: { (true) in
})
}
func swipeRight() {
// Moves masterView back to original location
let moveRightFrame = CGRect(x: 0, y: masterView.frame.minY, width: masterView.frame.width, height: masterView.frame.height)
UIView.animateKeyframes(withDuration: 0.5, delay: 0, options: .calculationModeCubic, animations: {
UIView.addKeyframe(withRelativeStartTime: 0.0, relativeDuration: 0.4, animations: {
masterView.frame = moveRightFrame
})
}, completion: { (true) in
})
}
于是往左滑动来查看视图(其上装载关屏)的另一半后,我无法刷卡回到正确的。不能在已加载屏幕的位置执行手势操作?
编辑:所以在修补了一下之后,我发现手势无法识别在加载离屏的视图的任何位置。因此,如果我移动视图以查看从屏幕外开始的50个像素,则无法在该50个像素上识别手势。
答
我真的不明白你的答案,但希望这可以帮助
因此,我认为你的问题是
- 你声明你刷卡功能是错误的方式。相反FUNC swipeLeft的()将其更改为FUNC swipeLeft(发件人:UISwipeGestureRecognizer)
或
- 问题可能是你在你的声明框架变量的方式。您可以在我的代码中查看更改后的一个。
.code。
func swipeLeft(sender: UISwipeGestureRecognizer) {
// Moves masterView to the left equal to that of the width of the screen (or half of masterView's total width)
let moveLeftFrame = CGRect(x: -view.frame.width, y: 0, width: masterView.frame.width, height: masterView.frame.height)
UIView.animateKeyframes(withDuration: 0.5, delay: 0, options: .calculationModeCubic, animations: {
UIView.addKeyframe(withRelativeStartTime: 0.0, relativeDuration: 0.4, animations: {
masterView.frame = moveLeftFrame
})
}, completion: { (true) in
})
}
func swipeRight(sender: UISwipeGestureRecognizer) {
// Moves masterView back to original location
let moveRightFrame = CGRect(x: 0, y: 0, width: masterView.frame.width, height: masterView.frame.height)
UIView.animateKeyframes(withDuration: 0.5, delay: 0, options: .calculationModeCubic, animations: {
UIView.addKeyframe(withRelativeStartTime: 0.0, relativeDuration: 0.4, animations: {
masterView.frame = moveRightFrame
})
}, completion: { (true) in
})
}
视图移动工作正常。只是在最初不可见的视图上拖动任何地方都无法识别手势 – Tamarisk
如何拖动不可见的一面。我认为你的意思是你想拖动屏幕的其余部分而不是图像。那么我建议你的手势添加到你的'视图'而不是'masterView' –