几行代码实现iOS摇一摇功能

  实现这个功能很简单,我们直接看代码

  

- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event{
    NSLog(@"开始摇一摇");
}

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event{
    NSLog(@"结束摇一摇");
    UIAlertController *alterVC = [UIAlertController alertControllerWithTitle:@"你摇我干啥类" message:nil preferredStyle:UIAlertControllerStyleAlert];
    [alterVC addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil]];
    [self presentViewController:alterVC animated:YES completion:nil];
}

- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event{
    NSLog(@"取消摇一摇");
}

  主要就靠这三行代码实现这个功能。

  把这三行代码放入你需要实现功能的控制器,并且使该控制器成为第一响应者 。

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self resignFirstResponder];
}

  开始摇一摇,摇一摇结束,摇一摇被取消。

  开始摇一摇之后,不论摇多少次,只会出现一次摇一摇结束。摇一摇取消,这个很难调试出来,估计就是在你来电话,或者其他一些事的时候,跳出

猜你喜欢

转载自www.cnblogs.com/muzhou/p/9224232.html