iOS 摇一摇事件

我知道的摇一摇有如下2种方案:post

1、直接用系统自带的motionBegan方法server

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

假如程序不响应此方法,试着加入下面方法:事件

-(BOOL)canBecomeFirstResponderget

{it

  return YES;io

}event

若是还不行,建议用第二种方法。class


2、motionBegan+通知的方法object

1.在Appdelegate里写motionBegan方法

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

{

[[NSNotificationCenter defaultCenter]postNotificationName:@"shake" object:self];

}

2.在须要接收通知的页面添加通知

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(shakeAction) name:@"shake" object:nil];

写在viewDidLoad里便可。

这里的shakeAction就是摇一摇须要调用的方法,本身修改,通知名字对应就好,可自由修改。

三.补充摇一摇的其余方法

/** 摇一摇结束(须要在这里处理结束后的代码) */
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    // 不是摇一摇运动事件
    if (motion != UIEventSubtypeMotionShake) return;
    
    NSLog(@"motionEnded");
}

/** 摇一摇取消(被中断,好比忽然来电) */
- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    NSLog(@"motionCancelled");
}

相关文章
相关标签/搜索