0

使用時にタッチオフセットを取得するにはどうすればよいUITouchですか? リモコンのようなプロジェクトを行っています。マルチタッチに設定されたビューがあり、ビューを Mac のタッチ パッドのように動作させたいので、マウスを制御するために人が移動するときにタッチ オフセットが必要です。何か案は?

4

1 に答える 1

0

これはUIPanGestureRecognizer、画面の中心から現在のタッチ位置までの対角距離を測定することで実現できます。

#import <QuartzCore/QuartzCore.h>

self.view画面全体がタッチ イベントに応答するように、ジェスチャを宣言してフックします。

UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(myPanRecognizerMethod:)];
[pan setDelegate:self];
[pan setMaximumNumberOfTouches:2];
[pan setMinimumNumberOfTouches:1];
[self.view setUserInteractionEnabled:YES];
[self.view addGestureRecognizer:pan];

次に、このメソッドでは、ジェスチャ レコグナイザーの state: を使用しUIGestureRecognizerStateChangedて、タッチ位置が変化したときにタッチ位置と画面中心の間の対角線距離を測定する整数を更新します。

-(void)myPanRecognizerMethod:(id)sender
{
    [[[(UITapGestureRecognizer*)sender view] layer] removeAllAnimations];
    if ([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateChanged) {
        CGPoint touchLocation = [sender locationOfTouch:0 inView:self.view];
        NSNumber *distanceToTouchLocation = @(sqrtf(fabsf(powf(self.view.center.x - touchLocation.x, 2) + powf(self.view.center.y - touchLocation.y, 2))));
        NSLog(@"Distance from center screen to touch location is == %@",distanceToTouchLocation);
    }
}
于 2012-08-16T04:19:12.613 に答える