触摸开始无法检测到所触摸的内容
我正在使用 NSTimer 构建一个旋转横幅来跟踪当前图像,该图像是由 5 个不同图像制作而成的动画。我设置了一个 TouchBegan 来在有人点击横幅时继续处理横幅上的触摸事件。我的概念验证有效,但将其转移到另一个项目中时,它就崩溃了。
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [[event allTouches] anyObject];
if ([touch view] == myImageView){
[self getImage];
NSLog(@"%@", currentImage);
}
}
现在,当我在项目中添加断点时,它可以很好地捕捉触摸,但是当它到达 if ([触摸视图] == myImageView) 它没有检测到图像视图被触摸。
I'm building a rotating banner using a NSTimer to keep track of the current image with the image being animated from 5 different images. I have a touchesBegan set up to keep handle the touch event on the banner if someone clicks it. My proof-of-concept works, but moving it into another project, it breaks.
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [[event allTouches] anyObject];
if ([touch view] == myImageView){
[self getImage];
NSLog(@"%@", currentImage);
}
}
Now when I put break points into my project, it grabs the touch just fine, but when it gets to the
if ([touch view] == myImageView)
it doesn't detect that the image view is being touched.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不确定什么会导致这种情况,但您尝试过使用 UIGestureRecognizer 吗?尝试类似下面的代码,看看该方法是否被调用。
Not sure what would cause that but have you tried using a UIGestureRecognizer? Try something like the code below and see if the method gets called.
首先,您必须在 viewDidLoad 方法中将 userInteractionEnabled 设置为 YES,如下所示:
请注意,对于 myImageView,通过 Identity Inspector 检查 User Interaction Enabled 对我来说不起作用。
然后更改
为
,touchesBegan 方法如下所示:
First of all you have to set userInteractionEnabled to YES in your viewDidLoad method like below:
Note that for the myImageView, checking User Interaction Enabled via Identity Inspector didn't work for me.
Then change
to
so that the touchesBegan method looks like below: