问题描述
我们在iOS 12中开发了一个应用程序,该应用程序确实运行良好.现在,在iOS 13中,UIPanGestureRecognizer不再起作用.
We have developed an app in iOS 12 which worked really fine. Now in iOS 13 UIPanGestureRecognizer is not working any more.
我正在寻找解决方案,但没有找到任何东西.
I've search for solutions but didn't find anything.
@IBAction func handlePan(recognizer:UIPanGestureRecognizer) {
let translation = recognizer.translation(in: self.view)
if let view = recognizer.view {
let center = view.frame.origin.y + view.bounds.height / 2
if(center <= SliderView.bounds.height && center >= SliderView.bounds.minY) {
view.center = CGPoint(x:view.center.x, y:view.center.y + translation.y)
}
if(center > SliderView.bounds.height) {
view.center = CGPoint(x: view.center.x, y: view.center.y - 1)
}
if(center < SliderView.bounds.minY) {
view.center = CGPoint(x: view.center.x, y: view.center.y + 1)
}
lowerSliderView.frame = CGRect(x: 0, y: center, width: SliderView.bounds.width, height: SliderView.bounds.height - center)
slider = 1 - Float(center / SliderView.bounds.height)
slider = min(slider, 1.0)
slider = max(slider, 0.0)
}
recognizer.setTranslation(CGPoint.zero, in: self.view)
}
我希望滑块可以在该应用上正常工作.
I expect that the slider will work on the app.
推荐答案
我在iOS13上的手势识别器也遇到了类似的问题.(可以正常使用12)
I had similar issues with my gesture recognizer on iOS13. (Worked fine on 12)
我的问题是:我在具有手势识别器的视图上设置了.center = someValue,但该视图也具有约束.当您在视图上有约束时,iOS13似乎不喜欢它,并且还手动设置了它的框架.因此,我将代码完全转换为仅在手势识别器的处理程序方法中设置框架.iOS13似乎在防止您手动设置.center或.frame方面变得更加严格,如果您对此视图也有约束并且正在调用layoutIfNeeded()导致布局通过.我对此不太确定,但目前,我已使用手动框架设置来备份并运行.
My problem was: I was setting .center = someValue, on my view which had the gesture recognizer on it, but that view also had Constraints on it. iOS13 doesn't seem to like it when you have constraints on a view, and are also setting it's frame manually. So i converted my code completely to just set the frame inside the gesture recognizer's handler method. iOS13 seems to have gotten more strict with preventing you from setting .center, or .frame manually if you also have constraints on that view and are calling layoutIfNeeded() causing a layout pass. I'm not sure about this, but for now, i'm back up and running using manual frame setting.
如果您的手势识别器事件未触发,请尝试实现以下方法并检查其中的值,以查看是否存在其他手势识别器重叠并争夺触摸手势.为您的手势识别器返回TRUE,并抑制其他手势,或者为所有其他手势返回true.您需要先设置委托.
If your gesture recognizer event is not firing, please try to implement the following method and inspect the values inside to see if there are other gesture recognizers overlaid and competing for the touch gesture. Return TRUE for your gesture recognizer, and suppress others, or just return true for all of them. You need to set the delegate first.
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer,
shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
if (gestureRecognizer is UIPanGestureRecognizer || gestureRecognizer is UIRotationGestureRecognizer) {
print("Should recognize")
return true
} else {
print("Should NOT recognize")
return false
}
}
这是我的设置.当我从视图上删除所有带有识别器的约束后,现在可以正常工作了.约束正在撤消"笔势识别器方法中的转换,仅导致视图移动+1或-1,然后将其恢复原位.
This is the setup of mine. works fine now, after I removed all the constraints from my view which had the recognizer on it. Constraints were "undoing" the translations which I had in the gesture recognizer method, only resulting in +1 or -1 movement of the view, and then it snapped back in place.
let recStart = UIPanGestureRecognizer(target: self, action: #selector(handleStartPan))
recStart.delegate = self
self.startView.addGestureRecognizer(recStart)
handleStartPan:
And handleStartPan:
@objc final func handleStartPan(_ gestureRecognizer: UIPanGestureRecognizer) {
if gestureRecognizer.state == .began || gestureRecognizer.state == .changed {
let translation = gestureRecognizer.translation(in: containerOfMyView)
// do stuff with translation.x or .y
...
这篇关于UIPanGestureRecognizer在iOS 13中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!