-3

Ive Ive tried this tutorial: http://2cupsoftech.wordpress.com/2012/09/20/shake-animation-to-a-uiview-or-uibutton/

But I need it to only shake when I press the check button:

- (IBAction)btncheck:(id)sender {



// Enter has been pressed, see if the text in the textView equals our string
NSString *answer = [_textbox.text stringByReplacingOccurrencesOfString:@" " withString:@""];
if ([answer isEqualToString:@"q"]) {

    _keyboard.hidden = YES;
    _textXclear.hidden = YES;

    [_closeone setHidden:NO];
    [_wrongone setHidden:YES];

}
else {

    [_wrongone setHidden:NO];
}
1
  • Please edit your earlier question to clarify and add details rather than reposting on the same subject.
    – jscs
    Mar 21, 2014 at 20:39

1 Answer 1

0

I think this can help you ! Add this code into your source code

- (void) startShake:(UIView*)view
{
    CGAffineTransform leftShake = CGAffineTransformMakeTranslation(-5, 0);
    CGAffineTransform rightShake = CGAffineTransformMakeTranslation(5, 0);

    view.transform = leftShake;  // starting point

    [UIView beginAnimations:@"shake_button" context:view];
    [UIView setAnimationRepeatAutoreverses:YES]; // important
    [UIView setAnimationRepeatCount:5];
    [UIView setAnimationDuration:0.06];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(shakeEnded:finished:context:)];

    view.transform = rightShake; // end here & auto-reverse

    [UIView commitAnimations];
}

- (void) shakeEnded:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
{
    if ([finished boolValue]) {
        UIView* item = (UIView *)context;
        item.transform = CGAffineTransformIdentity;
    }
}

and call the startShake: method like this :

- (IBAction)btncheck:(id)sender {

    [self startShake:[sender view]]; //add this

// Enter has been pressed, see if the text in the textView equals our string
NSString *answer = [_textbox.text stringByReplacingOccurrencesOfString:@" " withString:@""];
if ([answer isEqualToString:@"q"]) {

    _keyboard.hidden = YES;
    _textXclear.hidden = YES;

    [_closeone setHidden:NO];
    [_wrongone setHidden:YES];

}
else {

    [_wrongone setHidden:NO];
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.