手机摇一摇功能算是智能手机里面比较屌的功能了,一直想把它加到自己的工程里,正好公司的项目有机会。再次做一下最简单的叙述。
#pragma mark --摇一摇功能
//让当前控制器成为第一响应者,只有这样才能接收事件,所以此段代码必须加到控制器中
- (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(@"取消");
}