2012-08-16 45 views
0

如何在使用UITouch时获得触摸偏移?我正在做一个像遥控器一样的项目。我有一个视图设置为多点触摸,我希望视图可以像Mac的触摸板一样操作,所以当人们移动鼠标时需要触摸偏移。有任何想法吗?如何在使用UITouch时获得触摸偏移

+0

我的回答有帮助吗? – 2012-08-17 17:50:27

回答

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); 
    } 
} 
相关问题