我希望拦截父 UIView 上的所有触摸事件并显示用户触摸的位置,但仍然将这些触摸事件冒泡到 subview ,即 UIButtons、UITableViewCell 等,以便这些 subview 通过默认方式进行交互。
我使用基本委托(delegate)方法在 UIView 上以交互方式显示触摸事件没有问题:
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesBegan:touches withEvent:event];
}
-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesEnded:touches withEvent:event];
}
此时我的问题是,当 UIView 包含其他交互式 subview (例如 UIButtons)时,将这些事件冒泡到下一个响应者。 TouchsBegan:点击按钮时永远不会在父 View 上触发,因此我无法在点击按钮时向用户显示“漂亮”的内容。
除了为 UIButton、UITableViewCell 创建类别之外,还有更好的方法吗?谢谢
这是我希望在用户触摸屏幕时执行的操作的示例...(请注意用户触摸按钮时的白色圆圈): https://m1.behance.net/rendition/modules/154366385/disp/9532a948c71e28ffda2219600c05ffd9.gif
最佳答案
您可以通过创建 UIView 的子类来做到这一点。 View 应该位于 View 层次结构的顶部,并且其大小应该充满屏幕。
所有触摸事件都将发送到此 View 。如果在pointInside:widthEvent:方法中返回NO,则触摸事件将被发送到下层对象。
@implementation MyView
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
// show bubble here
return NO; // send touch events to lower layer objects.
}
关于objective-c - 交互式触摸显示屏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28893859/