用户登录
用户注册

分享至

iOS晃动检测

  • 作者: 英雄赵一曼
  • 来源: 51数据库
  • 2022-08-17

1、在AppDelegate.h中进行如下设置:

1.     -(BOOL)application:(UIApplication *)applicationdidFinishLaunchingWithOptions:(NSDictionary *)launchOptions

2.     {

3.        application.applicationSupportsShakeToEdit = YES;

4.     }

 

2、在你需要对晃动事件进行处理的ViewController中添加如下代码:

1.     -(BOOL)canBecomeFirstResponder{

2.        return YES;

3.     }

4.      

5.     -(void)viewDidAppear:(BOOL)animated{

6.        [super viewDidAppear:animated];

7.        [self becomeFirstResponder];

8.     }

9.      

10.    - (void)viewWillDisappear:(BOOL)animated{

11.       [self resignFirstResponder];

12.       [super viewWillDisappear:animated];

13.    }

14.     

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

16.    {

17.       if (motion == UIEventSubtypeMotionShake) {

18.          

19.                 在这里写晃动时 触动的事件

20.       }

21.    }

软件
前端设计
程序设计
Java相关