0

Possible Duplicate:
How do i put a void(shake gesture) inside a IBAction(button)?

I am a beginner at creating apps and I am currently working on a project with a animation. So far i have a button and when the user press's the button a animation plays and ends. But I need help, because i need the user to shake the iPhone after he presses play. This is what i exactly need when user press's play a uilabel shows saying shake to play once the user shake's the iPhone the uilabel disappears and the animation play's. Here what I have so far: -(IBAction)startanimation {

animation.animationImages= [NSArray arrayWithObjects:
                            [UIImage imageNamed:@"Frame0.png"],
                            [UIImage imageNamed:@"Frame1.png"],
                            [UIImage imageNamed:@"Frame2.png"],
                            [UIImage imageNamed:@"Frame3.png"],
                            [UIImage imageNamed:@"Frame4.png"],
                            [UIImage imageNamed:@"Frame5.png"],
                            [UIImage imageNamed:@"Frame6.png"],
                            [UIImage imageNamed:@"Frame7.png"],
                            [UIImage imageNamed:@"Frame8.png"],
                            [UIImage imageNamed:@"Frame9.png"],
                            [UIImage imageNamed:@"Frame10.png"],
                            [UIImage imageNamed:@"Frame11.png"],
                            [UIImage imageNamed:@"Frame12.png"],
                            [UIImage imageNamed:@"Frame13.png"],
                            [UIImage imageNamed:@"Frame14.png"], 
                            nil];

[animation setAnimationRepeatCount:1];
animation.animationDuration = 1;
[animation startAnimating];


button.hidden = 1;
animation.hidden = 0;
Menu.hidden = 0;
replay.hidden = 0;

}

and this is what i plan to put in:

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
if (event.subtype == UIEventSubtypeMotionShake)

Thank you i am a total beginner so please explain with instructions, thank you! :)

Community
  • 1
  • 1
Stevenp
  • 3
  • 3
  • i can't put a void inside a Ibaction, i don't know how to get the animation to react to the motion only after the button is pressed :) – Stevenp Jun 29 '12 at 21:18
  • You said that already. What does it mean to "put a void in an IBAction"? That means nothing. – Linuxios Jun 29 '12 at 21:19

3 Answers3

1

You cannot solve this by "putting a void in a IBAction," because IBAction and void are somewhat equivalent in Objective-C.

However, here's how you can make this work: when the user presses the button, you put up a label and set a BOOL flag indicating that user has pressed the label.

Inside the motionEnded:withEvent method, you then check if (event.subtype == UIEventSubtypeMotionShake && flag) before you start the animation.

Don't forget to initialize the flag to NO/false

Here's the sample code:

I assume your .h file looks something like this

@interface MyController: UIViewController {
  BOOL buttonPressed;
}
@end

Here's what should be in your .m file

- (void)viewDidLoad {
  buttonPressed = NO;
}

- (IBAction)buttonPress:(id)sender {
  buttonPressed = YES;
  // show label
}

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
  if (event.subtype == UIEventSubtypeMotionShake && buttonPressed) {
    // start animation
  }
}
Steven Luu
  • 1,047
  • 1
  • 7
  • 13
  • 1
    I kinda cheated. I posted and then thought about it, and was like "OP says he's a newbie, some code samples wouldn't hurt." :-) – Steven Luu Jun 29 '12 at 21:10
  • ah thank you a lot i will try it now, how do i set a BOOl flag to a label? – Stevenp Jun 29 '12 at 21:15
  • You don't. The BOOL flag is a separate variable. You declare it in your .h file, inside the interface. To initialize it, you can do it in the `viewDidLoad` method. – Steven Luu Jun 29 '12 at 21:17
  • I added more code in the answer. You can refresh. – Steven Luu Jun 29 '12 at 21:21
  • when i copy the above code it says buttonPressed is an undeclared identifier, and i have only declared buttonPress as an action, what do i do for the buttonPressed? – Stevenp Jun 29 '12 at 21:24
  • You should be coming code from here. It's very raw and sample-y. Notice the difference between buttonPress and buttonPressed. One is a BOOL variable and the other is an IBAction method. – Steven Luu Jun 29 '12 at 21:27
  • Sorry the new code is sort of working i just need to connect everything in the storyboard, tell you if it works in a few mins :) – Stevenp Jun 29 '12 at 21:28
  • Hi im testing the app and when i press play it pauses the simulator and comes up with the debugger. I think i might have connecting it wrong in the story board, there are 2 button press outlets, and i have put them both to the same button, what have i done wrong, i am using the code you provided? thanks – Stevenp Jun 29 '12 at 21:36
  • The code I gave you didn't have any outlet. Like I said, the BOOL variable *buttonPressed* is just a variable. It's not related to the button, not related to the IBAction, and certainly is not an IBOutlet. If the name confuses you then you can rename it to something like `flagUserDidPressTheButton`. – Steven Luu Jun 29 '12 at 21:40
  • okay thanks, sorry for bothering you so much haha, so wait – Stevenp Jun 29 '12 at 21:45
  • this is my .m file - (void)viewDidLoad { buttonPressed = NO; } - (IBAction)buttonPress:(id)sender { buttonPressed = YES; } - (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event { if (event.subtype == UIEventSubtypeMotionShake && buttonPressed) { animation.animationImages= [NSArray arrayWithObjects: – Stevenp Jun 29 '12 at 21:46
  • [UIImage imageNamed:@"Frame0.png"], [UIImage imageNamed:@"Frame1.png"], [UIImage imageNamed:@"Frame2.png"], [UIImage imageNamed:@"Frame3.png"], [UIImage imageNamed:@"Frame4.png"], [UIImage imageNamed:@"Frame5.png"], nil]; [animation setAnimationRepeatCount:1]; animation.animationDuration = 1; [animation startAnimating]; } } – Stevenp Jun 29 '12 at 21:46
  • and my .h: BOOL buttonPressed; IBOutlet UIImageView *animation; IBOutlet UIButton *button; } -(IBAction)buttonpress; – Stevenp Jun 29 '12 at 21:47
  • Please paste your code somewhere (gist, pastebin, etc) and provide the link here. – Steven Luu Jun 29 '12 at 21:47
  • i think I've connected it properly, but when i click play nothing happens, if i press shake and then play still nothing happens – Stevenp Jun 29 '12 at 21:47
  • the links aren't working. and you should remove your email from here. – Steven Luu Jun 30 '12 at 11:31
  • here : http://pastebin.com/5skprswk – Stevenp Jun 30 '12 at 11:35
  • well when i simulate it on Xcode and press play and then do the shake gesture from the hardware list at the top nothing happens, the animation docent start, whats wrong with it?? – Stevenp Jun 30 '12 at 11:40
  • The code seems fine (except you should use properties for IBOutlets, but don't worry about it right now). You most likely have the IBOutlets connected wrong. Debug in the console, use `NSLog` to print out `animation`, make sure it's not `nil`. Else scratch the project, start fresh and STOP COPYING AND PASTING CODE! I'm out! – Steven Luu Jun 30 '12 at 11:44
0

you can call a void with [self nameOfTheVoid];

-(IBAction)youraction {

    //some code in here
    [self motionEnded];
}
SpaceDust__
  • 4,844
  • 4
  • 43
  • 82
0

write a function hooked up to your button, which will set a flag like BOOL buttonPressed and display that label telling the user to shake the phone.

In motionEnded you can check to see if buttonPressed is YES, in which case you will hide that label, perform your animation, then set buttonPressed back to NO.

Dima
  • 23,484
  • 6
  • 56
  • 83