手機搖一搖功能算是智能手機里面比較屌的功能了,一直想把它加到自己的工程里须误,正好公司的項目有機會帮辟。再次做一下最簡單的敘述蒙保。
#pragma mark --搖一搖功能
//讓當(dāng)前控制器成為第一響應(yīng)者蠢古,只有這樣才能接收事件奴曙,所以此段代碼必須加到控制器中
- (BOOL)canBecomeFirstResponder
{
return YES;// default is NO
}
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
NSLog(@"開始搖動手機");
}
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
NSLog(@"搖一搖成功");
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"溫馨提示" message:@"MDZZ,搖個蛋蛋" preferredStyle:UIAlertControllerStyleAlert];
__weak typeof(alert) weakAlert = alert;
[alert addAction:[UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
NSLog(@"%@",[weakAlert.textFields.firstObject text]);
}]];
[alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
[self presentViewController:alert animated:YES completion:nil];
}
- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
NSLog(@"取消");
}