我正在製作一個項目,其中的一個井字格正在屏幕上繪製。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)
編輯:下面是一些截圖該應用程序可以更好地瞭解問題所在。
該截圖顯示一個空網的樣子。
該截圖顯示網格我點擊頂部中間和電網的中間偏左的瓷磚後。正如你所看到的,圖像被正確添加。
但是,如果我點擊網格的底部行,則什麼都不會發生。看看這個GIF:
我拍了拍底部中間多次的,什麼都沒有發生。然而在中心竊聽正常工作。
以下是有趣的部分:在相同的底部中部瓷磚的頂部輕敲會導致圖像出現。請看:
任何幫助將不勝感激!
正如您所說,使用'clipToBounds = true'顯示截斷點。任何想法如何我會去解決這個問題? –
理想情況下,您可以使用自動佈局來佈置使用NSLayoutConstraint的視圖。但是,您可能更容易計算超視圖的框架,並明確設置框架。 – Nailer
使用界面構建器,您也可以直接在此處創建此佈局,而不是通過編程方式構建界面。無論哪種方式,你會學到很多:) – Nailer