-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 に答える