废话不多说,直接贴代码。关键代码如下

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self becomeFirstResponder];
}
- (void) viewWillAppear:(BOOL)animated
{
    [self resignFirstResponder];
    [super viewWillAppear:animated];    
}
-(BOOL)canBecomeFirstResponder 
{
    return YES;
}
- (void) motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{

    if (motion == UIEventSubtypeMotionShake) {
        NSLog(@"摇一摇"); 
    }    
}

另外值得一提的是,在模拟器中运行时,可以通过「Hardware」-「Shake Gesture」来测试「摇一摇」功能。

请输入图片描述


wall2flower
84 声望7 粉丝

开始刷版本号