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

iOS 之搖一搖手勢實作

編輯:iOS 相關資訊

UIResponder 中存在以下三個方法,正是用來處理搖一搖(shake)手勢的:

- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event;
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event;
- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event;

使用方式很簡單,只要讓該 Controller 支援搖動,並成為第一響應者即可:

- (void)viewDidLoad
{
    [super viewDidLoad];
    [[UIApplication sharedApplication] setApplicationSupportsShakeToEdit:YES];
    [self becomeFirstResponder];
}

接著實作上述方法即可:

- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    // 偵測到搖動
}

- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    // 搖動取消
}

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    // 搖動結束
    if (event.subtype == UIEventSubtypeMotionShake) {
        // 在這裡處理搖一搖後的行為
    }
}

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