IOS中实现设备摇动检测
這些方法都是在網(wǎng)上看到的,我自己經(jīng)過(guò)測(cè)試的就這2種;
1.UIWindow(已測(cè)試)
?- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
}
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
?? ?if (motion == UIEventSubtypeMotionShake )
?? ?{
?? ??? ?//這里可以促發(fā)系統(tǒng)震動(dòng),或者發(fā)送一個(gè)通知,進(jìn)行相應(yīng)的處理
?? ?}
}
- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event
{?? ?
}
2. UIViewController(繼承自UIResponder - 已測(cè)試)
-(BOOL)canBecomeFirstResponder {
? ? returnself.shakeSyncIsOn;//這里是self擁有的一個(gè)變量,通過(guò)這個(gè)變量來(lái)設(shè)定知否支持搖動(dòng)
}
-(void)viewDidAppear:(BOOL)animated {
? ? [super viewDidAppear:animated];
? ? if ([selfcanBecomeFirstResponder]==YES)
? ? {
? ? ? ? NSLog(@"becomeFirstResponder:YES");
//下面這句代碼,不一定非得寫(xiě)到viewDidLoad或者applicationDidFinishLaunch中,這里也可以
? ? ? ? [[UIApplicationsharedApplication] setApplicationSupportsShakeToEdit:YES];
? ? ? ? [selfbecomeFirstResponder];
? ? }
? ? else{
? ? ? ? NSLog(@"becomeFirstResponder:NO");
? ? }
}
- (void)viewWillDisappear:(BOOL)animated {
? ? [selfresignFirstResponder];
? ? [super viewWillDisappear:animated];
}
?
#pragma mark -?
#pragma mark - shakeToSync: UIResponder support motion
//開(kāi)始搖動(dòng)檢測(cè)
-(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
? ? NSLog(@"====== Shake Begins =======");
}
-(void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
? ? NSLog(@"======= Shake Canceled =======");
}
-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
? ? ? ?if (motion == UIEventSubtypeMotionShake)
? ? ? {
? ? ? ? ? ?NSLog(@"======== Shake End ========");
//這里可以添加震動(dòng)反饋,或者發(fā)送一個(gè)通知
//AudioToolBox.framework
//#import?"AudioToolbox/AudioToolbox.h"?AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
? ? ? }
}
?
轉(zhuǎn)載于:https://www.cnblogs.com/traximus/archive/2013/02/28/2937009.html
總結(jié)
以上是生活随笔為你收集整理的IOS中实现设备摇动检测的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 非法ip通过ssh成功登录,自动结束会话
- 下一篇: Linux下使用SSH、Crontab、