ios shake手势

先说点,下面的是IOS7 后的方法,but 如果向下兼容IOS6 的话,也是可以的,不过需要加上这个方法(因为IOS7可以不用写): -( BOOL )canBecomeFirstResponder

The following code shows how to implement the shake gesture in iOS. The most important thing is to make sure the target view to be set as the first responder in Window. In UIView, shake gesture mainly has three functions as below:

- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event

- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event


Sample Code:


-(BOOL)canBecomeFirstResponder

{

    return YES// default is NO

}


- (void)viewDidAppear:(BOOL)animated

{

    [super viewDidAppear:animated];

    [self becomeFirstResponder];

}


- (void)viewWillDisappear:(BOOL)animated

{    

    [self resignFirstResponder];

    [super viewWillDisappear:animated];

}


- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event  

{  

NSLog(@"开始摇动手机");  

}  

- (void) motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {

            

    if (motion == UIEventSubtypeMotionShake){

        

        //Your code here...

    }

 

- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event {  

NSLog(@"取消");  

}  

你可能感兴趣的:(shake,摇一摇)