繁体   English   中英

UITapGestureRecognizer不会启动UIView子类

[英]UITapGestureRecognizer doesn't fire up on UIView Subclass

我已经创建了UIView的自定义子类,因为我需要覆盖drawRect方法。 我尝试添加一个UITapGestureRecognizer,但这对我不起作用。 这里,我的ViewController的代码:

MyCustomView *customView = [[MyCustomView alloc] initWithFrame:CGRectFrame(0, 30, 30, 30)];
[customView setUserInteractionEnabled:YES];

UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doSomething)];
[customView addGestureRecognizer:tapGestureRecognizer];

我不明白为什么当我触摸我的UIView子类对象时,手势识别器不会启动。

非常感谢提前!

固定了!

我的观点是在UIImageView上,而UIImageView没有将userInteractionEnabled设置为YES。

固定了!

我的观点是在UIImageView上,而UIImageView没有将userInteractionEnabled设置为YES。

确保你在某处显示它( addSubview )。

还要确保userInteractionEnabled设置为true。 如果这是真的,请发布创建并挂钩手势识别器的代码。

在要添加的子视图上将UserInteractionEnable检查设置为true,并在选择器方法中,将对象与其一起传递,即

UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doSomething:)];

并将选择器方法设为

-(void) doSomething:(UITapGestureRecognizer *)gesture;

如果您还有问题,请告诉我。 谢谢!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM