2012-09-09 15 views
5

Nel progetto, ci sono un UIView myView e un UIImageView myImage sedere il myView, vista la gerarchia:Come aggiungere il riconoscimento dei gesti a una UIImageview?

UIWindow
| - UIImageView (myImage)
| - UIView (myView) [schermo intero]

poi ha aggiunto un sistema di riconoscimento gesto per myImage in ViewController

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)]; 
     tapGesture.numberOfTapsRequired = 1; 
     [myImage addGestureRecognizer:tapGesture]; 

Ma il gesto non influisce myImage, myImage ha setUserInteractionEnabled:YES. Interesserà solo quando myImage viene posizionato davanti a myView, come posso risolvere questo problema?

+0

Se è dietro una vista, non riceverà tocchi. – ohr

risposta

11

Il tuo UIView ovviamente intercetta i tocchi prima che possano raggiungere il UIImageView.

è possibile mettere il UIImageView di fronte al vostro UIView, o interazione con l'utente disabilita sulla UIView in modo che non intercetta i tocchi.

Se avete bisogno di grana più fine sopra la cattura i tuoi eventi touch, si può:

  • mettere il UIView in cima alla UIImageView se deve ai fini della progettazione (se maschera il UIImageView un po ', per esempio), ma renderlo non intercettare eventi (userInteractionEnabled = NO), e utilizzare un diverso UIView sotto il UIImageView per catturare in realtà gli eventi al di fuori del UIImageView
  • mantenere la UIView sulla parte superiore del UIImageView e tenerlo cattura eventi (userInteractionEnabled = YES), ma filtrare gli eventi che lo attraversano sottoclassi il UIView e sostituisce il metodo pointInside:withEvent: o hitTest:withEvent:.

Per ulteriori informazioni si dovrebbe davvero leggere the dedicated Apple Programming Guide related to Event Handling.

+0

ma myImage deve rimanere dietro a myView, esiste un modo in cui posso effettuare un rilevamento all'avvio del tocco e determinare quale visualizzazione avrà effetto? – pqteru

+0

Sì. Come ho affermato nella mia risposta, disattiva l'interazione dell'utente ('userInteractionEnabled = NO') su UIView. Se 'UIView' ha ancora bisogno di catturare gli eventi touch ovunque tranne dove si trova' UIImageView', hai più possibilità: (1) rendere il tuo 'UIView' in cima a' UIImageView' ma con 'userInteractionEnabled = NO' e mettere un altro 'UIView' sotto' UIImageView' che cattura ogni evento tattile che non è stato catturato da 'UIImageView' o (2) mette 'UIView' in cima a' UIImageView', sottoclasse e sovrascrive 'hitTest: withEvent: 'or metodo pointInside: withEvent:' UIView – AliSoftware

1

il mio codice:

UILongPressGestureRecognizer *press = [[UILongPressGestureRecognizer alloc] 
initWithTarget:self action:@selector(hasLongPressed)]; 
[press setMinimumPressDuration:0.1f]; //1ms 
press.delegate = (id)self; 
[self.DGEMLogo addGestureRecognizer:press]; 

due proprietà sono neccessary fare

1.) aggiungere il GestureRecognizer al UIImageView (self.DGEMLogo) [self.DGEMLogo addGestureRecognizer: stampa];

2.) impostare il vostro UIImage proprietà Interazione Enabled su True

Questo è tutto.

Problemi correlati