類(lèi)似微信的搖一搖功能在IOS中的實(shí)現(xiàn)是很簡(jiǎn)單的,下面特酷吧根據(jù)自己實(shí)踐過(guò)的代碼簡(jiǎn)單的做些記錄。
主要的使用接口是[繼承自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);
很簡(jiǎn)單,你只需要讓這個(gè)Controller本身支持搖動(dòng)(默認(rèn))。同時(shí)讓他成為第一相應(yīng)者。
下面給出具體的示例:
- (void)viewDidLoad
{
[superviewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[[UIApplication sharedApplication] setApplicationSupportsShakeToEdit:YES];//配置支持搖動(dòng)
[self becomeFirstResponder];
}
//檢測(cè)到搖動(dòng)
- (void) motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{}
//搖動(dòng)取消
- (void) motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event
{}
//搖動(dòng)結(jié)束
- (void) motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
//可以執(zhí)行系統(tǒng)震動(dòng)操作-每個(gè)調(diào)用都會(huì)生成一個(gè)簡(jiǎn)短的1~2秒的震動(dòng)。在不支持震動(dòng)的平臺(tái)上(ipod touch),該調(diào)用不執(zhí)行任何操作,但也不會(huì)發(fā)生錯(cuò)誤
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
if (event.subtype == UIEventSubtypeMotionShake) {
//something happens
}
}