第一种:
第一步:在AppDelegate中设置如下:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOp
{
}
第二步:在相应的viewController中添加相应的代码如下:
-(BOOL)canBecomeFirstResponder {
}
-(void)viewDidAppear:(BOOL)animated {
}
- (void)viewWillDisappear:(BOOL)animated {
}
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
}
在模拟器中测试晃动,按组合键:Ctrl + Win + Z
第二种:
利用UIAccelerometer加速器来检测,代码如下:
- (void)viewDidLoad
{
}
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceletration *)acceleration
{
if(fabsf(acceleration.x)>2.0||fabsf(acceleration.y>2.0)||fabsf(acceleration.z)>2.0)
}