我通过以下代码检测摇动:
override func motionEnded(motion: UIEventSubtype, withEvent event: UIEvent) {
if motion == .MotionShake {
self.shakeLabel.text = "Text"
}
}
}
好的,它的工作。但是,当动作摇动时,我需要获得3个字符串中的随机项目。我找不到这个解决方案。
答案 0 :(得分:0)
准备一系列文本,并按照以下步骤准备:
let texts = ["text1", "text2", "text3"]
self.shakeLabel.text = texts[Int(arc4random_uniform(UInt32(texts.count)))]
摇动3次后,显示随机文字
var shakeCount = 0
let shakeTexts = ["text1", "text2", "text3"]
override func motionEnded(motion: UIEventSubtype, withEvent event: UIEvent) {
if motion == .MotionShake {
shakeCount++
if shakeCount >= 3 {
self.shakeLabel.text = shakeTexts[Int(arc4random_uniform(UInt32(shakeTexts.count)))]
}
}
}