如何防止触摸事件传递给 UIView 的超级视图?

how to prevent a touch event passed to a UIView#39;s superview?(如何防止触摸事件传递给 UIView 的超级视图?)

本文介绍了如何防止触摸事件传递给 UIView 的超级视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个程序,其中包含可以拖动以在屏幕上移动的图片.我也使用了 UIScrollView,因为空间比窗口大.我将 UIView 子类化为图片视图,以在此类中显示图片并处理触摸.图片视图作为子视图添加到滚动视图.但现在我有一个问题:当我拖动图片时,scrollView 与图片一起移动.我想知道是否有办法消耗图片类中的触摸以防止它们传递给超级视图.

I've made a program which contains pictures that can be dragged to move on the screen. also I used UIScrollView since the space is larger than the window. I subclassed UIView as a pictures view to display the picture and handle touches in this class. picture views are added as subviews to the scrollView. but now I have a problem: when I drag the picture, the scrollView moves together with the picture. I'm wondering if there is a way to consume the touches in the picture class to prevent them from passing to superview.

推荐答案

你应该把你的代码放在 UIView 的 "touchesMoved:withEvent:" 方法中.如果你的 View 会自己处理这个事件,那么它不应该冒泡到 superview

You should to put your code in "touchesMoved:withEvent:" method of your UIView. If your View will handle this event by yourself, then it should not bubble to superview

这篇关于如何防止触摸事件传递给 UIView 的超级视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何防止触摸事件传递给 UIView 的超级视图?

基础教程推荐