3

I have used collectionView and also implemented pull to sync (pull_Down_To_Get_Data_From_Server) in my ViewController(named : "DashboardViewController"). I tried the shake gesture code(given below) in my DashboardViewController and its not working but similar code is working in another viewController of same app.

I used the canBecomeFirstResponder in viewDidLoad in first try and in viewDidAppear secondly but it was also useless.

Requirement: Actualy i want to know the cases(if any) in which ".motionShake" event do not trigger directly and we have to implement another way? Also that i did not find anything fruitful on google. Image of storyboard may help in understanding all the utilities used

override func viewDidLoad() {
 // call super
    super.viewDidLoad()
    self.becomeFirstResponder() // for shake gesture detection
}

// it will make first responder to detect shake motion event
override var canBecomeFirstResponder: Bool {
    get {
        return true
    }
}

// get detection of shake motion event
override func motionEnded(_ motion: UIEventSubtype, with event: UIEvent?) {
    if motion == .motionShake 
    {
        print("Shake detected")
    }
}
Ummar Ahmed
  • 135
  • 1
  • 11
  • It appears your code was copied from https://stackoverflow.com/a/43151351/1226963 – rmaddy Aug 11 '17 at 06:26
  • Actually this code is use to get the shake gesture motion and i used it for my own sake and the reference you given using this code for his purpose. Code might be similar because it is provided for use but the difference is of approach for which we are use it @rmaddy – Ummar Ahmed Aug 11 '17 at 06:48

1 Answers1

2

We ran into something similar while working on Yoshi

We ended up creating an extension on UIWindow, that contained the motionBegan event and forwarded the event. Not sure what caused it, but it seemingly broke with swift 3.0.

Campbell_Souped
  • 871
  • 10
  • 22
  • I like this answer. I don't know why, but I haven't had any success implementing the `motionBegan` function at the view controller level (an older codebase; Swift 2.3 in Xcode 8). I implemented it on UIWindow in an extension and I post a notification and it works well. – Tim Fuqua Mar 28 '18 at 22:13