iOS UIImageView tap 只在视图的顶部注册

iOS UIImageView tap only registering on top part of view

我正在做一个在屏幕上绘制井字棋网格的项目。

问题是,对于 "tiles" 的底行,UITapGestureRecognizer 无法正常工作。对于网格的前两行,当识别器分配到的 UIImageView 的任何部分被点击时,将调用适当的函数。但是,对于底行,它仅在 UIImageView 的最顶部被点击时才有效。

代码如下:

func drawTiles() {
    for view in subviews {
        if (view as? UIImageView != nil) {
            view.removeFromSuperview()
        }
    }

    // Upper Left Tile [0][0]
    var view = UIImageView(image: battleGround.tiles[0][0].image)
    view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(upperLeftTapped)))
    view.isUserInteractionEnabled = true
    view.frame = CGRect(x: frame.minX, y: frame.minY, width: frame.width / 3, height: frame.height / 3)
    addSubview(view)

    // Upper Middle Tile [0][1]
    view = UIImageView(image: battleGround.tiles[0][1].image)
    view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(upperCenterTapped)))
    view.isUserInteractionEnabled = true
    view.frame = CGRect(x: frame.minX + frame.width / 3, y: frame.minY, width: frame.width / 3, height: frame.height / 3)
    addSubview(view)

    // Upper Right Tile [0][2]
    view = UIImageView(image: battleGround.tiles[0][2].image)
    view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(upperRightTapped)))
    view.isUserInteractionEnabled = true
    view.frame = CGRect(x: frame.minX + ((frame.width / 3) * 2), y: frame.minY, width: frame.width / 3, height: frame.height / 3)
    addSubview(view)



    // Middle Left Tile [1][0]
    view = UIImageView(image: battleGround.tiles[1][0].image)
    view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(middleLeftTapped)))
    view.isUserInteractionEnabled = true
    view.frame = CGRect(x: frame.minX, y: frame.minY + ((frame.height / 3)), width: frame.width / 3, height: frame.height / 3)
    addSubview(view)

    // Middle Center Tile [1][1]
    view = UIImageView(image: battleGround.tiles[1][1].image)
    view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(middleCenterTapped)))
    view.isUserInteractionEnabled = true
    view.frame = CGRect(x: frame.minX + ((frame.width / 3)), y: frame.minY + ((frame.height / 3)), width: frame.width / 3, height: frame.height / 3)
    addSubview(view)

    // Middle Center Tile [1][2]
    view = UIImageView(image: battleGround.tiles[1][2].image)
    view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(middleRightTapped)))
    view.isUserInteractionEnabled = true
    view.frame = CGRect(x: frame.minX + ((frame.width / 3) * 2), y: frame.minY + ((frame.height / 3)), width: frame.width / 3, height: frame.height / 3)
    addSubview(view)

    // FIXME: Only clicking the top of the next 3 views works

    // Lower Left Tile [2][0]
    view = UIImageView(image: battleGround.tiles[2][0].image)
    view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(lowerLeftTapped)))
    view.isUserInteractionEnabled = true
    view.frame = CGRect(x: frame.minX, y: frame.minY + ((frame.height / 3) * 2), width: frame.width / 3, height: frame.height / 3)
    addSubview(view)

    // Lower Center Tile [2][1]
    view = UIImageView(image: battleGround.tiles[2][1].image)
    view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(lowerCenterTapped)))
    view.isUserInteractionEnabled = true
    view.frame = CGRect(x: frame.minX + ((frame.width / 3)), y: frame.minY + ((frame.height / 3) * 2), width: frame.width / 3, height: frame.height / 3)
    addSubview(view)

    // Lower Right Tile [2][2]
    view = UIImageView(image: battleGround.tiles[2][2].image)
    view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(lowerRightTapped)))
    view.isUserInteractionEnabled = true
    view.frame = CGRect(x: frame.minX + ((frame.width / 3) * 2), y: frame.minY + ((frame.height / 3) * 2), width: frame.width / 3, height:frame.height / 3)
    addSubview(view)
}

func upperLeftTapped() {
    tapped(row: 0, column: 0)
}

func upperCenterTapped(){
    tapped(row: 0, column: 1)
}

func upperRightTapped() {
    tapped(row: 0, column: 2)
}

func middleLeftTapped() {
    tapped(row: 1, column: 0)
}

func middleCenterTapped() {
    tapped(row: 1, column: 1)
}
func middleRightTapped() {
    tapped(row: 1, column: 2)
}

func lowerLeftTapped() {
    tapped(row: 2, column: 0)
}

func lowerCenterTapped() {
    tapped(row: 2, column: 1)
}

func lowerRightTapped() {
    tapped(row: 2, column: 2)
}

func tapped(row: Int, column: Int) {
    if (battleGround.tiles[row][column] == .empty) {
        battleGround.tiles[row][column] = currentPlayer

        drawTiles()
    }
}

这里是要注意的代码(什么绘制了网格的下行)

// Lower Left Tile [2][0]
view = UIImageView(image: battleGround.tiles[2][0].image)
view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(lowerLeftTapped)))
view.isUserInteractionEnabled = true
view.frame = CGRect(x: frame.minX, y: frame.minY + ((frame.height / 3) * 2), width: frame.width / 3, height: frame.height / 3)
addSubview(view)

