2011-01-26 60 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