IOS 檢測搖晃 幾個問題

 IOS3.0以後可以用如下

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

}

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

}

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

}

這三個方法檢測手機搖晃事件。

我說兩個要注意的地方

1.要實現搖晃的view或者viewController一定要添加

-(BOOL)canBecomeFirstResponder{

    return YES;

}

注意不是-(BOOL)becomeFirstResponder;這個方法。

 

2.在這個view的viewController中添加[view becomeFirstResponder]; 或者在viewController添加

[self becomeFirstResponder];

注意了以上這段代碼必須添加在

-(void)viewDidAppear:(BOOL)animated{

    [super viewDidAppear:animated];

    [self becomeFirstResponder];

}

這裏

(添加在其他地方就無效了)

本人一開始添加在如下里

-(void)viewWillAppear:(BOOL)animated{

    [super viewWillAppear:animated];

}

添加這裏看似差不多,其實差遠了完全接收不到搖晃事件。

就這麼多大家注意一下吧。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章