Search code examples
iphoneobjective-ccocoa-touchdebuggingcore-animation

Bug Fixing in moving an object and setting new coordinates


I have a very simple issue, but I am not able to find the reason for this bug. Any ideas appreciated.

So, I have an object of UIImageView and flick it (move it) from Point A(x1,y1) to Point B(x2, y2) to Point C(x2,y2), and so on....

When I move it from A to B to C to D and so on... WITHOUT ANIMATION, it works !

When I move it from A to B to C .... and so on... WITH ANIMATION -> There is a bug ! Only A to B works, B to C,... and so forth appears some place else.

I basically implement the touchesBegan method, and touchesEnd method and move the object to the new location with

//NO ANIMATION  ==> Works !

-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    NSLog(@"End of touch");

    UITouch *touch = [[event touchesForView:self.view] anyObject];
    CGPoint location = [touch locationInView:touch.view];

    object.center = CGPointMake(location.x,  location.y); 

}

..

//ADDING ANIMATION ==> Bug ! 

-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    NSLog(@"End of touch");

    UITouch *touch = [[event touchesForView:self.view] anyObject];
    CGPoint location = [touch locationInView:touch.view];

    [self spinLayer:object.layer duration:1 direction:SPIN_CLOCK_WISE newFrame:location];
    tempFrame = location;   // tempFrame is global. 
}

And at the end of animation, I do

- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag {

object.center = tempFrame;

// I assign this because the object has to be updated to its new location to move it to points B, C, D and so forth...

// The object is actually at the desired point. But it appears on the SCREEN at a different location although it `NSLogs` at the right point.

}

This is the bug :

When I assign object.center = tempFrame; I expect the object to be at (x2,y2). It is technically at (x2,y2) (when I NSlog the location, I found out), but it appears elsewhere on the screen.

If I again move it to Point C, it uses (x2, y2) and correctly moves to (x3,y3), but it appears somewhere else on the screen again !

Any ideas ??????


Solution

  • Instead of using animatio transform.translation.x and transform.translation.y key paths I'd suggest using position.x and position.y keypaths. So, change the fragment

    theAnimationX=[CABasicAnimation animationWithKeyPath:@"transform.translation.x"];
    theAnimationX.toValue=[NSNumber numberWithFloat:(newFrame.x-object.center.x)];
    theAnimationY=[CABasicAnimation animationWithKeyPath:@"transform.translation.y"];
    theAnimationY.toValue=[NSNumber numberWithFloat:(newFrame.y-object.center.y)];
    

    to

    theAnimationX=[CABasicAnimation animationWithKeyPath:@"position.x"];
    theAnimationX.toValue=[NSNumber numberWithFloat:newFrame.x)];
    theAnimationY=[CABasicAnimation animationWithKeyPath:@"position.y"];
    theAnimationY.toValue=[NSNumber numberWithFloat:(newFrame.y)];
    

    You can also use just position keypath in this case (and set CGPoint as toValue)