简体   繁体   English

UIView第一次没有动画

[英]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:

[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 在完全设置视图之前,可能会调用“键盘已显示”通知,例如是否将视图控制器推到导航控制器上并立即将文本视图集中在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. 您可以跟踪键盘何时向上或向下以及何时调用viewDidAppear ,检查键盘是否向上并运行。 Or you can postpone giving focus to a text view/field until viewDidAppear: is called. 或者,您可以推迟将焦点放在文本视图/字段上,直到调用viewDidAppear:为止。

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. PS:如果您使用自动布局,最好使用约束使用上面的代码移动视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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