4
votes

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?

2
I copied your code into a UIView subclass, and animated the frame of an image view -- it worked as it should, so I don't think the problem is in the code you posted. - rdelmar
Do you have another animation block that is running at the same time / encapsulates this block? - Wain
@Wain, you're right! I did accidentally encapsulated 2 animations. It's weird though that the old animation style worked fine, isn't it? - Rizon
@Wain, anyway please right your comment as an answer so I can give you the credit :) - Rizon

2 Answers

7
votes

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

1
votes

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