Please do not post the same thing multiple times. The board software automatically flags certain posts as needing moderator attention. This happens the most often for new users. I'm pretty sure this is made clear at the time you attempt to post. Posting the same thing over and over again just makes that many more posts the moderators have to weed through later. This makes us sad. Don't make us sad. If your post/thread doesn't appear, just wait a while. Don't post it again. If it hasn't shown up by the next day, then you can try again. I normally go through posts in the mornings, and try to check a few times throughout the day, but I'm not here 24/7. There will typically be a significant delay before posts are approved. Just be patient.
I currently have a button that when pressed, updates a label with a random number from 1-100. I need to have the same action done when the device is shook. I'm using the following code and would also like a switch to turn on and off the shake ability.
I currently have a button that when pressed, updates a label with a random number from 1-100. I need to have the same action done when the device is shook. I'm using the following code and would also like a switch to turn on and off the shake ability.
Both. First i want to be able to shake the device to do that action.
After i get that working, i would like to have a switch to enable/disable the detection.
Do a search on this board for shake. There is a better way to do it in 3.0 but honestly I don't recall it off the top of my head. The old way of doing it is....
In your view controller .h file add the UIAccelerometerDelegate to the @interface line. For example my code is based on the utility app project type so my viewcontroller interface line looks like @interface MainViewController : UIViewController
Then in view did load (Viewcontroller.m) add....
// This view should get accel events [UIAccelerometer sharedAccelerometer].delegate = self;
Then add the following to your view controller .m file #define kAccelerationThreshold 2.2
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration { if (shakeToReset) // Boolean to control whether or not to care about shakes. { if (fabsf(acceleration.x) > kAccelerationThreshold || fabsf(acceleration.y) > kAccelerationThreshold || fabsf(acceleration.z) > kAccelerationThreshold) [self DO SOMETHING]; } }
Replace DO SOMETHING with the function you want to invoke. Replace shakeToReset with a boolean value which is set by your switch and your in business.
I found the previous before I saw that 3.0 adds a spiff new way and as usual, because it was working I never went back to fix it. But it works solidly.
Well, yes and no. I used this code to get it working, but now i need to know how to add a switch to turn on and off the ability to trigger the action when shook.
Well, yes and no. I used this code to get it working, but now i need to know how to add a switch to turn on and off the ability to trigger the action when shook.
Thanks, this worked great but now my action is a :(id)sender{ action. How can i still use the switch because now it doesnt work and i'm getting awarning saying that it may not respond, which it doesnt.
Thanks, this worked great but now my action is a :(id)sender{ action. How can i still use the switch because now it doesnt work and i'm getting awarning saying that it may not respond, which it doesnt.
Thanks, Chase
I am not sure I am following your question. Your switch doesn't need an action only your button does. isOn is checking the state of the switch in your button action.
#pragma mark Motion Handling - (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event { if ([mySwitch isOn]) { [self magic]; }
[super motionEnded:motion withEvent:event]; }
, where if the switch is on, the action will be activated. when the action was:
-(IBAction)magic{
}
It worked fine, now the action is:
-(IBAction)magic:(id)sender{
}
and i get the error "may not respond to -magic" because the method types dont match.
If you want to use the magic function as both an event handler and a stand alone function you need to either pass a valid id when you are invoking it as a stand alone function or create a simple no param function which is called by both.
You really need to take a day or two and crank through some books. Begining iPhone development or the iPhone cookbook would speed up your development greatly.
In a nutshell you have 3 functions now.
function 1
-(void)doTheRealWork { //Do whatever }
// This function handles the shake - (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event { if ([mySwitch isOn]) { [self doTheRealWork]; }
[super motionEnded:motion withEvent:event]; }
// This function would be connected in IB to the switch. Do you really need to do anything when the switch is thrown? -(IBAction)handleSwitchChange:(id)sender { [self doTheRealWork]; }
If needed you can use that last function to also respond to the button clicks etc.
function, and the shake can't do [self doTheRealWork]; unless the doTheRealWork doesnt have the
:(id)sender
part, which it needs.
you need to just pass nil to the method like this:
[self doTheRealWork:nil];
I do that in several places where I have a button from interface builder that triggers an event like my info screen, and then I also have a button that I created in the code that also needs to trigger the action but doesn't require the (id)sender. Sending :nil along will supply what the method requires and will just run the method. You only need to be sure that you are not using sender in the method because if you are it will == nil.
-Mark
Check out my app 'Dress Up Studio' on the iTunes Store -
Replies
Rich
======================================
Tutorials
Twitter Search iPhone Integration
<a href="http://www.tempered.mobi/jsonmysql" target="_
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeAfter i get that working, i would like to have a switch to enable/disable the detection.
Freelance Inquiries: amnesiapps.com/contact
My Apps: AppStore.com/ChaseActon
I sell source code, email me.
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeThe old way of doing it is....
In your view controller .h file add the
UIAccelerometerDelegate to the @interface line. For example my code is based on the utility app project type so my viewcontroller interface line looks like
@interface MainViewController : UIViewController
Then in view did load (Viewcontroller.m) add....
// This view should get accel events
[UIAccelerometer sharedAccelerometer].delegate = self;
Then add the following to your view controller .m file
#define kAccelerationThreshold 2.2
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{
if (shakeToReset) // Boolean to control whether or not to care about shakes.
{
if (fabsf(acceleration.x) > kAccelerationThreshold || fabsf(acceleration.y) > kAccelerationThreshold || fabsf(acceleration.z) > kAccelerationThreshold)
[self DO SOMETHING];
}
}
Replace DO SOMETHING with the function you want to invoke. Replace shakeToReset with a boolean value which is set by your switch and your in business.
I found the previous before I saw that 3.0 adds a spiff new way and as usual, because it was working I never went back to fix it. But it works solidly.
Rich
======================================
Tutorials
Twitter Search iPhone Integration
<a href="http://www.tempered.mobi/jsonmysql" target="_
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeHow do I detect when someone shakes an iPhone? - Stack Overflow
Its the method which uses motion ended.
Rich
======================================
Tutorials
Twitter Search iPhone Integration
<a href="http://www.tempered.mobi/jsonmysql" target="_
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeFreelance Inquiries: amnesiapps.com/contact
My Apps: AppStore.com/ChaseActon
I sell source code, email me.
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeRich
======================================
Tutorials
Twitter Search iPhone Integration
<a href="http://www.tempered.mobi/jsonmysql" target="_
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
[self randomNumber];
[super motionEnded:motion withEvent:event];
Freelance Inquiries: amnesiapps.com/contact
My Apps: AppStore.com/ChaseActon
I sell source code, email me.
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeTry
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
if ([mySwitch isOn])
{
[self randomNumber];
}
[super motionEnded:motion withEvent:event];
It still receives the event but it does not call your function if your switch is set.
Rich
======================================
Tutorials
Twitter Search iPhone Integration
<a href="http://www.tempered.mobi/jsonmysql" target="_
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome+Rep!
Freelance Inquiries: amnesiapps.com/contact
My Apps: AppStore.com/ChaseActon
I sell source code, email me.
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeThanks,
Chase
Freelance Inquiries: amnesiapps.com/contact
My Apps: AppStore.com/ChaseActon
I sell source code, email me.
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeRich
======================================
Tutorials
Twitter Search iPhone Integration
<a href="http://www.tempered.mobi/jsonmysql" target="_
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome-(IBAction)magic{
}
It worked fine, now the action is:
-(IBAction)magic:(id)sender{
}
and i get the error "may not respond to -magic" because the method types dont match.
Freelance Inquiries: amnesiapps.com/contact
My Apps: AppStore.com/ChaseActon
I sell source code, email me.
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeRich
======================================
Tutorials
Twitter Search iPhone Integration
<a href="http://www.tempered.mobi/jsonmysql" target="_
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeFreelance Inquiries: amnesiapps.com/contact
My Apps: AppStore.com/ChaseActon
I sell source code, email me.
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeIn a nutshell you have 3 functions now.
function 1
-(void)doTheRealWork
{
//Do whatever
}
// This function handles the shake
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
if ([mySwitch isOn])
{
[self doTheRealWork];
}
[super motionEnded:motion withEvent:event];
}
// This function would be connected in IB to the switch. Do you really need to do anything when the switch is thrown?
-(IBAction)handleSwitchChange:(id)sender
{
[self doTheRealWork];
}
If needed you can use that last function to also respond to the button clicks etc.
Rich
======================================
Tutorials
Twitter Search iPhone Integration
<a href="http://www.tempered.mobi/jsonmysql" target="_
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeRight now I dont care about the switch, I just want the shake to trigger the action.
Freelance Inquiries: amnesiapps.com/contact
My Apps: AppStore.com/ChaseActon
I sell source code, email me.
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesomeyou need to just pass nil to the method like this:
[self doTheRealWork:nil];
I do that in several places where I have a button from interface builder that triggers an event like my info screen, and then I also have a button that I created in the code that also needs to trigger the action but doesn't require the (id)sender. Sending :nil along will supply what the method requires and will just run the method. You only need to be sure that you are not using sender in the method because if you are it will == nil.
-Mark
http://itunes.apple.com/us/app/dress...359728350?mt=8
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome