1
这是我的方法移动播放器(又名ball
与label
同步):UIImageView的框架不是动态
int x2 = self.ball.frame.origin.x;
int y2 = self.ball.frame.origin.y;
double distance = sqrt(pow((x2 - coords.x), 2.0) + pow((y2 - coords.y), 2.0));
// 140 is just an arbitrary multiplier
NSTimeInterval time = distance/140;
animating = YES;
[UIView animateWithDuration:time delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
[self.ball setFrame:CGRectMake(coords.x, coords.y, 48, 48)];
[self.label setFrame:CGRectMake(coords.x - 30, coords.y - 25, 108, 20)];
} completion:^(BOOL finished) {
animating = NO;
}];
此代码的工作就一个水龙头,但是当一名球员重定向他的举动(倒是在别处虽然仍然动画,因此UIViewAnimationOptionBeginFromCurrentState
),距离是错误的。这是因为该框架设置为第一次触摸的结束点,而不是动画结束的位置。
UIView动画从来没有打算用于玩游戏场景。 “重新引导他的举动”这个概念就是一个例子。你可以用UIView动画来做,但这是一个彻头彻尾的破解,因为UIView动画不是一个真正的动画物理引擎的实时,逐帧的世界。这听起来像你可能想考虑采用SpriteKit。 – matt