UIView触摸处理行为随Xcode 4.2而改变? [英] UIView touch handling behavior changed with Xcode 4.2?

查看:116
本文介绍了UIView触摸处理行为随Xcode 4.2而改变?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我几天前将iPad升级到5.0,同时将Xcode升级到4.2,这样我就可以继续测试我的应用了。现在我在使用以前版本的Xcode的几个应用程序中遇到触摸代码问题。

I upgraded my iPad to 5.0 a couple days ago, and upgraded Xcode to 4.2 at the same time so I could continue to test my apps. Now I am having problems with touch code in several apps that worked with previous versions of Xcode.

我通过覆盖将UIImageView子类化为添加一些拖动功能 - (void)TouchesBegan - (void)TouchesMoved 。我在子类中覆盖 - (void)TouchesEnded ,但在视图控制器中处理包含图像视图的视图。

I subclassed UIImageView to add some dragging features by overriding -(void)TouchesBegan and -(void)TouchesMoved. I did not override -(void)TouchesEnded in the subclass, but handled that in the view controller for the view that contains the image view.

我将子类 UIImageView 拉入一个新项目进行测试,并将问题缩小到 UIView (Xcode创建的模板)似乎没有将触摸事件转发给视图控制器(也是由Xcode创建的)。

I pulled the subclassed UIImageView into a new project for testing, and have narrowed down the issue to the fact that the parent UIView (the template created by Xcode) does not seem to be forwarding touch events to the view controller (also created by Xcode).

如果我将它添加到我的子类:

If I add this to my subclass:

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"touches ended event in ImageToDrag");
    [self.nextResponder touchesEnded:touches withEvent:event];
}

这是我父视图的视图控制器:

and this to my parent view's view controller:

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"touches ended event in ViewController");
}

当我放开图像时,我拖动屏幕,我得到触及ImageToDrag中的已结束事件,但不是来自视图控制器的日志。

when I let go of the image I am dragging around the screen, I get the "touches ended event in ImageToDrag", but not the log from the view controller.

但是,如果我故意在子类视图中跳过视图:

However, if I intentionally skip over the view by doing this in the subclassed view:

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"touches ended event in ImageToDrag");
    [[self.nextResponder nextResponder] touchesEnded:touches withEvent:event];
}

然后我得到两个日志条目。

then I get both log entries.

我能想出的唯一解释是,出于某种原因, UIView 正在消耗 touchesEnded 事件,而不是将其传递给视图控制器。

The only explanation I can come up with is that for some reason, UIView is consuming the touchesEnded event and not passing it to the view controller.

我已经确认 exclusiveTouch 设置为 ,并且 userInteractionEnabled 在父视图和子类 UIImageView YES c $ c>。

I have verified that exclusiveTouch is set to NO, and userInteractionEnabled is set to YES on the parent view and the subclassed UIImageView.

我还测试了iOS 5.0和iOS 4.2的编译,并将测试应用程序部署到iOS 5 iPad和iOS 4.3.1 iPad。

I have also tested compiling for iOS 5.0 and iOS 4.2, and deploying the test app to both an iOS 5 iPad and iOS 4.3.1 iPad.

我能够将触摸事件传递给 viewController 的唯一方法是跳过视图和在子类中使用double nextResponder 。虽然这个方法起作用,但对我来说这似乎是一个黑客攻击,我确信它会在以后再次给我发牢骚。

The only way I have been able to get the touch event to the viewController is by skipping over the view and using the double nextResponder in the subclass. Although that method functions, it seems like a hack to me and I'm sure it will come back to bite me later.

还有其他人看到过这种行为吗?有什么方法让我找出 UIView 对我的触摸事件做了什么?

Has anybody else seen this behavior? Is there any way for me to find out what the UIView is doing with my touch events?

谢谢,
Dan

Thanks, Dan

推荐答案

我是在过去的几个小时里一直试图追查类似的问题。终于设法在这篇文章的帮助下解决了这个问题

I've been trying to track down the a similar issue for the last few hours. Finally managed to solve it with the help of this post


实际上看起来我只是设法解决它,使用来自
的提示 https://devforums.apple.com/message/519690#519690

之前,我只是
将touchesEnded事件转发给了自己.nextResponder。当我添加
touchesBegan,Moved,取消处理程序与类似的实现
作为touchesEnded时,事件似乎冒泡到根视图
控制器。

所以我猜在iOS5上,当他们没有看到相关的touchesBegan时,视图会丢弃touchesEnded事件

Actually it looks like I just managed to solve it, using the hint from https://devforums.apple.com/message/519690#519690
Earlier, I just forwarded the touchesEnded event to self.nextResponder. When I added touchesBegan, Moved, Cancelled handlers with similar implementations as the touchesEnded, the event seems to bubble up to the root view controller.
So I guess on iOS5, views discard touchesEnded events when they have not seen the relevant touchesBegan.

我不需要添加移动/等等,我刚刚在TouchesBegan转发,然后TouchesEnded再次开始工作!

I didn't need to add Moved/etc., I just forwarded on TouchesBegan, and then TouchesEnded start working again!

这篇关于UIView触摸处理行为随Xcode 4.2而改变?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