Si prega di qualcuno può aiutare a ordinare un noob fuori? Ho postato questo problema su vari forum e non ho ricevuto risposte, mentre molte ricerche per altre risposte sono diventate stackOverflow, quindi spero che questo sia il posto giusto.UIScrollView tocchi vs tocchi subview
Ho un BeachView.h (sottoclasse di UIScrollView, immagine di una spiaggia sabbiosa) coperto con un numero casuale di Stone.h (sottoclasse di UIImageView, un PNG casuale di una pietra, userInteractionEnabled = SÌ per accettare tocchi).
Se l'utente tocca e si sposta sulla spiaggia, deve scorrere. Se l'utente tocca una pietra, deve chiamare il metodo "touchedStone". Se l'utente tocca la spiaggia dove non c'è pietra, deve chiamare il metodo "touchedBeach".
Ora, mi rendo conto che sembra semplice. Tutti e tutti mi dicono che se c'è qualcosa su un UIScrollView che accetta tocca che dovrebbe passare il controllo su di esso. Quindi quando tocco e trascino, dovrebbe scorrere; ma se tocco, ed è su una pietra, dovrebbe ignorare i rubinetti sulla spiaggia e accettare i rubinetti di pietra, sì?
Tuttavia, sembra che entrambe le visualizzazioni stiano accettando il tocco e chiamando sia touchedStone AND touchedBeach. Inoltre, il tap alla spiaggia viene eseguito per primo, quindi non posso nemmeno inserire un flag di tipo "if touchedStone then do not run touchBeach".
Ecco un po 'di codice. Su BeachView.m
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
if (self.decelerating) { didScroll = YES; }
else { didScroll = NO; }
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:touch.view];
NSLog(@"touched beach = %@", [touch view]);
lastTouch = touchLocation;
[super touchesBegan:touches withEvent:event];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
didScroll = YES;
[super touchesMoved:touches withEvent:event];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
if (didScroll == NO && isPaused == NO) {
[self touchedBeach:YES location:lastTouch];
}
[super touchesEnded:touches withEvent:event];
}
Su Stone.m
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[parent stoneWasTouched]; // parent = ivar pointing from stone to beachview
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:touch.view];
NSLog(@"touched stone = %@", [touch view]);
[parent touchedStone:YES location:touchLocation];
}
Dopo un rubinetto di pietra, La mia NSLog assomiglia a questo:
Touched beach = <BeachView: 0x1276a0>
ran touchedBeach
Touched Stone = <Stone: 0x1480c0>
ran touchedStone
Quindi è effettivamente in esecuzione entrambi. Ciò che è ancora più strano è che se prendo i tocchi Began e tocchi Fine di Stone.m ma lasci userInteractionEnabled = YES, il beachView registra entrambi i tocchi, ma restituisce la Pietra come la vista che ha toccato (la seconda volta).
Touched beach = <BeachView: 0x1276a0>
ran touchedBeach
Touched beach = <Stone: 0x1480c0>
ran touchedBeach
Quindi PER FAVORE, ho cercato di ordinare questo per giorni. Come faccio a fare in modo che una pietra maschiata richiami solo touchedStone e le chiamate a una spiaggia toccata toccano soloBeach? Dove sto andando male?
mi sono trasferito [super] per l'inizio dei metodi e ha fatto la differenza. Registra ancora due tap ma almeno ora sta dicendo 1-beginBeach 2-beginStone 3-endedBeach 4-endedStone, quindi c'è il tempo di far apparire una bandiera lì. Stavo solo guardando HitTest ma quando ha iniziato a parlare di CAlayers vs UIviews sembrava un libro che non ero ancora pronto ad aprire. Darò un altro colpo. Grazie Hatfinch, -k. –