UserInteraction仅启用子视图 [英] UserInteraction enable for subview only

查看:155
本文介绍了UserInteraction仅启用子视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个视图和视图.UserInteractionenabled = no并且在视图中添加了一个按钮。我只需要点击按钮。是否可以仅为按钮启用交互。

I have a view and view.UserInteractionenabled = no and a button is added to the view. i need to click the button only . is it possible to enable interaction for button only.

推荐答案

视图无法接收触摸,除非 userInteractionEnabled 用于视图及其所有超级视图,直到 UIWindow 对象。

A view cannot receive touches unless userInteractionEnabled is YES for the view and all of its superviews up to the UIWindow object.

您可以创建 UIView 的子类来包含该按钮,并通过覆盖<$ c使其忽略按钮外的触摸$ C>则hitTest:withEvent:方法。示例:

You can make a subclass of UIView to contain the button, and make it ignore touches outside the button by overriding hitTest:withEvent:. Example:

@interface MyView : UIView

@property (nonatomic, strong) IBOutlet UIButton *button;

@end


@implementation MyView

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    UIView *subview = [super hitTest:point withEvent:event];
    return subview == self.button ? subview : nil;
}

@end

这篇关于UserInteraction仅启用子视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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