ios iphone uiimageview uitouch touchesbegan

ios - ¿Cómo sabes qué objeto está siendo tocado en toquesBegan?



iphone uiimageview (2)

Sé que esta es una pregunta muy frecuente, ¡pero todas las respuestas en todos los sitios web no funcionan! Si aún no sabes a qué me refiero, tal vez esta línea de código te ayude a comprender.

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

No hace nada cuando tocas el nextbutton, prevbutton, and more optionsbutton , que por UIImageViews son UIImageViews . También he intentado usar isEqual: lugar de == , pero eso tampoco ha funcionado. ¿Alguna sugerencia?


Creé un cheque para asegurarme de que es la vista en la que espero que se haga clic antes de continuar.

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


Debe configurar userinteractionEnabled = YES para todas sus UIImageViews, de lo contrario no recibirán eventos táctiles. También cambia la línea:

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

a

UITouch *touch = [touches anyObject];