私は自分のプログラム Bit に類似したものを書きましたが、3D で回転するため、私のケースはもう少し複雑だと思います: https://itunes.apple.com/ua/app/bit/id366236469?mt=8
基本的に私がしていることは、何らかのメソッドを定期的に呼び出す NSTimer をセットアップすることです。回転行列を作成するために方向と速度を取得するだけです (私が言ったように、3D は少し厄介です :P )。速度に 1 より小さい数値を掛けて、速度を下げます。減算ではなく乗算を行う理由は、ユーザーからのスピンが 2 倍の速さである場合、オブジェクトを 2 倍長く回転させたくないためです。
ホイールがどちらの方向に回転しているかを把握するには、すべての情報がある 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
}