2016-11-16 109 views
1

我正在制作一个项目,其中的一个井字格正在屏幕上绘制。iOS UIImageView水龙头只在视图顶部注册

问题是,对于“瓷砖”的底部行,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) 

编辑:下面是一些截图该应用程序可以更好地了解问题所在。

image1

该截图显示一个空网的样子。

image2

该截图显示网格我点击顶部中间和电网的中间偏左的瓷砖后。正如你所看到的,图像被正确添加。

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

gif1

我拍了拍底部中间多次的,什么都没有发生。然而在中心窃听正常工作。

以下是有趣的部分:在相同的底部中部瓷砖的顶部轻敲会导致图像出现。请看:

enter image description here

任何帮助将不胜感激!

回答

0

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

使用调试 - >查看调试 - >捕获视图层次结构,并检查您将所有这些瓷砖插入到实际拉伸的视图,只要你认为它正在做。

一个简单的测试也是使用self.clipsToBounds = YES; 如果你不能看到所有的瓷砖,你的看法含有不够大,这意味着手势就不能正确地转发到手势识别。

+0

正如您所说,使用'clipToBounds = true'显示截断点。任何想法如何我会去解决这个问题? –

+0

理想情况下,您可以使用自动布局来布置使用NSLayoutConstraint的视图。但是,您可能更容易计算超视图的框架,并明确设置框架。 – Nailer

+0

使用界面构建器,您也可以直接在此处创建此布局,而不是通过编程方式构建界面。无论哪种方式,你会学到很多:) – Nailer

0

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

相关问题