Pergunta

I'm animating a ImageView to move it up. Everything works fine, but when I present a ViewController the ImageView swaps back to its original position.

I tried already to set the Frame when the Animation is completed, But it still swap back to its original position.

heres what I do:

-(void)viewDidLoad:(BOOL)animated{
    [super viewDidLoad:animated];
    [self performSelector:@selector(animationCode) withObject:nil afterDelay:0.1f];
}

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
}

//Animate welcome screen
-(void)animationCode{
    CGRect imageFrame = self.imageViewLogo.frame;
    [UIView animateWithDuration:1.5 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        self.imageViewLogo.center = CGPointMake(self.imageViewLogo.center.x, self.imageViewLogo.center.y-150);
    }completion:^(BOOL finished){
        self.imageViewLogo.frame = CGRectMake(imageFrame.origin.x, imageFrame.origin.y-150, imageFrame.size.width, imageFrame.size.height);
    }];
}

Any idea how to fix this? I think it has something todo with the viewDissapering..

Foi útil?

Solução

First, you should not animated views in viewDidLoad. You should start animation in viewWillAppear or viewDidAppear.

What was said in the comments is correct, you should not be modifying frames directly when autolayout is enabled. Any constraint update or layout pass that comes will reset the frames to their original values. What you should do is modify the constraints themselves that are in place. If you add these constraints in code, perhaps save the ones necessary for animation as properties and modify them in the animation setup method. If you set up your view in Interface Builder, you can create outlets of constraints in the code and access them that way. Remember to call [self.view layoutIfNeeded] inside your animation block to actually trigger a constraint update and layout pass.

-(void)animationCode{
    CGRect imageFrame = self.imageViewLogo.frame;
    [UIView animateWithDuration:1.5 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        self.imageViewLogoYConstraint.constant -= 150;
        [self.view layoutIfNeeded];
    } completion:nil];
}

Where, imageViewLogoYConstraint is a constraint example that has been set up somewhere.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top