Fork me on GitHub

[iOS] 摇动检测

1. Add/Override canBecomeFirstResponderviewDidAppear: and viewWillDisappear: methods in your View Controller (这个View controller必须是顶层的):

-(BOOL)canBecomeFirstResponder 

{
    return YES;
}

-(void)viewDidAppear:(BOOL)animated

{
    [super viewDidAppear:animated];
    [self becomeFirstResponder];
}

- (void)viewWillDisappear:(BOOL)animated 

{
    [self resignFirstResponder];
    [super viewWillDisappear:animated];
}

2. Add the motionEnded or motionBegan method to your View Controller (模拟器中响应motionBegan, motionEnded,但真实机器iphone4中只能响应motionBegan):
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    if (motion == UIEventSubtypeMotionShake)
    {
        UIAlertView *servicesDisabledAlert = [[UIAlertView alloc] initWithTitle:@"title" message:@"this is a test." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [servicesDisabledAlert show];
        [servicesDisabledAlert release];

    }
}

posted on 2012-02-08 17:31  pengyingh  阅读(359)  评论(0)    收藏  举报

导航