UITapGestureRecognizer点击半径

时间:2013-11-20 04:15:01

标签: objective-c uiimageview uitapgesturerecognizer

我有UIImageView在屏幕上移动,我在图片中添加了UITapGestureRecognizer。这一切都很好,但是,当图像开始在屏幕上移动得太快时,它就越难以点击并被识别。

如果您看一下这张图片,您会看到一个围绕2个数字的绿色正方形。

http://i.stack.imgur.com/3x7rT.png

比方说,你必须在绿色方块内触摸才能注册为“点按”。目前它就像顶部的图像,但我想在底部图像中有一些额外的填充。那么我如何才能使UITapGestureRecognizer更大的区域?

1 个答案:

答案 0 :(得分:0)

不是将点击手势识别器添加到图像视图,而是将其添加到图像视图的超级视图中。迭代子视图以找到应该接收轻击手势的视图。例如:

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIView *containerView = [self view];

    UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
    [containerView addGestureRecognizer:tapRecognizer];
}

- (void)tap:(UITapGestureRecognizer *)sender
{
    UIView *containerView = [sender view];
    CGPoint location = [sender locationInView:containerView];

    for (UIView *subview in [[containerView subviews] reverseObjectEnumerator]) {
        if (![subview isKindOfClass:[UIImageView class]]) {
            continue;
        }
        if (CGRectContainsPoint([subview frame], location)) {
            [self imageViewTapped:(UIImageView *)subview];
            return;
        }
    }
}

- (void)imageViewTapped:(UIImageView *)imageView
{
    // do something when an image view is tapped
}