你怎么知道触摸什么对象?

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

- (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当你触摸nextbutton, prevbutton, and more optionsbuttonUIImageViews ,它什么也不做。 我也尝试使用isEqual:而不是== ,但是还没有解决。 有什么build议么?

您必须为所有的UIImageView设置userinteractionEnabled = YES,否则它们将不会收到触摸事件。 还要改行:

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

  UITouch *touch = [touches anyObject]; 

我创build了一个检查,以确保它的观点,我希望在继续之前点击。

 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]; } }