2013-03-05 52 views
4

我想整合應用程序中的搖動功能。所以我正在做一切在AppDelegate。我需要推一個viewController,我可以推動motionBegan,但我想要做motionEnded。 yes motion motion在視圖控制器中工作,但在應用程序委託中它沒有被調用。 做的motionDefined未在appDelegate中調用

- (void)applicationDidBecomeActive:(UIApplication *)application { 
    [self becomeFirstResponder]; 
} 
- (BOOL)canBecomeFirstResponder{ 
    return YES; 
} 

motionEnded不叫

-(void) motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event { 
    if(event.subtype==UIEventSubtypeMotionShake){ 
     NSLog(@"motionEnded called"); 
    } 
} 

motionBegan稱爲

-(void) motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event { 
    if(event.subtype==UIEventSubtypeMotionShake){ 
     NSLog(@"motionBegan called"); 
    } 
} 
+0

發佈通知可以幫助我猜 – nsgulliver 2013-03-05 10:48:19

+0

我避難所ot那麼多的通知,我嘗試了applicationDidBecomeActivie中的addedObserver「DeviceShaken」,但沒有收穫 – 2013-03-05 12:11:49

+0

你解決了你的問題嗎?回答有用嗎? – nsgulliver 2013-03-05 16:51:55

回答

1

你基本上可以註冊viewControllerapplicationDidBecomeActiveNotification或任何根據您的需要

例如你的viewControllerviewDidLoad方法,你可以註冊它的通知

[[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(myMethod) 
               name:UIApplicationDidBecomeActiveNotification object:nil]; 

,並在你的類實現此方法,您myMethod將調用每次你的應用程序將變得活躍

-(void) myMethod(){ 
// do your stuff 
} 

終於從註銷的viewController通知dealloc方法

-(void)dealloc{ 
[[NSNotificationCenter defaultCenter] removeObserver:self]; 
} 
相關問題