Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIView animation ignores duration

I'm trying to perform an animation from within a UIView subclass, on one of its subviews.

The animation duration ignores any value I try to set it to, and just performs on a permanent duration of ~2 secs. The finished flag returns with True.

[UIView animateWithDuration:0.5f animations:^{
    img.frame = newFrame;
} completion:^(BOOL finished) {
    NSLog(@"result: %d", finished);
}];

When I'm using the old way, it works fine:

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5f];
img.frame = newIconRectFinal;
[UIView commitAnimations];

What could be the problem?

like image 541
Rizon Avatar asked Feb 16 '23 22:02

Rizon


2 Answers

You probably have the animation encapsulated inside another animate and its duration is being used for both of the animations.

like image 179
Wain Avatar answered Feb 24 '23 05:02

Wain


Use this line of code before start the animation write this single line code.

[UIView setAnimationsEnabled:YES];

see below the code it will work. Now it will not ignore duration

-(void)doAnimation
 {
   view2=(UIView*)[self.view viewWithTag:100];
   UIView setAnimationsEnabled:YES];
   [UIView animateWithDuration:3.0 animations:^{       
    view2.frame=CGRectMake(0, 30, 1024,768);
    view2.alpha=1.0       
     completion:^(BOOL finished){  
     view2.frame=CGRectMake(0, 0, 1024,768);
       view2.alpha=0.0 
    [weakSelf doAnimation];  
      }];
   }
like image 23
vijay Avatar answered Feb 24 '23 05:02

vijay