Detect shake gesture IOS Swift

后端 未结 8 2142
难免孤独
难免孤独 2020-12-08 02:27

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 ges

相关标签:
8条回答
  • 2020-12-08 02:54

    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?");
        }
    }
    
    0 讨论(0)
  • 2020-12-08 03:02

    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")
            }
        }
    
    }
    
    0 讨论(0)
  • 2020-12-08 03:06

    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
    }
    
    0 讨论(0)
  • 2020-12-08 03:07

    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?")
        }
    }
    
    0 讨论(0)
  • 2020-12-08 03:07

    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")
        }
    }
    
    0 讨论(0)
  • 2020-12-08 03:11

    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
       }
    }
    
    0 讨论(0)
提交回复
热议问题