Detect shake gesture IOS Swift

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-28 05:26:18

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?")
    }
}
Ryan Dines

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

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
}

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

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

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

}

swift Docs:- https://developer.apple.com/documentation/uikit/uiresponder .

To detect motion events swift has provided 3 methods:-

  1. func motionBegan() -> Tells the receiver that motion has begun

  2. func motionEnded() -> Tells the receiver that a motion event has ended

  3. func motionCancelled() -> Tells the receiver that a motion event has been cancelled

For example if you want to update image after shake gesture

class ViewController: UIViewController {
   override func motionEnded(_ motion: UIEvent.EventSubtype, with event: 
                                                                        UIEvent?) 
   {
        updateImage()
   }

   UpdateImage(){
      // Implementation
   }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!