类似微信的摇一摇功能在IOS中的实现是很简单的,下面特酷吧根据自己实践过的代码简单的做些记录。
主要的使用接口是[继承自UIWindow]在UIResponder中存在的如下:方法
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event__OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0);
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event__OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0);
- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event__OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0);
很简单,你只需要让这个Controller本身支持摇动(默认)。同时让他成为第一相应者。
下面给出具体的示例:
- (void)viewDidLoad
{
[superviewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[[UIApplication sharedApplication] setApplicationSupportsShakeToEdit:YES];//配置支持摇动
[self becomeFirstResponder];
}
//检测到摇动
- (void) motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{}
//摇动取消
- (void) motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event
{}
//摇动结束
- (void) motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
//可以执行系统震动操作-每个调用都会生成一个简短的1~2秒的震动。在不支持震动的平台上(ipod touch),该调用不执行任何操作,但也不会发生错误
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
if (event.subtype == UIEventSubtypeMotionShake) {
//something happens
}
}
网友评论