【问题标题】:How do you tell what object is being touched in touchesBegan?你如何知道在 touchesBegan 中被触摸的对象是什么?
【发布时间】:2010-04-15 19:53:50
【问题描述】:

我知道这是一个非常常见的问题,但是每个网站上的所有答案都不起作用!如果你还是不明白我的意思,那么也许这行代码可以帮助你理解。


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [[event allTouches] anyObject];
    CGPoint location = [touch locationInView:self.view];
    if (touch.view == nextbutton)
        [self performSelector:@selector(next)];
    if (touch.view == prevbutton)
        [self performSelector:@selector(previous)];
    if (touch.view == moreoptionsbutton)
        [self performSelector:@selector(moresettings)];
}

当你触摸nextbutton, prevbutton, and more optionsbutton 时它不会做任何事情,顺便说一下UIImageViews。我也尝试过使用isEqual: 而不是==,但这也没有成功。有什么建议吗?

【问题讨论】:

    标签: ios iphone uiimageview uitouch touchesbegan


    【解决方案1】:

    你必须为你的所有 UIImageViews 设置 userinteractionEnabled = YES 否则他们不会收到触摸事件。同时换行:

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

     UITouch *touch = [touches anyObject];
    

    【讨论】:

    • 感谢 userinteractionEnabled = YES 提示。我正在努力弄清楚为什么我的 UIImageView 没有注册触摸。
    【解决方案2】:

    我创建了一个检查,以确保它是我希望在继续之前点击的视图。

    if( [touch.view isKindOfClass:[Custom class]] ){
        CGPoint touchPoint = [touch locationInView:self.view];
    
        if( CGRectContainsPoint( self.customClassOne.frame, touchPoint )
           || CGRectContainsPoint( self.customClassTwo.frame, touchPoint ) ){
            [self touchOnCustomClass];
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-08
      • 1970-01-01
      • 2011-05-01
      • 2013-06-02
      • 1970-01-01
      相关资源
      最近更新 更多