0

iPhoneでシェイクイベントを取得できません。

私はここで他の質問に従いましたが、結果はありません。AppleのGLPaintの例にも従おうとしましたが、ソースコードとまったく同じように見えますが、わずかな違いがあります。GLPaintのソースコード/works/、私の/ doesn't/。

だから、ここに私が持っているものがあります:

Controller.m

- (void)awakeFromNib {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(shakeEnded) name:@"shake" object:nil];
}

ShakingEnabledWindow.m

- (void)shakeEnded {
    NSLog(@"Shaking ended.");
}

- (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:@"shake" object:self];
        NSLog(@"Shaken!");
    }
}

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

私のXIBには、ShakingEnabledWindowであるウィンドウとオブジェクトである私のコントローラーがあります。

私はここでアイデアが不足しています。誰かが私に手を貸してくれることを願っています。:)

4

3 に答える 3

1

NSNotificationCenterのドキュメントによると:

addObserver:selector:name:object: notificationSelector受信者が通知の投稿を通知するためにnotificationObserverに送信するメッセージを指定するセレクター。NotificationSelectorで指定されるメソッドには、引数が1つだけである必要があります(NSNotificationのインスタンス)。

したがって、shakeEndedメソッドはパラメーターを受け取らないため、間違っています。次のようになります。

- (void)shakeEnded:(NSNotification*)notiication {
    NSLog(@"Shaking ended.");
}

- (void)awakeFromNib {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(shakeEnded:) name:@"shake" object:nil];
}
于 2011-04-03T22:09:39.407 に答える
1

viewDidAppear、ファーストレスポンダーになります。

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

そして、あなたがファーストレスポンダーになることができることを確認してください:

- (BOOL)canBecomeFirstResponder {
    return YES;
}

次に、モーション検出を実装できます。

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    if (event.subtype == UIEventTypeMotion){
        //there was motion
    }
}
于 2011-04-03T22:44:42.557 に答える
0

モーションタイプを間違ってチェックしていると思います。event.subtype代わりにチェックする必要がありますmotion

-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
    if ( event.subtype == UIEventSubtypeMotionShake ) {
        // Put in code here to handle shake
    }
}
于 2011-04-03T22:08:35.753 に答える