Pergunta

Eu tenho um objeto que está se movendo da direita para a esquerda na tela:

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

Descobri que mesmo enquanto a animação ainda está acontecendo, o XCode já marca a localização da imagem como destino final, e para detectar um toque na imagem em movimento, preciso usar o 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!");
    }
}

Esta parte funciona.Agora, gostaria que a imagem subisse quando for pressionada.Quero que a imagem suba enquanto continua seu movimento lateral.Em vez disso, este código move a imagem não apenas para cima, mas também até seu destino final à esquerda:

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

Quero que a imagem suba enquanto continua seu movimento lateral.Alguém sabe uma maneira de fazer isso?

Muito obrigado!

Foi útil?

Solução

Você já tentou definir a opção de animação UIViewAnimationOptionBeginFromCurrentState?

(Eu digo opção porque é uma opção nos métodos de animação baseados em blocos introduzidos no iOS 4.Também está disponível como [UIView setAnimationBeginsFromCurrentState:YES] se você ainda não pode abandonar os métodos obsoletos da classe UIView.)

touchesBegan se torna (usando animações de bloco):

- (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 você usar isso, poderá especificar o final x e y coordenadas desejadas e faça com que a animação prossiga do ponto em que o objeto foi tocado até aquela posição.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top