我有一个非常简单的问题,但我无法找到这个错误的原因。任何想法都赞赏。
所以,我有一个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),但它会再次显示在屏幕上的其他位置!
任何想法??????
答案 0 :(得分:1)
我建议使用transform.translation.x
和transform.translation.y
密钥路径,而不是使用动态position.x
和position.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
)