ios 中增加获得手势 摇动

http://stackoverflow.com/questions/150446/how-do-i-detect-when-someone-shakes-an-iphone


  • Add/Override canBecomeFirstResponderviewDidAppear: andviewWillDisappear: methods in your View Controller (这个View controller必须是顶层的):


-(BOOL)canBecomeFirstResponder { return YES;
} -(void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; [self becomeFirstResponder];
} - (void)viewWillDisappear:(BOOL)animated { [self resignFirstResponder]; [super viewWillDisappear:animated];
}

  • Add the motionEnded or motionBegan method to your View Controller (模拟器中响应motionBegan, motionEnded,但真实机器iphone4中只能响应motionBegan):


- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{ if (motion == UIEventSubtypeMotionShake) { // your code }
}


你可能感兴趣的:(ios 中增加获得手势 摇动)