Discussion Forums  >  Uncategorized

Replies: 13    Views: 2198

mutzy
Aspiring developer
Profile
Posts: 841
Reg: Nov 03, 2010
Medford, MA
9,860
04/16/11 12:15 PM (14 years ago)

How about Shake to get back home?

I know we keep coming up with more and more things we'd like to see added to Buzztouch, and there's certainly no hurry, but something I thought of yesterday that I think could be really useful in the future is a shake option. My apps have many menus and sub-menus and sub-sub-menus etc. I initially toyed with putting right buttons on all the navigation bars to go home but this didn't look very good. I think it would great if I could set up my app so that when the user shakes their iphone/ipad it will automatically bring them back to the home screen. I'll check out some of the iphone developer forums to see if maybe I could do this on my own but think it would be a great addition for buzztouch apps in general! JOsh
 
David @ buzztouch
buzztouch Evangelist
Profile
Posts: 6866
Reg: Jan 01, 2010
Monterey, CA
78,840
like
04/16/11 10:17 PM (14 years ago)
In v1.5 you could do this pretty easily. It would be implemented differently for a non-tabbed app or a tabbed-app but mostly the same. When you dig-around for some examples... put your 'shaking logic' in the [app-name]_appDelegate.m file. This is the idea, and what to figure out on your end. a) Implement gesture recognize delegate methods in [app-name]_appDelegate.m. This will allow you to 'catch' shakes on any screen your app is on b) When the app shakes... and you trap it in your method that detects the shake you would do (if a tabbed app): [[self.rootApp.rootTabBarController selectedViewController] popToRootViewController:YES]; This is psuedo code but should get you pointed in the right direction.
 
mutzy
Aspiring developer
Profile
Posts: 841
Reg: Nov 03, 2010
Medford, MA
9,860
like
04/17/11 06:05 AM (14 years ago)
What about for a non tabbed app?
 
mutzy
Aspiring developer
Profile
Posts: 841
Reg: Nov 03, 2010
Medford, MA
9,860
like
04/17/11 06:29 AM (14 years ago)
According to a tutorial I found on stack overflow, in my appdelegate.m file I should put: - (void)applicationDidFinishLaunching:(UIApplication *)application { application.applicationSupportsShakeToEdit = YES; [window addSubview:viewController.view]; [window makeKeyAndVisible]; } Yet this gives me errors. Then, in my view controller file, I'm supposed to put the following: -(BOOL)canBecomeFirstResponder { return YES; } -(void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; [self becomeFirstResponder]; } - (void)viewWillDisappear:(BOOL)animated { [self resignFirstResponder]; [super viewWillDisappear:animated]; } and then... - (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event { if (motion == UIEventSubtypeMotionShake) { // your code } } Look right to you?
 
