首先在App's Delegate中设定applicationSupportsShakeToEdit属性:

    - (void)applicationDidFinishLaunching:(UIApplication *)application {

        application.applicationSupportsShakeToEdit = YES;

        [window addSubview:viewController.view];
        [window makeKeyAndVisible];
}

然后在你的View控制器中添加/重载canBecomeFirstResponder, viewDidAppear:以及viewWillDisappear:

-(BOOL)canBecomeFirstResponder {
    return YES;
}

-(void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self becomeFirstResponder];
}

- (void)viewWillDisappear:(BOOL)animated {
    [self resignFirstResponder];
    [super viewWillDisappear:animated];
}


最后在你的view控制器中添加motionEnded:

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    if (motion == UIEventSubtypeMotionShake)
    {
        // your code
        NSLog(@"end animations");
    }
}

- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event{

    if (motion == UIEventSubtypeMotionShake)
    {
        // your code
        NSLog(@"begin animations");
    }
}

 

相关文章:

  • 2022-02-02
  • 2021-05-31
  • 2021-10-14
  • 2022-03-10
  • 2021-08-11
  • 2022-12-23
猜你喜欢
  • 2021-10-23
  • 2022-12-23
  • 2022-12-23
  • 2022-03-01
  • 2021-12-11
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案