2012-04-14 12 views
21

ho aggiunto il seguente codice alla mia appDelegate.miOS: come rilevare il movimento dell'oscillazione?

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

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event 
{ 
    if (motion == UIEventSubtypeMotionShake) 
    { 
     // User was shaking the device. Post a notification named "shake". 
     [[NSNotificationCenter defaultCenter] postNotificationName:@"shake" object:self]; 
    } 
} 

- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event 
{ 
} 

- (void)shakeSuccess 
{ 
    // do something... 
} 

e poi ho aggiunto:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    // Override point for customization after application launch. 
    // INIT THE BACKGROUND PATH STRING 

    [self refreshFields]; 
    [self.window addSubview:navController.view]; 
    [self.window makeKeyAndVisible]; 
    ***[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(shakeSuccess) name:@"shake" object:nil];*** 

    return YES; 
} 

Quando inizio il mio app sul mio iPhone, il metodo denominato "shakeSuccess" non chiama . Cosa devo fare per implementare questa funzione nella mia app? qualche idea?

risposta

59

Questo potrebbe aiutare ...
https://stackoverflow.com/a/2405692/796103

dice che è necessario impostare s' applicationSupportsShakeToEdit al YES il UIApplication. e sovrascrivi 3 metodi nel tuo VC:

-(BOOL)canBecomeFirstResponder { 
    return YES; 
} 

-(void)viewDidAppear:(BOOL)animated { 
    [super viewDidAppear:animated]; 
    [self becomeFirstResponder]; 
} 

- (void)viewWillDisappear:(BOOL)animated { 
    [self resignFirstResponder]; 
    [super viewWillDisappear:animated]; 
} 

Il resto del codice va bene. (La -motionEnded:withEvent:)

+0

Fantastico! Funziona alla grande! Grazie mille. – Samblg

+20

Lol e lui non l'ha mai fatto. –

+0

"Il resto" è il punto. – Itachi

26

Si potrebbe fare qualcosa di simile ...

In primo luogo ...

Impostare la proprietà applicationSupportsShakeToEdit nella delegato della App:

- (void)applicationDidFinishLaunching:(UIApplication *)application { 

    application.applicationSupportsShakeToEdit = YES; 

    [window addSubview:viewController.view]; 
    [window makeKeyAndVisible]; 
} 

In secondo luogo ..

Aggiungi/Sostituisci canBecomeFirstResponder, visualizzazione DidAppear: e viewWillDisappear: metodi nella tua View Controller:

-(BOOL)canBecomeFirstResponder { 
    return YES; 
} 

-(void)viewDidAppear:(BOOL)animated { 
    [super viewDidAppear:animated]; 
    [self becomeFirstResponder]; 
} 

- (void)viewWillDisappear:(BOOL)animated { 
    [self resignFirstResponder]; 
    [super viewWillDisappear:animated]; 
} 

In terzo luogo ...

Aggiungere il metodo motionEnded al View Controller:

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event 
{ 
    if (motion == UIEventSubtypeMotionShake) 
    { 
    // your code 
    } 
} 

che dovrebbe funzionare se il prima risposta no e questo è solo digitato rapidamente non testato :)

13

Se si vuole essere in grado di d applicare il movimento della scossa all'app, il modo migliore è quello di sovrascrivere la classe della tua applicazione con una classe personalizzata.

e quindi implementare questo nella vostra classe di applicazioni personalizzate

@implementation PSApplication 

- (void)sendEvent:(UIEvent *)event 
{ 
    if (event.type == UIEventTypeMotion && event.subtype == UIEventSubtypeMotionShake) { 
     [[NSNotificationCenter defaultCenter] postNotificationName:@"shakeNotification" object:nil]; 
    } 

    [super sendEvent:event]; 
} 

@end 
+3

Questa è la migliore risposta per chi è alla ricerca di un codice globale di scuotimento in-app che rilevi il codice, non vincolato al controller a vista singola. – RaffAl

+0

Per sovrascrivere l'applicazione UIA nel main.m, mettere questo: 'return UIApplicationMain (argc, argv, @" PSApplication ", NSStringFromClass ([AppDelegate class]));' –

+0

Why not subclass '- (void) motionEnded :(UIEventSubtype) motion withEvent: (UIEvent *) event' direttamente? – JakubKnejzlik

3

ho esteso classe UIApplication e riferimento di classe aggiunto principale: MyApplication.h

@interface MyApplication : UIApplication 

@end 

MyApplication.m

@implementation MyApplication 

- (void) sendEvent:(UIEvent *)event 
{ 
    if(event && (event.subtype==UIEventSubtypeMotionShake)) 
    { 
     AppDelegate *objAppDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate; 

     [objAppDelegate doWhatEver]; 
     [super sendEvent:event]; 
    } 
    else 
    { 
     [super sendEvent:event]; 
    } 
} 

@end 

E l'ultimo passaggio in main.m

int main(int argc, char *argv[]) 
{ 
return UIApplicationMain(argc, argv, NSStringFromClass([MyApplication class]), NSStringFromClass([AppDelegate class])); 
} 

Questo funziona in tutti i casi.

+0

Bella soluzione! Saluti –