2016-11-15 73 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) 
     } 
    }