David @ buzztouch
buzztouch Evangelist
Profile
Posts: 6866
Reg: Jan 01, 2010
Monterey, CA
78,840
like
04/17/11 10:39 PM (14 years ago)
You'll need to declare your appDelegate.h file as a UIAccelerometerDelegate so it listens for the accelerometer notification. Your appDelegate.h file already lists several delegates it's implementing. It should look like this right now: @interface YOUAPPNAME_appDelegate : NSObject <UIApplicationDelegate, BT_downloadFileDelegate, UITabBarControllerDelegate, AVAudioPlayerDelegate, UIAlertViewDelegate>{ You could just add another interface to this list after the UIAlertViewDelegate like this: @interface BT_appDelegate : NSObject <UIApplicationDelegate, BT_downloadFileDelegate, UITabBarControllerDelegate, AVAudioPlayerDelegate, UIAlertViewDelegate, UIAccelerometerDelegate>{ This is in the .h file. Next, in the .m file, you'll need to implement the delegates didAccelerate method. Then, in that method you'll do some logic. The didAccelerate method fires every time the device moves. Your logic figures out what to do with those motions. You'll need to initialize an instance of the UIAccelerometer somewhere. I do this when the app launches... //in your appDelegate.m's didFinishLaunchingWithOptions method, do this.... UIAccelerometer *accelerometer = [UIAccelerometer sharedAccelerometer]; accelerometer.delegate = self; accelerometer.updateInterval = 0.25; This will initialize an instance of the shared accelerometer...Next...add a method like this to 'listen' for the events... - (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration{ double myThreshold = 2.0; if (fabsf(acceleration.x) > myThreshold || fabsf(acceleration.y) > myThreshold || fabsf(acceleration.z) > myThreshold) { NSLog(@'the device is shaking...'); } } See this: http://blog.ablepear.com/2010/02/iphone-sdk-shake-rattle-roll.html
 
mutzy
Aspiring developer
Profile
Posts: 841
Reg: Nov 03, 2010
Medford, MA
9,860
like
04/21/11 05:45 AM (14 years ago)
I finally got around to adding this code to try and get shaking to work. When I tried to build my project I got two errorss both relating to the last line: NSLog(@'the device is shaking...'); error #1: unexpected @ in program error #2: expected expression before @ token I'm not terribly concerned about this because I want to set it up so when I shake the device it will go back to my non-tabbed layout home screen and not say that the device is shaking. How would I go about doing that? Thanks!! Josh
 
David @ buzztouch
buzztouch Evangelist
Profile
Posts: 6866
Reg: Jan 01, 2010
Monterey, CA
78,840
like
04/21/11 09:51 PM (14 years ago)
Hi... so it sounds like syntax issues. Be sure to use double-quotes around strings. The forum removes these. This means the sample code uses single quotes where there should be double quotes. Please confirm you're using double quotes in log NSLog Statements so I understand if I need to continue helping of if this fixed the errors. Xcode's pretty smart and syntax aware ;-)
 
mutzy
Aspiring developer
Profile
Posts: 841
Reg: Nov 03, 2010
Medford, MA
9,860
like
04/22/11 07:33 AM (14 years ago)
I will check tonight and let you know...
 
mutzy
Aspiring developer
Profile
Posts: 841
Reg: Nov 03, 2010
Medford, MA
9,860
like
04/22/11 03:31 PM (14 years ago)
success! I changed the code to double quotes and now when I shake my phone I see the device is shaking in my xcode log. now that my app can detect shaking motions, how do i get it to return to my home screen (non-tabbed menu) when a shake is registered? Thanks!
 
David @ buzztouch
buzztouch Evangelist
Profile
Posts: 6866
Reg: Jan 01, 2010
Monterey, CA
78,840
like
04/22/11 11:53 PM (14 years ago)
Yippeeeee. Great job. So.. the next step to to fire the 'get back home' method when the app shakes. I relize your not using tabs but this code should work either way (in case you want to copy-paste it into a future app that does use tabs: //just below the device-is-shaking message, add this...should work. Change YES to NO if you don't want the animation effect when it transitions to the home screen. if([self.rootApp.tabs count] > 0){ //for tabbed apps, send the selected tab to home screen [self.rootApp.rootTabBarController.selectedViewController popToRootViewControllerAnimated:YES]; }else{ //for non-tabbed apps [self.rootApp.rootNavController popToRootViewControllerAnimated:YES]; }
 
mutzy
Aspiring developer
Profile
Posts: 841
Reg: Nov 03, 2010
Medford, MA
9,860
like
04/23/11 06:46 AM (14 years ago)
I tried the above code and I just couldn't it to work right at first. Every time I hit an icon on my home screen it would just transition right back to the home screen. BUT I DID GET IT TO WORK by changing it to this: //hopefully get this baby to shake back home - (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration{ double myThreshold = 2.0; if (fabsf(acceleration.x) > myThreshold || fabsf(acceleration.y) > myThreshold || fabsf(acceleration.z) > myThreshold) { NSLog(@the device is shaking...); [self.rootApp.rootNavController popToRootViewControllerAnimated:YES]; } } @end Of course then I got greedy and tried to get it to vibrate as well using the link you provided above and I ended up with 63 errors and had to redownload my project since I couldn't figure out what I had done wrong. I have it back to the way it was though and vibrating will just have to wait. Thanks so much for you help!
 
mutzy
Aspiring developer
Profile
Posts: 841
Reg: Nov 03, 2010
Medford, MA
9,860
like
04/30/11 06:03 AM (14 years ago)
Having a new issue with my awesome new shake to home feature. I have an option to play a cpr mentronome which is a mp3 file embedded in the project. Usually when I play the mp3 it stops automatically when I close the program or hit the back button. However, if I shake out of the screen to go home the mp3 keeps playing. I don't think someone would hold their phone doing cpr but if they did it would quickly send them to the home screen. Is there an easy way to prevent that? If not I'll have to take it out... probably not a often used feature anyways. Thanks!
 
David @ buzztouch
buzztouch Evangelist
Profile
Posts: 6866
Reg: Jan 01, 2010
Monterey, CA
78,840
like
05/01/11 09:57 PM (14 years ago)
Ok, so if I understand, you start an .mp3 when a certain screen loads. The way the 'back' or 'close' stops the sound depends on a few things. If my assumptions are right, and you are using the Background Audio feature in the Advanced Settings of a screen (the screen that plays the .mp3), you'll need to do this just before you return to home (popToRootViewController) if(self.audioPlayer != nil){ [self.audioPlayer stopAudio]; } See if that does it. Also, because you are basically simulating a 'back button tap' - you may want to get a look at the handleLeftButton method in the BT_viewControllerManager.m file. It's a long method but shows you step-by-step all the things the software does on back click. This is where I dug-up the stop-audio method.
 
mrDavid
BTMods.com
Profile
Posts: 3936
Reg: May 21, 2011
San Diego, CA
51,910
like
12/02/13 01:37 PM (12 years ago)
For anyone looking for this functionality in a plugin.. https://www.buzztouch.com/plugins/plugin.php?pid=FAD3221726833083625AFA1 Cheers! David https://buzztouchmods.com/market
 

Login + Screen Name Required to Post

pointerLogin to participate so you can start earning points. Once you're logged in (and have a screen name entered in your profile), you can subscribe to topics, follow users, and start learning how to make apps like the pros.