iOS-摇一摇实现

利用Motion Events实现摇一摇功能。

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"摇一摇";
    [[UIApplication sharedApplication] setApplicationSupportsShakeToEdit:YES];
    [self becomeFirstResponder];
}

- (void) motionEnded:(UIEventSubtype)motion withEvent:(UIEvent*)event
{
    if(event.subtype == UIEventSubtypeMotionShake) {
        NSArray *colorArray = [NSArray arrayWithObjects:[UIColor redColor], [UIColor grayColor], [UIColor greenColor], [UIColor blackColor],  [UIColor orangeColor], [UIColor yellowColor], [UIColor whiteColor], nil];
        self.view.backgroundColor = [colorArray objectAtIndex:arc4random() % colorArray.count];
        }
}

- (BOOL)canBecomeFirstResponder
{
    return YES;
}