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];

然后在这个方法中,我们使用手势识别器状态: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 回答