ios - 当 1 根手指在屏幕上抬起时禁用捏合识别器

标签 ios ios5 uigesturerecognizer

我有一个 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/

相关文章:

iOS - 使按钮 subview 不受 Super View 的双击识别器的影响

iphone - 如何将两个 UIGestureRecognizer 链接在一起?

ios - 当我在 safari 浏览器中点击顶部的返回应用程序按钮时,如何清除 ios 应用程序中的 facebook 凭据?

ios - 如何在 nib/storyboard 的 UITextView 中换行?

ios - RxSwift 和 UICollectionView、UITableView

iphone - deleteRowsAtIndexPaths 不起作用

ios - 为什么 reloadRowsAtIndexPaths 不适用于 iOS 5.0?

ios - 使用 UIGestureRecognizer 同时缩放、旋转和移动

ios - 设置使用 AVAssetImageGenerator ios 生成的缩略图的图像大小

ios - 设备方向改变时如何移动对象?