0
想知道爲什麼當我將WhiteDot拖到SmallDot上時,它會向我的UITextField添加多於1個?它每次運行iPhone模擬器都有所不同。有時會增加11,2,3,5等等。不知道爲什麼? 另一個問題是,當SmallDot消失時,我也希望它在指定的視圖中產卵。分數在右上角。UITextField添加多於1,隨機產生
class SecondViewController: UIViewController {
private var AddOne = 0
@IBOutlet weak var Score: UITextField!
}
@IBAction func handlePan(recognizer:UIPanGestureRecognizer) {
let translation = recognizer.translation(in: self.view)
if let view = recognizer.view {
view.center = CGPoint(x:view.center.x + translation.x,
y:view.center.y + translation.y)
}
recognizer.setTranslation(CGPoint.zero, in: self.view)
if (WhiteDot.frame.contains(SmallDot.frame)) {
SmallDot.image = nil;
AddOne += 1
Score.text = "\(AddOne)"
}
}
}
在Duncan的回答中展開,你應該嘗試使用手勢的'state'屬性('UIGestureRecognizerState')。 – n00bProgrammer