您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 訊息 >> iOS 相關資訊

iOS 搖一搖功能實作

編輯:iOS 相關資訊

iOS 的搖一搖可透過 motionBegan:withEvent: 取得。若 App 沒有收到事件,可先確認 view controller 是否能成為 first responder。

- (BOOL)canBecomeFirstResponder {
    return YES;
}

- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event {
    if (motion == UIEventSubtypeMotionShake) {
        NSLog(@"shake detected");
    }
}

若需要在多個頁面共用搖一搖事件,可在 AppDelegate 中攔截 motionBegan,再透過 NSNotificationCenter 發送通知;各頁面只需註冊通知並執行對應 action。

[[NSNotificationCenter defaultCenter] postNotificationName:@"shake" object:self];

這種寫法適合全域搖一搖,例如切換背景、刷新資料或觸發全域動畫。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved