456
技術社區[雲棲]
IOS搖一搖功能實現
#pragma mark - 生命周期函數
- (void)viewDidLoad {
[super viewDidLoad];
_shouldShowClearHistory = YES;
_tableView.height -= 49;
[self addTableHeaderViewAndFooterView];
// 設置允許搖一搖功能
[UIApplication sharedApplication].applicationSupportsShakeToEdit = YES;
// 並讓自己成為第一響應者
[self becomeFirstResponder];
return;
}
1、在viewDidLoad裏添加這兩行代碼,支持搖一搖功能
#pragma mark - 搖一搖相關方法
// 搖一搖開始搖動
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event {
NSLog(@"開始搖動");
return;
}
// 搖一搖取消搖動
- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event {
NSLog(@"取消搖動");
return;
}
// 搖一搖搖動結束
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
if (event.subtype == UIEventSubtypeMotionShake) { // 判斷是否是搖動結束
NSLog(@"搖動結束");
}
return;
}
2、實現上麵這幾個搖一搖檢測方法
最後更新:2017-04-03 05:39:42