Detect shake gesture IOS Swift

帅比萌擦擦* 提交于 2019-11-27 00:45:25

问题


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?


回答1:


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



回答2:


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



回答3:


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



回答4:


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
}



回答5:


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



回答6:


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

}



回答7:


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


来源:https://stackoverflow.com/questions/33503531/detect-shake-gesture-ios-swift

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