如何制作持有图像抖动的UIView?

时间:2014-03-21 19:39:26

标签: ios iphone objective-c

我已经尝试过本教程:http://2cupsoftech.wordpress.com/2012/09/20/shake-animation-to-a-uiview-or-uibutton/

但是我需要它才能在按下检查按钮时摇晃:

- (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 个答案:

答案 0 :(得分:0)

我认为这可以帮到你!将此代码添加到源代码中

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

并像这样调用startShake:方法:

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