2010-04-15 11 views
15

So che questa è una domanda molto comune, ma tutte le risposte su ogni sito Web non funzionano! Se ancora non sai cosa intendo, forse questa linea di codice ti aiuterà a capire.Come si dice quale oggetto viene toccato in contatto?


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

E non fa niente quando si tocca nextbutton, prevbutton, and more optionsbutton, che sono UIImageViews a proposito. Ho anche provato a utilizzare isEqual: anziché ==, ma non ha funzionato. Eventuali suggerimenti?

risposta

34

Devi impostare userinteractionEnabled = YES per tutte le tue UIImageViews altrimenti non riceveranno eventi touch. cambiare anche la linea:

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

a

UITouch *touch = [touches anyObject]; 
+1

Grazie per l'utenteinteractionEnabled = SÌ suggerimento. Stavo sbattendo la testa per capire perché il mio UIImageView non registrava tocchi. – DenVog

2

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