你怎么知道touchesBegan中被触摸的对象?

Fla*_*la2 16 iphone uiimageview uitouch touchesbegan ios

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


- (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)];
}
Run Code Online (Sandbox Code Playgroud)

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

Run*_*oop 35

您必须为所有UIImageView设置userinteractionEnabled = YES,否则他们将不会收到触摸事件.也改行:

 UITouch *touch = [[event allTouches] anyObject];
Run Code Online (Sandbox Code Playgroud)

 UITouch *touch = [touches anyObject];
Run Code Online (Sandbox Code Playgroud)