How do I detect when someone shakes an iPhone?

后端 未结 16 2143
后悔当初
后悔当初 2020-11-22 06:41

I want to react when somebody shakes the iPhone. I don\'t particularly care how they shake it, just that it was waved vigorously about for a split second. Does anyone know h

16条回答
  •  南旧
    南旧 (楼主)
    2020-11-22 07:00

    A swiftease version based on the very first answer!

    override func motionEnded(_ motion: UIEventSubtype, with event: UIEvent?) {
        if ( event?.subtype == .motionShake )
        {
            print("stop shaking me!")
        }
    }
    

提交回复
热议问题