Detect shake gesture IOS Swift

detect shake motion apple documentation
shake gesture ios simulator
motion detection ios swift
objective-c shake gesture
appdelegate detect shake
iphone shake gesture
shake gesture android
ios simulator shake gesture shortcut

I'm developing a app with a gesture system, basically if I turn the iPhone to left my app will do a function, if I turn the iPhone to Right, other function, with others gestures.

I don't have idea how to work with that, i'm trying search in google but not work, the result is only touch gesture and not motion gesture.

someone have a tutorial to help me?

Swift3 ios10:

override func viewDidLoad() {
    super.viewDidLoad()
    self.becomeFirstResponder() // To get shake gesture
}

// We are willing to become first responder to get shake motion
override var canBecomeFirstResponder: Bool {
    get {
        return true
    }
}

// Enable detection of shake motion
override func motionEnded(_ motion: UIEventSubtype, with event: UIEvent?) {
    if motion == .motionShake {
        print("Why are you shaking me?")
    }
}

Detect shake gesture IOS Swift, Swift3 ios10: override func viewDidLoad() { super.viewDidLoad() self.​becomeFirstResponder() // To get shake gesture } // We are willing to  To detect a shake gesture in iOS UIKit provides three different methods, let’s see them one by one.Method 1: When the shake gesture begins.override func motio

Super easy to implement:

1) Let iOS know which view controller is the first in the responder chain:

override func viewDidLoad() {
    super.viewDidLoad()
    self.becomeFirstResponder()
}   
override func canBecomeFirstResponder() -> Bool {
    return true
}

2) Handle the event in some fashion:

override func motionEnded(motion: UIEventSubtype, withEvent event: UIEvent?) {
    if(event.subtype == UIEventSubtype.MotionShake) {
        print("You shook me, now what")
    }
}

Detect Shake Gestures iOS Tutorial, iOS devices have the ability to detect shake gestures. Open the Assistant Editor and make sure the ViewController.swift file is visible. How to detect shake gestures in SwiftUI. Step 1 : Create a new file and name it ShakeGestureManager.swift. Make sure you import the SwiftUI and the Combine framework. import SwiftUI Step 2 : Inside this file, you need to create a PassthroughSubject that is part of the Combine framework. By using

Swift 5

Just add following methods to ViewController and execute the code

override func becomeFirstResponder() -> Bool {
    return true
}

override func motionEnded(_ motion: UIEvent.EventSubtype, with event: UIEvent?){
    if motion == .motionShake {
        print("Shake Gesture Detected")
        //show some alert here
    }
}

How to respond to the device being shaken, An event-subtype constant indicating the kind of motion. A common motion is shaking, which is indicated by UIEvent.EventSubtype.motionShake . event. iOS devices have the ability to detect shake gestures. In this tutorial we will show how to detect a shake gesture, and when detected a label text will be updated. This tutorial is made with Xcode 10 and built for iOS 12.

This has been updated for IOS 12 - see Apple Docs You no longer need to add becomeFirstResponder() method.

You just need to add motionEnded function to your code.

override func motionEnded(_ motion: UIEvent.EventSubtype, with event: UIEvent?) 
{
    // code here
}

motionEnded(_:with:), How to detect shake gestures in a UIViewController. ViewController.m. @​implementation ViewController. #pragma mark - UIResponder. In apple's documentation, I did not find any gesture related to shake for swiftUI. So how to detect it? I am very new to swift programming and this question really bothers me for a long time. In UIKit If I want to detect shake gesture is quite simple and straight forward. In swiftUI, there are bunch of gestures like tap drag rotation, however I

Swift 5

class MyViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        self.becomeFirstResponder()
    }

    override var canBecomeFirstResponder: Bool {
        get {
            return true
        }
    }

    override func motionEnded(_ motion: UIEvent.EventSubtype, with event: UIEvent?) {
        if motion == .motionShake {
            print("shake")
        }
    }

}

How to detect Shake Gesture in iOS - Xcode Swift Tutorial, I know how to detect shake gesture. It can be detect by following procedure : Open your 'AppDelegate.m' and add the code below to the application:didFinishLaunchingWithOptions: method.

IOS 11+, Swift 4+, Beginners, Tutorial : Detect Shake Gesture , An event-subtype constant indicating the kind of motion. A common motion is shaking, which is indicated by UIEvent.Event Subtype.motion Shake.

How To Use The Shake Gesture Recogniser In Xcode 8 (Swift 3.0 , A swipe gesture occurs when the user moves one or more fingers across the screen in a specific horizontal or vertical direction. Use the UISwipe Gesture Recognizer class to detect swipe gestures. You can attach a gesture recognizer in one of these ways:

How To Make an iPhone App - Ep 16, Learn how to detect shake gesture. ZOOM TUTORIAL 2020 | How To Use Zoom STEP BY STEP For Beginners! [COMPLETE GUIDE] - Duration: 12:53. Naturalvita Tutorials Recommended for you

Comments
  • I suggest you are looking for Motion Events: developer.apple.com/library/ios/documentation/EventHandling/…
  • Good for Swift 4 and iOS 11!
  • Isn't resigning first responder required? Like shown in the last part of this answer: stackoverflow.com/a/47449094/897007
  • Shouldn't you override canBecomeFirstResponder instead of becomeFirstResponder? This wasn't working for me until i changed that to canBecomeFirstResponder.
  • Please provide proof or link to documentation about becomeFirstResponder.
  • This only worked for me. Returning canBecomeFirstResponder as true did the trick.