iphone - 通过触摸传递到下面的 UIViews

标签 iphone ios uiview uiimageview uitapgesturerecognizer

我有一个 UIView,上面有 4 个按钮,另一个 UIView 在按钮 View 之上。最上面的 View 包含一个 UIImageView,上面有一个 UITapGestureRecognizer

我试图创建的行为是,当用户点击 UIImageView 时,它会在屏幕右下角变小和动画变大之间切换。当它很大时,我希望底部 View 上的按钮被禁用,当它很小并且在右下角时,我希望将触摸传递给按钮并让它们正常工作。我快到了,但除非我禁用顶 View 的 UserInteractions,否则我无法将触摸传递给按钮。

我在顶 View 的 initWithFrame: 中有这个:

// Add a gesture recognizer to the image view
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageTapped:)];
tapGestureRecognizer.cancelsTouchesInView = NO;
[imageView addGestureRecognizer:tapGestureRecognizer];
[tapGestureRecognizer release];

我这是我的 imageTapped: 方法:

- (void) imageTapped:(UITapGestureRecognizer *) gestureRecognizer {
    // Toggle between expanding and contracting the image
    if (expanded) {
        [self contractAnimated:YES];
        expanded = NO;
        gestureRecognizer.cancelsTouchesInView = NO;
        self.userInteractionEnabled = NO;
        self.exclusiveTouch = NO;
    }
    else {
        [self expandAnimated:YES];
        expanded = YES;
        gestureRecognizer.cancelsTouchesInView = NO;
        self.userInteractionEnabled = YES;
        self.exclusiveTouch = YES;
    }
}

使用上面的代码,当图像很大时按钮处于非事件状态,当我触摸图像时它会缩小并且按钮会变为事件状态。但是,小图像不会接收到触摸,因此不会展开。

如果我在这两种情况下都设置了 self.userInteractionEnabled = YES,那么图像会在触摸时展开和收缩,但按钮永远不会接收到触摸并且会像禁用一样。

是否可以让图像在触摸时展开和收缩,但如果图像处于收缩状态,下面的按钮只能接收触摸?我是不是在这里做了一些愚蠢的事情而遗漏了一些明显的东西?

我非常想让它工作,所以任何帮助都将不胜感激,

戴夫

更新:

为了进一步测试,我覆盖了 touchesBegan:touchesCancelled: 方法,并在包含 UIImageView 的 View 上调用它们的 super 实现。使用上面的代码,永远不会调用 touchesCancelled: 而总是调用 touchesBegan:

所以看起来 View 正在获取触摸,它们只是没有传递到下面的 View 。

更新

这是因为响应链的工作方式吗?我的 View 层次结构如下所示:

VC - View1
     -View2
      -imageView1 (has tapGestureRecogniser)
      -imageView2
     -View3
      -button1
      -button2

我认为操作系统首先执行 hitTest,因为 View2 在前面,所以应该获得所有触摸,并且这些触摸永远不会传递给 View3,除非 View2 的 userInteractions 设置为 NO,在这种情况下,imageView1 也无法接收触及。它是这样工作的吗?有没有办法让 View2 将它的触摸传递给 View3?

最佳答案

我认为 UIGestureRecognizer 是一个转移注意力的问题。最后为了解决这个问题,我重写了 UIView:

pointInside:withEvent: 方法
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
    BOOL pointInside = NO;

    if (CGRectContainsPoint(imageView.frame, point) || expanded) pointInside = YES;

    return pointInside;
}

如果您触摸 imageView 或设置了其扩展标志,这会导致 View 捕获所有触摸。如果未展开,则仅捕获位于 imageView 上的触摸。

通过返回 NO,顶层 VC 的 View 查询其 View 层次结构的其余部分以寻找命中。

关于iphone - 通过触摸传递到下面的 UIViews,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9026097/

相关文章:

iphone - 在 UITableView 上方增加 UIView 的高度

ios - iPhone5和iPhone 6图片后缀?

iphone - 在 iOS 中将单元格添加到 UITableView 的底部

ios - UIImage -drawAtPoint : very slow for large images

swift - Material swift : UIView with Searchbar and TabBar moves out of view swift 3

ios - Swift - 在显示时将 textField 移动到键盘上

iphone - 我应该如何将文件包含在用户可以传输到笔记本电脑/桌面上的应用程序中

iphone - Facebook iOS SDK : session invalid on iPhone after logging out of Facebook from a desktop

ios - MPMoviePlayerController 寻找前进按钮停止 IOS7 中的视频?

ios - Firebase动态链接无法打开iOS应用