摇动检测iPhone 3.0无法正常工作

时间:2009-10-10 12:23:37

标签: iphone events triggers detection shake

我有一个ViewController,可以与触发动作的按钮完美配合。我想用摇动事件替换按钮,所以我用Google搜索并创建了一个ShakeDetector类,它来自UIView

我的实施如下:

@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

但我不能让它发挥作用......任何帮助?

由于

1 个答案:

答案 0 :(得分: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

中编写它