xcode UITapGestureRecognizer 不适用于 iOS9
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31049033/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UITapGestureRecognizer not working on iOS9
提问by Liron
I have an application which uses UITapGestureRecognizers
which seem to not work in iOS9 Beta 2.
我有一个使用的应用程序UITapGestureRecognizers
似乎在 iOS9 Beta 2 中不起作用。
They are successfully calling
他们正在成功调用
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
NSLog(@"shouldReceiveTouch");
return YES;
}
but it doesn't hit any of the other UITapGesture delegate methods.
但它不会影响任何其他 UITapGesture 委托方法。
When I run the same application (from Xcode 7) on a device running on iOS 8, it works as expected.
当我在运行 iOS 8 的设备上运行相同的应用程序(来自 Xcode 7)时,它按预期工作。
Has anyone else hit this?
有其他人打过这个吗?
Here is how I initialise the UITapGestureRecognizer.
这是我初始化 UITapGestureRecognizer 的方法。
Edit
If I create the UITapGestureRecognizer in code instead of in the ViewController xib it works fine, so there's something up with the xib parsing in iOS9 or something.
编辑
如果我在代码中而不是在 ViewController xib 中创建 UITapGestureRecognizer 它工作正常,所以在 iOS9 或其他东西中的 xib 解析有问题。
_tapGesture2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onMainViewTap:)];
_tapGesture2.numberOfTapsRequired = 1;
_tapGesture2.delegate = self;
[self.view addGestureRecognizer:_tapGesture2];
Edit2
If I deleted the GestureRecognizer in the XIB and added it back in again using XCode 7, it also worked.
When I did this, it added <pressTypeMask key="allowedPressTypes"/>
into the xib under the UITapGestureRecognizer.
Edit2
如果我删除了 XIB 中的 GestureRecognizer 并使用 XCode 7 重新添加它,它也可以工作。当我这样做时,它添加<pressTypeMask key="allowedPressTypes"/>
到 UITapGestureRecognizer 下的 xib 中。
采纳答案by Liron
It seems that iOS9 looks for a <pressTypeMask key="allowedPressTypes"/>
line in the xml of the gesture recognizer and the tap recognizer doesn't work without it.
To fix you can either build your recognizers in code, add that line into the xml or delete and re-add the gesture recognizer in Xcode 7, which will properly add the pressTypeMask
into the xml.
似乎 iOS9<pressTypeMask key="allowedPressTypes"/>
在手势识别器的 xml 中寻找一行,没有它,点击识别器就无法工作。要修复,您可以在代码中构建识别器,将该行添加到 xml 中,或者在 Xcode 7 中删除并重新添加手势识别器,这将正确地添加pressTypeMask
到 xml 中。
I wonder if apple will fix iOS9 to work without that line in the XML, but for now, this is an easy fix.
我想知道苹果是否会修复 iOS9 以使其在 XML 中没有该行的情况下工作,但就目前而言,这是一个简单的修复。
回答by qix
I ran into this problem as well, specifically when trying to get both my buttons and views w/ tap gestures within a table cell to respond to a click whose layout was defined in a xib. Worked fine on iOS 8 & 10, but not 9. The actual problem was that I added those to a custom uiview and assigned the view's class to my UITableViewCell subclass, rather than use a Table View Cell
object within IB. Thus I did not add my subviews to the contentView
that the IB object gives you, causing me the problem. The fix was to redo the xib to use the Table View Cell as my root, so I could attach my subviews to the Content View.
我也遇到了这个问题,特别是当我试图在表格单元格中使用点击手势获取我的按钮和视图以响应其布局在 xib 中定义的点击时。在 iOS 8 和 10 上运行良好,但在 9 上运行良好。实际问题是我将它们添加到自定义 uiview 并将视图的类分配给我的 UITableViewCell 子类,而不是Table View Cell
在 IB 中使用对象。因此,我没有将我的子视图添加到contentView
IB 对象给你的,导致我的问题。修复方法是重做 xib 以使用表视图单元作为我的根,这样我就可以将我的子视图附加到内容视图。
回答by kaushal
Implementing all optional delegate of UIGestureRecognizer will work.
实现 UIGestureRecognizer 的所有可选委托都将起作用。
回答by Doro
I checked it via Xcode-7 beta 2
我通过 Xcode-7 beta 2 检查了它
I've created vc with gesturerecognizer and create outlets for property and action. in viewDidload setted delegate, like this:
我已经用手势识别器创建了 vc,并为属性和动作创建了出口。在 viewDidload 设置的委托中,像这样:
All methods, that was called during debug - i've marked //called
在调试期间调用的所有方法 - 我已标记为 //called
I received events to the next methods:
我收到了下一个方法的事件:
func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldReceiveTouch touch: UITouch) -> Bool {
print("worked")
// called
return true
}
func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer) -> Bool
{
//called
print("worked")
return true
}
func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRequireFailureOfGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool
{
print("worked")
//called
return true
}
Try recreate project as i mentioned and check all your outlets and delegate
尝试按照我提到的方式重新创建项目并检查您的所有网点和委托
Hope this helps
希望这可以帮助