iOS UIImageView点击仅在视图顶部注册

iOS UIImageView点击仅在视图顶部注册,ios,swift,imessage,imessage-extension,Ios,Swift,Imessage,Imessage Extension,我正在做一个项目,在这个项目中,屏幕上正在绘制一个Tic-Tac-Toe网格 问题在于,对于底部一行的“tiles”,UITapgestureRecognitor工作不正常。对于网格顶部的两行,当点击识别器分配到的UIImageView的任何部分时,将调用相应的函数。但是,对于最下面一行,它仅在点击UIImageView的最顶部时才起作用 代码如下: func drawTiles() { for view in subviews { if (view as? UIImag

我正在做一个项目,在这个项目中,屏幕上正在绘制一个Tic-Tac-Toe网格

问题在于,对于底部一行的“tiles”,UITapgestureRecognitor工作不正常。对于网格顶部的两行,当点击识别器分配到的
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()
    }
}
下面是需要注意的代码(绘制网格下一行的内容)

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

此屏幕截图显示了空网格的外观

此屏幕截图显示了我单击网格中上和左中分幅后的网格。如您所见,图像已正确添加

但是,如果我点击网格的底部一行,什么也不会发生。看看这个GIF:

我在底部中间敲了几下,什么也没发生。然而,在中心的攻丝工作正常

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


任何帮助都将不胜感激

使用Debug->View Debug->Capture View Hierarchy,并检查要插入所有这些平铺的视图是否实际延伸到您假设的范围

一个简单的测试也是使用
self.clipsToBounds=YES

如果无法看到所有的互动程序,则表示包含的视图不够大,这意味着手势无法正确转发到手势识别器。

您的视图可能与其他视图重叠。Naller是对的,只需使用视图调试就可以解决这个问题,在那里你可以看到哪个视图与你的视图重叠并修复它。

@Naller的
clipToBounds
方法帮助我找到了解决方案,但没有完全暴露出来。经过几周的考虑,用
边界替换大部分
框架。
解决了这个问题。这是新代码

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)
}

正如您所说,使用
clipToBounds=true
显示截止点。知道我将如何解决这个问题吗?理想情况下,您可以使用自动布局来使用NSLayoutConstraint来布局视图。但是,计算superview的框架并显式设置框架可能更容易。使用interface builder,您也可以直接在此处进行布局,而不是通过编程方式构建界面。无论哪种方式,您都会学到很多:)
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)
}