Animare una UIImageView nel corso di un'animazione
-
12-12-2019 - |
Domanda
Ho un oggetto che si muove da destra a sinistra sullo schermo:
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:7.8];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
myImageview.layer.position = CGPointMake(20, myImageView.layer.position.y);
[UIView commitAnimations];
Ho scoperto che, anche durante l'animazione è ancora accadendo, XCode segna già l'immagine di posizione come destinazione finale, e al fine di rilevare un tocco sull'immagine in movimento, ho bisogno di utilizzare il presentationLayer:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesBegan:touches withEvent:event];
UITouch *touch = [touches anyObject];
CGPoint touchPoint = [touch locationInView:self.view];
if ([myImageview.layer.presentationLayer hitTest:touchPoint]) {
NSLog(@"it's a hit!");
}
}
Questa parte funziona.Ora, vorrei l'immagine per spostarsi in alto, quando viene premuto.Voglio l'immagine per spostarsi in alto mentre continua è il movimento laterale. Invece, questo codice si sposta l'immagine non solo, ma anche tutto il modo per la sua destinazione finale a sinistra:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesBegan:touches withEvent:event];
UITouch *touch = [touches anyObject];
CGPoint touchPoint = [touch locationInView:self.view];
if ([mouse.layer.presentationLayer hitTest:touchPoint]) {
NSLog(@"it's a hit!");
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
myImageView.layer.position = CGPointMake( mouse.layer.position.x, myImageView.layer.position.y - 40);
[UIView commitAnimations];
}
}
Voglio l'immagine per spostarsi in alto mentre continua è il movimento laterale.Qualcuno sa di un modo per ottenere questo risultato?
Grazie mille!
Soluzione
Hai provato impostando l'opzione di animazione UIViewAnimationOptionBeginFromCurrentState
?
(Dico opzione perché è un'opzione nel blocco di animazione basato su metodi introdotti con iOS 4.È disponibile anche come [UIView setAnimationBeginsFromCurrentState:YES]
se non è possibile passare da deprecato UIView i metodi della classe appena ancora.)
touchesBegan diventa (utilizzando animazioni di blocco):
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesBegan:touches withEvent:event];
UITouch *touch = [touches anyObject];
CGPoint touchPoint = [touch locationInView:self.view];
if ([mouse.layer.presentationLayer hitTest:touchPoint]) {
NSLog(@"it's a hit!");
[UIView animateWithDuration:0.5 delay:0.0 options:(UIViewAnimationOptionCurveLinear & UIViewAnimationOptionBeginFromCurrentState) animations:^{
myImageView.layer.position = CGPointMake( myImageView.layer.position.x, mouse.layer.position.y - 40);
}completion:^(BOOL complete){
//
}];
}
}
Se si utilizza che si dovrebbe essere in grado di specificare il finale x
e y
le coordinate che si desidera e avere l'animazione procedere dal punto in cui l'oggetto è stato toccato da quella posizione.