// Lower Center Tile [2][1]
view = UIImageView(image: battleGround.tiles[2][1].image)
view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(lowerCenterTapped)))
view.isUserInteractionEnabled = true
view.frame = CGRect(x: frame.minX + ((frame.width / 3)), y: frame.minY + ((frame.height / 3) * 2), width: frame.width / 3, height: frame.height / 3)
addSubview(view)

// Lower Right Tile [2][2]
view = UIImageView(image: battleGround.tiles[2][2].image)
view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(lowerRightTapped)))
view.isUserInteractionEnabled = true
view.frame = CGRect(x: frame.minX + ((frame.width / 3) * 2), y: frame.minY + ((frame.height / 3) * 2), width: frame.width / 3, height:frame.height / 3)
addSubview(view)

编辑:以下是应用程序的一些屏幕截图运行 可以更好地了解问题所在。

此屏幕截图显示了空网格的样子。

此屏幕截图显示了我单击网格的中上部和左中部图块后的网格。可以看到,图片已正确添加。

但是,如果我点击网格的底行,则没有任何反应。看看这个 GIF:

我多次点击底部中间,没有任何反应。然而在中心敲击工作正常。

这里是有趣的部分:点击同一个底部中间图块的顶部会导致图像出现。看一看:

如有任何帮助,我们将不胜感激!

使用“调试”->“查看调试”->“捕获视图层次结构”,并检查您插入所有这些图块的视图是否实际延伸到您假设的范围。

一个简单的测试也是使用self.clipsToBounds = YES; 如果您看不到所有图块,则说明您的包含视图不够大,这意味着手势无法正确转发到手势识别器。

您的视图可以与其他视图重叠。 Nailer 说的对,这个问题用View debugging 就可以看到哪个view 和你的view 重叠了,然后修复。

@Nailer 的clipToBounds 方法帮助我找到了解决方案,但没有直接揭示它。经过几周的审议,结果证明用 bounds. 替换大部分 frame. 可以解决问题。这是新代码。

func drawTiles() {
    for view in subviews {
        if (view as? UIImageView != nil) {
            view.removeFromSuperview()
        }
    }

    // Upper Left Tile [0][0]
    var view = UIImageView(image: battleGround.tiles[0][0].image)
    view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(upperLeftTapped)))
    view.isUserInteractionEnabled = true
    view.frame = CGRect(x: bounds.minX, y: bounds.minY, width: bounds.width / 3, height: bounds.height / 3)
    addSubview(view)

    // Upper Middle Tile [0][1]
    view = UIImageView(image: battleGround.tiles[0][1].image)
    view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(upperCenterTapped)))
    view.isUserInteractionEnabled = true
    view.frame = CGRect(x: bounds.minX + bounds.width / 3, y: bounds.minY, width: bounds.width / 3, height: bounds.height / 3)
    addSubview(view)

    // Upper Right Tile [0][2]
    view = UIImageView(image: battleGround.tiles[0][2].image)
    view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(upperRightTapped)))
    view.isUserInteractionEnabled = true
    view.frame = CGRect(x: bounds.minX + ((bounds.width / 3) * 2), y: bounds.minY, width: bounds.width / 3, height: bounds.height / 3)
    addSubview(view)



    // Middle Left Tile [1][0]
    view = UIImageView(image: battleGround.tiles[1][0].image)
    view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(middleLeftTapped)))
    view.isUserInteractionEnabled = true
    view.frame = CGRect(x: bounds.minX, y: bounds.minY + ((bounds.height / 3)), width: bounds.width / 3, height: bounds.height / 3)
    addSubview(view)

    // Middle Center Tile [1][1]
    view = UIImageView(image: battleGround.tiles[1][1].image)
    view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(middleCenterTapped)))
    view.isUserInteractionEnabled = true
    view.frame = CGRect(x: bounds.minX + ((bounds.width / 3)), y: bounds.minY + ((bounds.height / 3)), width: bounds.width / 3, height: bounds.height / 3)
    addSubview(view)

    // Middle Center Tile [1][2]
    view = UIImageView(image: battleGround.tiles[1][2].image)
    view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(middleRightTapped)))
    view.isUserInteractionEnabled = true
    view.frame = CGRect(x: bounds.minX + ((bounds.width / 3) * 2), y: bounds.minY + ((bounds.height / 3)), width: bounds.width / 3, height: bounds.height / 3)
    addSubview(view)

    // FIXME: Only clicking the top of the next 3 views works

    // Lower Left Tile [2][0]
    view = UIImageView(image: battleGround.tiles[2][0].image)
    view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(lowerLeftTapped)))
    view.isUserInteractionEnabled = true
    view.frame = CGRect(x: bounds.minX, y: bounds.minY + ((bounds.height / 3) * 2), width: bounds.width / 3, height: bounds.height / 3)
    addSubview(view)

    // Lower Center Tile [2][1]
    view = UIImageView(image: battleGround.tiles[2][1].image)
    view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(lowerCenterTapped)))
    view.isUserInteractionEnabled = true
    view.frame = CGRect(x: bounds.minX + ((bounds.width / 3)), y: bounds.minY + ((bounds.height / 3) * 2), width: bounds.width / 3, height: bounds.height / 3)
    addSubview(view)

    // Lower Right Tile [2][2]
    view = UIImageView(image: battleGround.tiles[2][2].image)
    view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(lowerRightTapped)))
    view.isUserInteractionEnabled = true
    view.frame = CGRect(x: bounds.minX + ((bounds.width / 3) * 2), y: bounds.minY + ((bounds.height / 3) * 2), width: bounds.width / 3, height:bounds.height / 3)
    addSubview(view)
}