-1

我有一个 ViewController 可以完美地与触发动作的按钮配合使用。我想用一个摇动事件替换按钮,所以我用谷歌搜索它并创建了一个从 UIView 继承的 ShakeDetector 类

我的实现如下:

@implementation ShakeDetector

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

    - (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
    {
        if (motion == UIEventSubtypeMotionShake )
        {
            // User was shaking the device. Post a notification named "shake".
            //[[NSNotificationCenter defaultCenter] postNotificationName:@"spin" object:self];
            NSLog(@"sss");
        }
    }

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

@end

但我不能让它工作......有什么帮助吗?

谢谢

4

1 回答 1

1

放 :

    -(BOOL)canBecomeFirstResponder
{
    return YES;
}

并为您的观点:

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

也可以写在 viewWillAppear 和 viewWillDisappear

于 2009-10-14T21:41:20.207 回答