Kumar sonu
Kumar sonu

Reputation: 535

Problem with simple animation

I am using animation on a button click first time show a view and second ti me hide a view. here is my code for hiding a view

-(IBAction)clickme
{
[UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3];

    [UIView setAnimationCurve:UIViewAnimationCurveLinear];
    [UIView setAnimationDelegate:self];


    [view1 setAlpha:0.0];
[UIView commitAnimations];
}

similar code is there for showing the view.

But the problem arises when user click the button many times again and again....means i am using 2 seconds for my animation but if user presses the same button in during the animation then the output result is very bad.

I don't want to disable that button during the period of animation.

Is there any other way?

Upvotes: 0

Views: 368

Answers (2)

meronix
meronix

Reputation: 6176

try to use + (void)setAnimationBeginsFromCurrentState:(BOOL)fromCurrentState:

-(IBAction)clickme
{
[UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3];
    [UIView setAnimationBeginsFromCurrentState:YES];

    [UIView setAnimationCurve:UIViewAnimationCurveLinear];
    [UIView setAnimationDelegate:self];


    [view1 setAlpha:0.0];
[UIView commitAnimations];
}

Upvotes: 2

joerick
joerick

Reputation: 16458

You need to keep track of whether there's an animation going on, and ignore the click if it is.

Declare an instance variable BOOL animating; in your class header, and initialise it to NO in your init.

Then,

-(IBAction)clickme
{
    if (animating) return;
    animating = YES;

[UIView beginAnimations:nil context:self];
    [UIView setAnimationDuration:0.3];

    [UIView setAnimationCurve:UIViewAnimationCurveLinear];
    [UIView setAnimationDelegate:self];

    [view1 setAlpha:0.0];
[UIView commitAnimations];
}

- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
    if (context == self)
        animating = NO;
}

Upvotes: 2

Related Questions