2011-01-26 54 views
10

我使用摇API这样的:目标C:检测抖动

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event 
{ 
    if (event.subtype == UIEventSubtypeMotionShake) 
    { 
     [img stopAnimating];  
    } 
} 

如何检测到晃动停止?

回答

22

你是在正确的轨道上,但是,仍然有你需要添加检测震动更多的东西:

您可以通过添加NSLogmotionBeganmotionEnded方法进行测试,并在模拟器,按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 
    } 
} 
+1

完美!非常感谢... – itsame69 2012-10-22 07:56:02