2012-04-14 34 views
21

tôi đã thêm đoạn mã sau vào appDelegate.m tôiiOS: Cách phát hiện chuyển động lắc?

- (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... 
} 

và sau đó tôi nói thêm:

- (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; 
} 

Khi tôi bắt đầu ứng dụng của tôi trên iPhone của tôi, các phương thức có tên "shakeSuccess" không gọi . Tôi nên làm gì để triển khai chức năng này trong ứng dụng của mình? bất kỳ ý tưởng nào?

Trả lời

59

Điều này có thể giúp bạn ...
https://stackoverflow.com/a/2405692/796103

Ông nói rằng bạn nên thiết lập 's applicationSupportsShakeToEdit để YESUIApplication. và ghi đè 3 phương thức trong VC của bạn:

-(BOOL)canBecomeFirstResponder { 
    return YES; 
} 

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

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

Phần còn lại của mã của bạn là tốt. (Các -motionEnded:withEvent:)

+0

Tuyệt vời! Nó hoạt động tuyệt vời! Cảm ơn nhiều. – Samblg

+20

Lol và anh ta chưa bao giờ làm điều đó. –

+0

"Phần còn lại" là điểm. – Itachi

26

Bạn có thể làm một cái gì đó như thế này ...

Thứ nhất ...

Thiết lập thuộc tính applicationSupportsShakeToEdit trong đoàn của App:

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

    application.applicationSupportsShakeToEdit = YES; 

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

Thứ hai .. .

Thêm/ghi đè canBecomeFirstResponder, chế độ xem DidAppear: và viewWillDisappear: phương pháp trong View Controller của bạn:

-(BOOL)canBecomeFirstResponder { 
    return YES; 
} 

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

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

Thứ ba ...

Thêm phương pháp motionEnded để View Controller của bạn:

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

Điều đó sẽ hoạt động nếu câu trả lời đầu tiên không và điều này chỉ được gõ nhanh chưa được kiểm tra :)

13

Nếu bạn muốn có thể khắc phục chuyển động lắc trên ứng dụng, cách tốt nhất là ghi đè lên lớp ứng dụng của bạn với một lớp tùy chỉnh.

Và sau đó thực hiện điều này trong lớp ứng dụng tùy chỉnh của bạn

@implementation PSApplication 

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

    [super sendEvent:event]; 
} 

@end 
+3

Đây là câu trả lời hay nhất cho những người tìm kiếm mã phát hiện lắc trong ứng dụng toàn cầu, không bị ràng buộc với bộ điều khiển chế độ xem đơn. – RaffAl

+0

Để ghi đè lên UIApplication trong main.m của bạn đặt này: 'return UIApplicationMain (argc, argv, @" PSApplication ", NSStringFromClass ([Lớp AppDelegate])); ' –

+0

Tại sao không phân lớp' - (void) motionEnded :(UIEventSubtype) chuyển động với sự kiện: (UIEvent *) event' trực tiếp? – JakubKnejzlik

3

tôi mở rộng lớp UIApplication và tham chiếu lớp bổ sung vào chính: 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 

Và bước cuối cùng trong main.m

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

Điều này hoạt động trong mọi trường hợp.

+0

Giải pháp tuyệt vời! Chúc mừng –

Các vấn đề liên quan