iPhone 横向模式下的球动画
我使用 UIAccelerometer 在纵向模式下在屏幕上制作一个球的动画。
它工作正常,现在我希望在横向模式下也能工作。当我尝试在横向模式下使用相同的代码部分(用于纵向)时,它给了我一个奇怪的结果,似乎完全错误。
我下面的代码用于纵向视图。
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
// apply acceleration to velocity
NSTimeInterval elapsedTime = acceleration.timestamp - lastAccelTimestamp;
ballVelocityX = ballVelocityX + (acceleration.x * MAX_ACCEL_PER_SEC * elapsedTime);
ballVelocityY = ballVelocityY - (acceleration.y * MAX_ACCEL_PER_SEC * elapsedTime);
if (lastAccelTimestamp > 0) {
// recalc ball position
ballX += (ballVelocityX * elapsedTime); // ballX is CGFloat
ballY += (ballVelocityY * elapsedTime); // ballY is CGFloat
// update view
[ballView setCenter:CGPointMake(ballX, ballY)];
}
lastAccelTimestamp = acceleration.timestamp;
}
I'm animating a ball on the screen in portrait mode using UIAccelerometer
.
It's working fine, now I want the same to work in landscape mode. When I tried to use the same part of code (for portrait) in the landscape mode it gives me a weird result, seems to be completely wrong.
My code below is for portrait view.
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
// apply acceleration to velocity
NSTimeInterval elapsedTime = acceleration.timestamp - lastAccelTimestamp;
ballVelocityX = ballVelocityX + (acceleration.x * MAX_ACCEL_PER_SEC * elapsedTime);
ballVelocityY = ballVelocityY - (acceleration.y * MAX_ACCEL_PER_SEC * elapsedTime);
if (lastAccelTimestamp > 0) {
// recalc ball position
ballX += (ballVelocityX * elapsedTime); // ballX is CGFloat
ballY += (ballVelocityY * elapsedTime); // ballY is CGFloat
// update view
[ballView setCenter:CGPointMake(ballX, ballY)];
}
lastAccelTimestamp = acceleration.timestamp;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论