错误修复移动对象并设置新坐标

时间:2011-12-29 13:35:46

标签: iphone objective-c cocoa-touch debugging core-animation

我有一个非常简单的问题,但我无法找到这个错误的原因。任何想法都赞赏。

所以,我有一个UIImageView的对象,然后从A点(x1,y1)到点B(x2,y2)到点C(x2,y2)轻弹它(移动它),依此类推....

当我将它从A移动到B到C到D等等...... 没有动画,它可以工作!

当我将它从A移动到B到C ......等等...... WITH ANIMATION - >有一个错误! 只有A到B有效,B到C,......等等出现在其他地方。

我基本上实现了touchesBegan方法和touchesEnd方法,并使用

将对象移动到新位置
//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. 
}

在动画结束时,我做了

- (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.

}

这是错误:

当我指定object.center = tempFrame;时,我希望对象位于(x2,y2)。它在技术上处于(x2,y2)(当我NSlog位置时,我发现了),但它出现在屏幕的其他位置。

如果我再次将它移动到C点,它会使用(x2,y2)并正确移动到(x3,y3),但它会再次显示在屏幕上的其他位置!

任何想法??????

1 个答案:

答案 0 :(得分:1)

我建议使用transform.translation.xtransform.translation.y密钥路径,而不是使用动态position.xposition.y密钥路径。 所以,改变片段

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)];

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

在这种情况下你也可以使用position密钥路径(并将CGPoint设置为toValue