Question

I have an object that is moving right to left across the screen:

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

I found out that even while the animation is still happening, XCode already marks the image's location as its final destination, and in order to detect a touch on the moving image, I need to use the 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!");
    }
}

This part works. Now, I'd like the image to move up when it is pressed. I want the image to move up while it continues it's sideways movement. Instead, this code moves the image not just up, but also all the way to its final destination on the left:

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

I want the image to move up while it continues it's sideways movement. Does anyone know of a way to accomplish this?

Thanks so much!

Was it helpful?

Solution

Have you tried setting the animation option UIViewAnimationOptionBeginFromCurrentState?

(I say option because it is an option in the block-based animation methods introduced with iOS 4. It is also available as [UIView setAnimationBeginsFromCurrentState:YES] if you can't switch away from the deprecated UIView class methods just yet.)

touchesBegan becomes (using block animations):

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

If you use that you should be able to specify the final x and y coordinates you want and have animation proceed from the point at which the object was touched to that position.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top