0

我有一个带有一个的subview,主视图有一个。我已经将唯一添加到这样的视图中:imageviewPanGestureRecognizerLongPressGestureRecognizerlongpress

screenRecognize = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(screenTaped:)];
screenRecognize.minimumPressDuration = 0.0;
self.userInteractionEnabled = YES;
[self addGestureRecognizer:screenRecognize];

这是图像视图:

imageViewPanRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(imageViewPulled:)];

imageView = [[UIImageView alloc] initWithFrame:CGRectMake(310, 50, 10, 40)];
imageView.image = [UIImage imageNamed:@"image.png"];
imageView.userInteractionEnabled = YES;
[imageView addGestureRecognizer:imageViewPanRecognizer];
[self addSubview:imageView];

当我触摸 时imageViewUILongPressRecognizer被触发。为什么是这样?

4

2 回答 2

2

答案已经在你的问题中了。你设置

screenRecognize.minimumPressDuration = 0.0;

这意味着,UILongPressGestureRecognizer 将像 UITapGestureRecognizer 一样工作。按线

[self addGestureRecognizer:screenRecognize];

你在整个自我上激活了这个手势。

现在,当您在self中添加imageView 时,imageView 还将响应 UILongPressGestureRecognizer 以及仅在 imageView 上处于活动状态的 UIPanGestureRecognizer。因此,触摸 imageView 会触发 UILongPressGestureRecognizer。


要解决此问题,您可以尝试增加minimumPressDuration值。

于 2013-07-04T12:06:27.630 回答
0

1) 为什么要使用 minimumPressDuration=0 的长手势?不能正确使用平移手势?

2)如果您希望手势协同工作,请尝试以下操作:

longGesture.delegate = self;
...
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}

3)如果您希望用户平移到 UIImageView 长手势禁用,请尝试以下操作:

self.tag = 1;
longGesture.delegate = self;
...
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    return touch.view.tag == 1;
}
于 2013-07-04T12:17:47.393 回答