我为我的程序 Bit 写了一些类似的东西,但我认为我的情况有点复杂,因为我在 3D 中旋转:https ://itunes.apple.com/ua/app/bit/id366236469?mt=8
基本上我所做的是设置一个定期调用某些方法的 NSTimer。我只是用方向和速度来创建一个旋转矩阵(正如我所说,3D 有点糟糕:P),然后我将速度乘以小于 1 的数字,所以它会下降。乘法而不是减法的原因是,如果用户的旋转速度是原来的两倍,那么您不希望对象旋转两倍的时间,因为等待我发现这很烦人。
至于确定车轮旋转的方向,只需将其存储在您拥有所有信息的 touchesEnded:withEvent: 方法中。既然您说只要用户手指向下,您就已经可以进行跟踪,这应该是显而易见的。
我在 3D 中的内容类似于:
// MyView.h
@interface MyView : UIView {
NSTimer *animationTimer;
}
- (void) startAnimation;
@end
// MyAppDelegate.h
@implementation MyAppDelegate
- (void) applicationDidFinishLaunching:(UIApplication *)application {
[myView startAnimation];
}
@end
// MyView.m
GLfloat rotationMomentum = 0;
GLfloat rotationDeltaX = 0.0f;
GLfloat rotationDeltaY = 0.0f;
@implementation MyView
- (void)startAnimation {
animationTimer = [NSTimer scheduledTimerWithTimeInterval:(NSTimeInterval)((1.0 / 60.0) * animationFrameInterval) target:self selector:@selector(drawView:) userInfo:nil repeats:TRUE];
}
- (void) drawView:(id)sender {
addRotationByDegree(rotationMomentum);
rotationMomentum /= 1.05;
if (rotationMomentum < 0.1)
rotationMomentum = 0.1; // never stop rotating completely
[renderer render];
}
- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
}
- (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
{
UITouch *aTouch = [touches anyObject];
CGPoint loc = [aTouch locationInView:self];
CGPoint prevloc = [aTouch previousLocationInView:self];
rotationDeltaX = loc.x - prevloc.x;
rotationDeltaY = loc.y - prevloc.y;
GLfloat distance = sqrt(rotationDeltaX*rotationDeltaX+rotationDeltaY*rotationDeltaY)/4;
rotationMomentum = distance;
addRotationByDegree(distance);
self->moved = TRUE;
}
- (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event
{
}
- (void)touchesCancelled:(NSSet*)touches withEvent:(UIEvent*)event
{
}
我省略了 addRotationByDegree 函数,但它的作用是使用全局变量 rotationDeltaX 和 rotationDeltaY 并将旋转矩阵应用于已存储的矩阵,然后保存结果。在您的示例中,您可能想要一些更简单的东西,例如(我现在假设只有 X 方向的运动才能旋转轮子):
- (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
{
UITouch *aTouch = [touches anyObject];
CGPoint loc = [aTouch locationInView:self];
CGPoint prevloc = [aTouch previousLocationInView:self];
GLfloat distance = loc.x - prevloc.x;
rotationMomentum = distance;
addRotationByDegree(distance);
self->moved = TRUE;
}
void addRotationByDegree(distance) {
angleOfWheel += distance; // probably need to divide the number with something reasonable here to have the spin be nicer
}