简体   繁体   中英

UIView not animating the first time

This code animates my view every time it's run, except for the first time. When the keyboard is displayed or hidden, a UIView is repositioned:

[UIView
animateWithDuration:0.26
animations:^{
    [self setupActiveOverlayViewFrame];
}completion:nil];

-(void)setupActiveOverlayViewFrame {
    float optimalOverlayHeight = [self.activePanel optimalHeight];

    float realOverlayHeight = MIN(optimalOverlayHeight, self.displayView.frame.size.height);

    if (self.activePanel.frame.size.height != realOverlayHeight) {
        self.activePanel.frame = CGRectMake(self.activePanel.frame.origin.x, 0, self.activePanel.frame.size.width, realOverlayHeight);
    }

    self.activePanel.center = [self correctCenterForOverlay];
}

The method I posted is just to show that all it does is re-size and re-position it.

The first time this code is run, it doesn't animate. It just jumps into position. Every time after that, it animates correctly.

Its possible that the "keyboard did display" notification is being called before your view is fully set up, like if you've pushed a view controller onto a navigation controller and immediately give a text view focus on viewDidLoad

You can either keep track of when the keyboard is up or down and when viewDidAppear is called, check if the keyboard is up, and run it. Or you can postpone giving focus to a text view/field until viewDidAppear: is called.

I got the same problem. I want to advice you to check Panel center before(!) animating to find out has it right position at the first run. If it's not. Just set right position. Also try the next code

[self setupActiveOverlayViewFrame];
[UIView animateWithDuration:0.26
                 animations:^{
           [self.view layoutIfNeeded];
}completion:nil];

PS If you are using autolayout better use constraints to move your views using the code above.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM