Pregunta

Tengo un objeto que se mueve de derecha a izquierda por la pantalla:

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:7.8];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
 myImageview.layer.position = CGPointMake(20,  myImageView.layer.position.y);
[UIView commitAnimations];

Descubrí que incluso mientras la animación aún está sucediendo, XCode ya marca la ubicación de la imagen como su destino final, y para detectar un toque en la imagen en movimiento, necesito usar la capa de presentación:

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

Esta parte funciona.Ahora, me gustaría que la imagen suba cuando se presione.Quiero que la imagen se mueva hacia arriba mientras continúa su movimiento lateral.En cambio, este código mueve la imagen no sólo hacia arriba, sino también hasta su destino final a la izquierda:

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

Quiero que la imagen se mueva hacia arriba mientras continúa su movimiento lateral.¿Alguien sabe de una manera de lograr esto?

¡Muchas gracias!

¿Fue útil?

Solución

¿Has intentado configurar la opción de animación? UIViewAnimationOptionBeginFromCurrentState?

(Digo opción porque es una opción en los métodos de animación basados ​​en bloques introducidos con iOS 4.También está disponible como [UIView setAnimationBeginsFromCurrentState:YES] si aún no puede abandonar los métodos obsoletos de la clase UIView).

touchesBegan se convierte (usando animaciones de bloques):

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

Si usa eso, debería poder especificar el final x y y Las coordenadas que desea y la animación proceden desde el punto en el que se tocó el objeto hasta esa posición.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top