我正在使用像这样的摇动api:
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
if (event.subtype == UIEventSubtypeMotionShake)
{
[img stopAnimating];
}
}
如何检测到摇晃已停止?
答案 0 :(得分:22)
你走在正确的轨道上,但是,还需要添加更多东西来检测震动:
您可以通过向NSLog
或motionBegan
方法添加motionEnded
进行测试,然后在模拟器中按CONTROL + COMMAND + Z
#pragma mark - Shake Functions
-(BOOL)canBecomeFirstResponder {
return YES;
}
-(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:NO];
[self becomeFirstResponder];
}
-(void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:NO];
}
-(void)viewDidDisappear:(BOOL)animated {
[self resignFirstResponder];
[super viewDidDisappear:NO];
}
-(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
if (motion == UIEventSubtypeMotionShake )
{
// shaking has began.
}
}
-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
if (motion == UIEventSubtypeMotionShake )
{
// shaking has ended
}
}