我正在我的视图控制器(UIViewController 的自定义子类)中处理几个 UI 组件的触摸.它有方法 touchesBegan:withEvent:、touchesMoved:withEvent: 和 touchesEnded:withEvent:.它工作正常.然后我添加了一个滚动视图(UIScrollView)作为层次结构中的顶视图.
I am handling touches for a couple of my UI components in my view controller (custom subclass of UIViewController). It has methods touchesBegan:withEvent:, touchesMoved:withEvent:, and touchesEnded:withEvent:. It was working fine. Then I added a scroll view (UIScrollView) as the top view in the hierarchy.
现在我在视图控制器上的触摸处理程序不起作用.他们不会被召唤.有趣的是,我在滚动视图中有各种其他的 UI 组件可以工作.有些是按钮,有些是自定义视图,它们定义了自己的 touchesBegan:withEvent: 等.唯一不起作用的是视图控制器上的触摸处理程序.
Now my touch handlers on the view controller don't work. They don't get called. The interesting thing is, I have various other UI components within the scroll view that do work. Some are buttons, some are custom views that define their own touchesBegan:withEvent:, etc. The only thing that doesn't work is the touch handlers on the view controller.
我想这可能是因为滚动视图出于自己的目的拦截了这些触摸,但我将 UIScrollView 子类化,只是为了看看我是否可以让它工作,我总是从 返回 和 YES>touchesShouldBegin:withEvent:inContentView:NO 始终来自 touchesShouldCancelInContentView:.还是不行.
I thought maybe it's because the scroll view is intercepting those touches for its own purposes, but I subclassed UIScrollView and just to see if I could get it to work I am returning YES always from touchesShouldBegin:withEvent:inContentView: and NO always from touchesShouldCancelInContentView:. Still doesn't work.
如果它有所不同,我的视图控制器位于标签栏控制器中,但我认为它不相关.
If it makes a difference my view controller is within a tab bar controller, but I don't think it's relevant.
有没有人遇到过这个问题并有现成的解决方案?我的猜测是滚动视图猴子在响应者链上.我可以猴子回来吗?我想如果我想不出其他任何东西,我会将滚动视图下的顶级视图设为自定义视图并将消息转发到视图控制器,但看起来很笨拙.
Has anyone had this problem and have a ready solution? My guess is the scroll view monkeys up the responder chain. Can I monkey it back? I guess if I can't figure anything else out I'll make the top level view under my scroll view be a custom view and forward the messages on to the view controller, but seems kludgy.
创建 UIScrollView 类的子类并覆盖 touchesBegan: 和其他触摸方法如下:
create a subclass of UIScrollView class and override the touchesBegan: and other touch methods as follows:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
// If not dragging, send event to next responder
if (!self.dragging){
[self.nextResponder touchesBegan: touches withEvent:event];
}
else{
[super touchesBegan: touches withEvent: event];
}
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
// If not dragging, send event to next responder
if (!self.dragging){
[self.nextResponder touchesMoved: touches withEvent:event];
}
else{
[super touchesMoved: touches withEvent: event];
}
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
// If not dragging, send event to next responder
if (!self.dragging){
[self.nextResponder touchesEnded: touches withEvent:event];
}
else{
[super touchesEnded: touches withEvent: event];
}
}
这篇关于UIScrollView 防止视图控制器上的 touchesBegan、touchesMoved、touchesEnded的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
iOS - 使用故事板和自动布局使 UIScrollView 居中iOS - Using storyboard and autolayout to center the UIScrollView(iOS - 使用故事板和自动布局使 UIScrollView 居中)
从 imageview 点击手势获取索引或标签值get index or tag value from imageview tap gesture(从 imageview 点击手势获取索引或标签值)
无论内容大小如何,UIScrollView 都不会滚动UIScrollView not scrolling regardless of large contentSize(无论内容大小如何,UIScrollView 都不会滚动)
清除分页 UIScrollView 中的自动旋转转换Clean autorotation transitions in a paging UIScrollView(清除分页 UIScrollView 中的自动旋转转换)
UIScrollView 使用自动布局缩放UIScrollView zooming with Auto Layout(UIScrollView 使用自动布局缩放)
如何从 UIView/UIScrollView 创建图像How to create an image from a UIView / UIScrollView(如何从 UIView/UIScrollView 创建图像)