转自

把下边的代码 放到UIVIEWCONTROLLER里,就能实现摇一摇效果

  1. #pragma mark - Shake  
  2. - (BOOL) canBecomeFirstResponder  
  3. {  
  4.     return YES;  
  5. }  
  6. - (void) viewDidAppear:(BOOL)animated  
  7. {  
  8.     [super viewDidAppear:animated];  
  9.     [self becomeFirstResponder];  
  10. }  
  11. - (void) viewWillAppear:(BOOL)animated  
  12. {  
  13.     [self resignFirstResponder];  
  14.     [super viewWillAppear:animated];  
  15. }  
  16. - (void) motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event  
  17. {  
  18.     if (motion == UIEventSubtypeMotionShake) {  
  19.         NSLog(@"Shake..........");  
  20.     }  
  21. }  


使用这段代码需要注意的是,如果ViewController不是FirstResponder的话,那么当前窗口就得到摇一摇的动作。

可以将montionBegan的事件,放到UIWindos中会更好一些

 

arrow
arrow
    全站熱搜

    戮克 發表在 痞客邦 留言(0) 人氣()