Come si fa a dire quale oggetto viene toccata in touchesBegan?
-
27-09-2019 - |
Domanda
So che questa è una domanda molto comunemente chiesto, ma tutte le risposte su ogni sito web non funzionano! Se ancora non sai cosa voglio dire, allora forse questa riga di codice vi aiuterà a capire.
- (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)];
}
Non fa nulla quando si tocca nextbutton, prevbutton, and more optionsbutton
, che sono UIImageViews
a proposito. Ho anche provato ad utilizzare isEqual:
invece di ==
, ma che non ha funzionato neanche. Qualche suggerimento?
Soluzione
È necessario impostare userinteractionEnabled = YES per tutte le vostre UIImageViews altrimenti non riceveranno eventi touch. Anche cambiare la riga:
UITouch *touch = [[event allTouches] anyObject];
a
UITouch *touch = [touches anyObject];
Altri suggerimenti
Ho creato un controllo per essere sicuri che la sua vista mi aspetto di essere cliccato prima di proseguire.
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];
}
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow