ios 检测摇晃手势IOS Swift

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33503531/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 08:10:46  来源:igfitidea点击:

Detect shake gesture IOS Swift

iosiphonexcodeswiftgesture

提问by FelipeRsN

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.

我正在开发一个带有手势系统的应用程序,基本上如果我将 iPhone 向左转动,我的应用程序将执行一个功能,如果我将 iPhone 向右转动,其他功能,以及其他手势。

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.

我不知道如何使用它,我正在尝试在 google 中搜索但不起作用,结果只是触摸手势而不是运动手势。

someone have a tutorial to help me?

有人有教程可以帮助我吗?

回答by Speedy99

Swift3 ios10:

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

回答by Ryan Dines

Super easy to implement:

超级容易实现:

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

1) 让 iOS 知道哪个视图控制器是响应者链中的第一个:

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

2) Handle the event in some fashion:

2)以某种方式处理事件:

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

回答by swiftBoy

Swift 5

斯威夫特 5

Just add following methods to ViewController and execute the code

只需将以下方法添加到 ViewController 并执行代码

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

回答by Howard Panton

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

这已针对 IOS 12 进行了更新 - 请参阅Apple Docs您不再需要添加 becomeFirstResponder() 方法。

You just need to add motionEnded function to your code.

您只需要在代码中添加motionEnded 函数即可。

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

回答by user2168735

Swift 5

斯威夫特 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")
        }
    }

}

回答by Prakash

Speedy99's swift answer is in the Objective C version

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

回答by Kathi Shiva Ram

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

快速文档:- https://developer.apple.com/documentation/uikit/uiresponder

To detect motion events swift has provided 3 methods:-

为了检测运动事件,swift 提供了 3 种方法:-

  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
  1. func motionBegan() -> 告诉接收者动作已经开始

  2. func motionEnded() -> 告诉接收者一个动作事件已经结束

  3. func motionCancelled() -> 告诉接收者一个动作事件已被取消

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