16

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


- (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:而不是==,但这也没有成功。有什么建议么?

4

2 回答 2

35

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

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

 UITouch *touch = [touches anyObject];
于 2010-04-15T21:08:19.747 回答
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];
    }
}
于 2014-12-15T13:52:04.423 回答