簡體   English   中英

搖動手勢如何在iOS中工作?

[英]How the shake gesture works in iOS?

我知道如何檢測shake gesture 可以通過以下步驟進行檢測:

打開您的“ AppDelegate.m ”,然后將以下代碼添加到application:didFinishLaunchingWithOptions:方法。

您必須在“ return YES ”之前添加它。

[application setApplicationSupportsShakeToEdit:YES];

現在,打開要在其中使用shake gesturecontrollerviewController.m ”文件。 編輯viewDidAppear:viewDidDisappear:方法。

添加以下代碼:

- (void)viewDidAppear:(BOOL)animated {

    [self becomeFirstResponder];
}

- (void)viewDidDisappear:(BOOL)animated {

    [self becomeFirstResponder];
}

之后,您應該在下面的代碼中添加方法:

- (BOOL)canBecomeFirstResponder {
    return YES;
}

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
    // Do your thing after shaking device
}

但是我想知道邏輯搖動的原理。

如何檢測到它? 是否使用CoreMotionaccelerometermagnetometergyroscope )進行檢測?

如果有人知道,請分享。

這取決於您擁有的設備,但是Core Motion確實是正確的答案。

設備通常有兩種方式來檢測運動,即加速度計和陀螺儀。
並非所有的iOS設備都具有陀螺儀。 無論如何,Core Motion啟用后都會跟蹤設備,並允許您從一個或兩個中進行采樣。

查看Apple的文檔以獲得更詳細的了解。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM