触摸开始无法检测到所触摸的内容

发布于 2024-12-11 00:06:07 字数 447 浏览 1 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

残花月 2024-12-18 00:06:07

不确定什么会导致这种情况,但您尝试过使用 UIGestureRecognizer 吗?尝试类似下面的代码,看看该方法是否被调用。

  //Add Gesture Recognizer
  UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self    action:@selector(imageSelected)];
  tapped.numberOfTapsRequired = 1;
  [theImageView addGestureRecognizer:tapped];

  //Memory Cleanup
  [tapped release];

 -(void)imageSelected
  {
    NSLog(@"Selected an Image");
  }

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.

  //Add Gesture Recognizer
  UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self    action:@selector(imageSelected)];
  tapped.numberOfTapsRequired = 1;
  [theImageView addGestureRecognizer:tapped];

  //Memory Cleanup
  [tapped release];

 -(void)imageSelected
  {
    NSLog(@"Selected an Image");
  }
天生の放荡 2024-12-18 00:06:07

首先,您必须在 viewDidLoad 方法中将 userInteractionEnabled 设置为 YES,如下所示:

[myImageView setUserInteractionEnabled:YES];

请注意,对于 myImageView,通过 Identity Inspector 检查 User Interaction Enabled 对我来说不起作用。

然后更改

UITouch *touch = [[event allTouches] anyObject];

UITouch *touch = [touches anyObject];

,touchesBegan 方法如下所示:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    if ([touch view] == myImageView) {
       // place any code here when myImageView is touched
    }
 }

First of all you have to set userInteractionEnabled to YES in your viewDidLoad method like below:

[myImageView setUserInteractionEnabled:YES];

Note that for the myImageView, checking User Interaction Enabled via Identity Inspector didn't work for me.

Then change

UITouch *touch = [[event allTouches] anyObject];

to

UITouch *touch = [touches anyObject];

so that the touchesBegan method looks like below:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    if ([touch view] == myImageView) {
       // place any code here when myImageView is touched
    }
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文