2016-11-15 59 views

回答

0

高度簡化的例子看起來是這樣的:

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 
    guard let touch = touches.first else { return } 
    let location = touch.location(in: self) 
    let node = SKNode() 
    node.position = location 
    addChild(node) 
} 
+0

其實它的工作原理,但我只能添加一個節點,當我按兩次我得到一個錯誤。控制檯說:「終止與類型NSException的未捕獲的異常」 –

+0

忘記它,我錯過了一行代碼 –

+0

酷,一個異常將是一個非常奇怪的行爲與此代碼:) –

0

這裏是你可以通過點擊添加刪除標籤:

class ViewController: UIViewController{ 
     private var labels = [UILabel]() 
     override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { 
      guard let touch = touches.first else { 
       return 
      } 
      for label in labels { 
       if label.frame.contains(touch.location(in: view)) { 
        label.removeFromSuperview() 
        return 
       } 
      } 
      let label = UILabel() 
      view.addSubview(label) 
      labels.append(label) 
      label.text = "touch" 
      label.sizeToFit() 
      label.center = touch.location(in: view) 
     } 
    }