0

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

Hello I am creating a app, and I need help. I have asked this question 2 times before but I am not getting the right answer, it either docent work or the person dosen't realise that I am a total beginner. So before answering please explain the answer because I am very new to Xcode. So I have a button saying play, and currently when you click on it a animation appears smoothly and then ends. What I need to change is when the user press's play, a label saying shake the device to start. Then when the user shake's the device the label disappears and the animation plays and end, this is what I have so far in my viewcontroller.m file:

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



}

By the way, the IBAction:startanimation is the action when the user press's the button 'play'

Please consider I am a beginner to coding, and if you have any questions feel free to ask, THanks!

Community
  • 1
  • 1
Stevenp
  • 3
  • 3

2 Answers2

2

Method

- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event;
- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event;
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event;

you just use this code:

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
  if (event.type == UIEventSubtypeMotionShake) {
    //Your code here
  }
}
Rajneesh071
  • 30,846
  • 15
  • 61
  • 74
1

Add the following in your header file (.h)

@interface YourViewController : UIViewController
{
    IBOutlet UILabel *label;
    BOOL canStartAnimation;
}

change start animation too

-(IBAction)startanimation 
{
    canStartAnimation = YES;
    label.text = @"shake to animate";
}

Add this function

-(void) doTheAnimation {
    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];   
    label.text = @"";
}

Add the motionBegan

- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    if (event.type == UIEventSubtypeMotionShake && canStartAnimation) {
        [self doTheAnimation];
    }
}
Omar Abdelhafith
  • 21,163
  • 5
  • 52
  • 56
  • Hello thank you so much for your help, i have tried this on a test Xcode project and I think i have done it wrong but I am not sure, is their anyway I could email the test Xcode, then you could check why it's not doing what I need it to do?? :) thanks – Stevenp Jun 30 '12 at 15:59