2017-03-11 33 views
1

我正在開發一個應用程序來顯示二叉樹。UIView Swift中的方向更改

每個節點將顯示爲從ViewController以編程方式生成的子視圖 - 我從viewDidLayoutSubviews()運行以下內容。

let theView = BinaryTreeView(frame: CGRect(x: 0, y: 50, width: width, height: 100)) 
    // let theView = BinaryTreeView(s: "I'm testing") 
    theView.backgroundColor = .white 
    theView.addGestureRecognizer(UIPinchGestureRecognizer(target:theView, action:#selector(BinaryTreeView.changeScale(recognizer:)))) 
    self.view.addSubview(theView) 
    theView.eyesOpen = false 


    let secondView = BinaryTreeView(frame: CGRect(x: width/2, y: 150, width: width/2, height: 100)) 
    // let theView = BinaryTreeView(s: "I'm testing") 
    secondView.backgroundColor = .white 
    secondView.addGestureRecognizer(UIPinchGestureRecognizer(target:secondView, action:#selector(BinaryTreeView.changeScale(recognizer:)))) 
    self.view.addSubview(secondView) 


    let thirdView = BinaryTreeView(frame: CGRect(x: (width/2)+width/4, y: 250, width: width/4, height: 100)) 
    // let theView = BinaryTreeView(s: "I'm testing") 
    thirdView.backgroundColor = .white 
    thirdView.addGestureRecognizer(UIPinchGestureRecognizer(target:thirdView, action:#selector(BinaryTreeView.changeScale(recognizer:)))) 
    self.view.addSubview(thirdView) 

的問題是,在方向改變的觀點重複對方(上面有三個節點,對方向變化4可能會顯示

我通過堆棧和在我的我加子類的UIView看:

self.contentMode = UIViewContentMode.redraw 

內程序化的子視圖,但同樣的情況

別擔心 - 我打算以後產生我在一個循環中的節點(我試圖瞭解佈局如何工作)。順便說一句,我發現使用UICollectionView發生的情況相同,所以我似乎在做一些根本性錯誤。

回答

1

將三個「目標:」更改爲控制器(自己)。你的控制器會迴應手勢,而不是視圖本身。在這三種情況下,目標都是一樣的。

secondView.addGestureRecognizer(UIPinchGestureRecognizer(target:secondView, action:#selector(BinaryTreeView.changeScale))) 
    self.view.addSubview(secondView) 

成爲

secondView.addGestureRecognizer(UIPinchGestureRecognizer(target:self, action:#selector(BinaryTreeView.changeScale))) 
    self.view.addSubview(secondView) 
+0

感謝 - 但舉手投足都工作正常。問題是關於方向改變和對象不能正確再生。 – stevenpcurtis

+0

你說你在取向變化時獲得了額外的物體。那是因爲你通過鏈接你的目標來鏈接你的識別器。你有沒有嘗試將目標全部替換爲3?行爲改變了嗎? – Mozahler

+0

如果我改變自我,然後點擊一個對象,我會崩潰。無法識別的選擇器發送到實例。 – stevenpcurtis