2011-12-29 56 views
0

我有一個非常簡單的問題,但我無法找到此錯誤的原因。任何想法讚賞。所以,我有一個UIImageView的對象,並將它從點A(x1,y1)滑動到點B(x2,y2)到點C(x2,y2),等等......錯誤修復移動對象並設置新座標

當我從A移動到B到C到D等等... 沒有動畫,它的工作原理!

當我從一個將它移動到B到C ....等等...... 動畫 - >有一個錯誤!只有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)(當我找到的位置時),但它出現在屏幕上的其他地方。

如果我再次將它移動到點C,它將使用(x2,y2)並正確移動到(x3,y3),但它會再次出現在屏幕上的其他位置!

任何想法??????

+1

我們可以看到該方法'spinLayer:持續時間:方向:newFrame的:'? – joerick 2011-12-29 13:41:01

+0

當然。它只是簡單的動畫... http://pastebin.com/P0ypj7RY – Legolas 2011-12-29 13:44:32

+0

爲什麼不動畫keypath'position.x'和'postion.y'而不是'transform.translate.X/Y'?我檢查了位置上的動畫並且工作得很好 – 2011-12-29 14:38:47

回答

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的keyPath在這種情況下,(並設置CGPoint爲toValue

+0

真棒!任何方式,你可以幫助我與http://stackoverflow.com/questions/8668731/moving-rotating-an-object-using-its-updated-position-and-tilt?這是一個後續,但從這個不同的問題:) – Legolas 2011-12-29 15:52:08