52

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?

FelipeRsN
  • 887
  • 1
  • 9
  • 19
  • I suggest you are looking for Motion Events: https://developer.apple.com/library/ios/documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/motion_event_basics/motion_event_basics.html – dalipsia Nov 03 '15 at 16:00

7 Answers7

84

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: UIEvent.EventSubtype, with event: UIEvent?) {
    if motion == .motionShake {
        print("Why are you shaking me?")
    }
}
Valentin Shamardin
  • 3,569
  • 4
  • 34
  • 51
Speedy99
  • 1,691
  • 15
  • 15
26

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")
    }
}
Ric Santos
  • 15,419
  • 6
  • 50
  • 75
Ryan Dines
  • 979
  • 10
  • 18
25

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
    }
}
Hejazi
  • 16,587
  • 9
  • 52
  • 67
swiftBoy
  • 35,607
  • 26
  • 136
  • 135
  • 2
    Shouldn't you override `canBecomeFirstResponder` instead of `becomeFirstResponder`? This wasn't working for me until i changed that to `canBecomeFirstResponder`. – Herbal7ea Jan 13 '20 at 19:50
15

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
}
Howard Panton
  • 247
  • 4
  • 6
12

Swift 5
iOS 10+

UIApplication, UIViewController, UIView, and UIWindow are all UIResponder objects by default which means they can all handle motion events, like shake, without any special configuration. Therefore, simply override the appropriate motion method:

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

Additionally, you can override motionBegan(_:with:) and motionCancelled(_:with:). And unlike overriding certain touch events, there is no need to call super.

trndjc
  • 11,654
  • 3
  • 38
  • 51
4

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")
        }
    }

}
user2168735
  • 395
  • 4
  • 14
2

Speedy99's swift answer is in the Objective C version

- (void)viewDidLoad {
    [super viewDidLoad];
    [self becomeFirstResponder];
}

- (BOOL)canBecomeFirstResponder{
    return true;
}

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event{
    if(event.subtype == UIEventSubtypeMotionShake){
       NSLog(@"Why are you shaking me?");
    }
}
Prakash
  • 812
  • 6
  • 16