ios shake手势

萝らか妹 提交于 2020-11-30 07:30:58
先说点,下面的是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(@"取消");  

}  

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!