2016-11-16 63 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調試來解決這個問題,在那裏你可以看到哪個視圖重疊你的視圖並修復它。

相關問題