检测摇动手势IOS Swift

时间:2015-11-03 15:56:04

标签: ios iphone xcode swift gesture

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

我不知道如何使用它,我正在谷歌搜索但不工作,结果只是触摸手势而不是动作手势。

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

8 个答案:

答案 0 :(得分:53)

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

答案 1 :(得分:16)

超级易于实施:

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

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

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

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

答案 2 :(得分:4)

此内容已针对IOS 12更新-请参见Apple Docs 您不再需要添加beginFirstResponder()方法。

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

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

答案 3 :(得分:1)

Speedy99的快速回答是在Objective C版本中

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

答案 4 :(得分:1)

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

}

答案 5 :(得分:0)

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

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

  1. func motionBegan()->告诉接收者运动已经开始

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

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

例如,如果您要在摇动手势后更新图像

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

   UpdateImage(){
      // Implementation
   }
}

答案 6 :(得分:0)

快速5

只需将以下方法添加到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
      }
 }

答案 7 :(得分:0)

迅速4、5
iOS 10 +

UIApplicationUIViewControllerUIViewUIWindow在默认情况下都是UIResponder对象,这意味着它们都可以处理运动事件,例如震动,而无需任何特殊配置。因此,只需覆盖适当的运动方法:

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

此外,您可以覆盖motionBegan(_:with:)motionCancelled(_:with:)。与覆盖某些触摸事件不同,无需调用超级。