我有一个 2d map ,用户可以使用手势识别器进行缩放和平移。虽然它有效,但我希望用户在抬起 1 根手指后立即开始平移。不幸的是,在文档中它说:
The gesture ends (UIGestureRecognizerStateEnded) when both fingers lift from the view.
这是假装我马上从捏缩放到平移。我能做些什么来解决这个问题?
最佳答案
这是可能的,而且很容易!它涉及成为您的手势识别器的代表。似乎没有人知道的东西存在。在我的 View Controller 子类中,我声明两者都符合协议(protocol) <UIGestureRecognizerDelegate>
和两个 ivars:
UIPinchGestureRecognizer *myPinchGR;
UIPanGestureRecognizer *myPanGR;
这些 ivars 是在加载 View 中实例化的。注意将 self 设置为代表。
-(void)viewDidLoad{
[super viewDidLoad];
myPanGR = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panTarget:)];
myPanGR.delegate = self;
[self.view addGestureRecognizer:myPanGR];
myPinchGR = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchTarget:)];
myPinchGR.delegate = self;
[self.view addGestureRecognizer:myPinchGR];
}
由
UIGestureRecognizer
调用的代表电话之一是 shouldRecognizeSimultaneouslyWithGestureRecognizer:
如果我有两个以上的手势识别器,那么这个函数必须包含一些逻辑。但由于只有两个,我可以返回 YES
.-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
return YES;
}
现在您必须在您的操作方法中包含一点(非常少)额外的逻辑来筛选适当的条件。
-(void)panTarget:(UIPanGestureRecognizer *)panGR{
if (panGR.numberOfTouches > 1) return;
NSLog(@"panny");
}
-(void)pinchTarget:(UIPinchGestureRecognizer *)pinchGR{
if (pinchGR.numberOfTouches < 2) return;
NSLog(@"pinchy");
}
运行此代码查看日志。你会看到当你移动一根手指时你会看到“panny”当你把第二根手指放下时你会看到“pinchy”,然后来回移动。
关于ios - 当 1 根手指在屏幕上抬起时禁用捏合识别器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8958043/