Question

J'ai un objet qui se déplace de droite à gauche sur l'écran :

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

J'ai découvert que même pendant que l'animation est toujours en cours, XCode marque déjà l'emplacement de l'image comme destination finale, et afin de détecter un contact sur l'image en mouvement, je dois utiliser le 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!");
    }
}

Cette partie fonctionne.Maintenant, j'aimerais que l'image monte vers le haut lorsqu'on appuie dessus.Je veux que l'image monte tout en continuant son mouvement latéral.Au lieu de cela, ce code déplace l'image non seulement vers le haut, mais également jusqu'à sa destination finale sur la gauche :

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

Je veux que l'image monte tout en continuant son mouvement latéral.Quelqu'un connaît-il un moyen d'y parvenir ?

Merci beaucoup!

Était-ce utile?

La solution

Avez-vous essayé de définir l'option d'animation UIViewAnimationOptionBeginFromCurrentState?

(Je dis option car c'est une option dans les méthodes d'animation basées sur des blocs introduites avec iOS 4.Il est également disponible sous forme [UIView setAnimationBeginsFromCurrentState:YES] si vous ne pouvez pas encore abandonner les méthodes de classe UIView obsolètes.)

touchesBegan devient (en utilisant des animations de blocs) :

- (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 vous l'utilisez, vous devriez pouvoir spécifier le résultat final x et y coordonnées souhaitées et faites en sorte que l'animation se déroule du point où l'objet a été touché jusqu'à cette position.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top