Non creare una nuova vista per il proprio riconoscitore di gesti. Il riconoscitore implementa una posizioneInView: metodo. Impostalo per la vista che contiene la regione sensibile. Su handleGesture, esegui il test della regione che ti interessa in questo modo:
0) Fai tutto questo sulla vista che contiene la regione che ti interessa. Non aggiungere una vista speciale solo per il riconoscimento dei gesti.
1) Imposta mySensitiveRect
@property (assign, nonatomic) CGRect mySensitiveRect;
@synthesize mySensitiveRect=_mySensitiveRect;
self.mySensitiveRect = CGRectMake(0.0, 240.0, 320.0, 240.0);
2) Creare il gestureRecognizer:
gr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];
[self.view addGestureRecognizer:gr];
// if not using ARC, you should [gr release];
// mySensitiveRect coords are in the coordinate system of self.view
- (void)handleGesture:(UIGestureRecognizer *)gestureRecognizer {
CGPoint p = [gestureRecognizer locationInView:self.view];
if (CGRectContainsPoint(mySensitiveRect, p)) {
NSLog(@"got a tap in the region i care about");
} else {
NSLog(@"got a tap, but not where i need it");
}
}
Il rect sensibile deve essere inizializzato nel sistema di coordinate di myView, la stessa vista a cui si collega il riconoscitore.
Scusa ma, cosa farà? e per la variabile mySensitiveRect, posso usare (0, 0, 320, 480)? Hai un esempio completo? Grazie: D –
@DavidMurray: Speriamo che sia più chiaro – danh
Grazie, ce l'ha fatta. :-) –