问题与UIView的自动布局动画(Issue with UIView Auto Layout Ani

2019-10-19 05:36发布

我有一个UIView ,当我打开视图控制器从顶部飞。 我已经设定的Y约束UIView至-200和视图负载时,下面的叫,一切工作正常:

- (void)updateViewConstraints
{
    [super updateViewConstraints];
    self.popUpViewYConstraint.constant = 37.0f;
    self.closeButtonYConstraint.constant = 28.0f;
    [self.popUpBaseView setNeedsUpdateConstraints];
    [self.closeButton setNeedsUpdateConstraints];

    [UIView animateWithDuration:0.25f animations:^{
        [self.popUpBaseView layoutIfNeeded];
        [self.closeButton layoutIfNeeded];
        [self.view layoutIfNeeded];
    }];
}

但现在我有一个关闭按钮应该动画UIView回到-200位置,然后从屏幕视图控制器。 但热闹是不是正在发生。 视图控制器是越来越直接除去。 下面是我在做什么:

- (IBAction)closePressed:(id)sender
{
    NSMutableArray *navigationArray = [[NSMutableArray alloc] initWithArray: self.navigationController.viewControllers];
    self.navigationController.viewControllers = navigationArray;

    [UIView animateWithDuration:2.0f animations:^{
        self.popUpViewYConstraint.constant = -200.0f;
        [self.popUpBaseView layoutIfNeeded];
    } completion:^(BOOL finished){
        [navigationArray removeObjectAtIndex: 1];
        [self.baseView removeFromSuperview];
        [self.view removeFromSuperview];
    }];
}

我提到这个链接。 这似乎是为他们工作,但对我不起作用。 请帮忙。

Answer 1:

这条线:

self.popUpViewYConstraint.constant = -200.0f;

动画块之前被调用。 另外我不知道关于你的看法层次,但要确保你正在呼吁与layoutIfNeeded调用正确的观点。



Answer 2:

这个怎么样

- (IBAction)closePressed:(id)sender
{
    NSMutableArray *navigationArray = [[NSMutableArray alloc]:initWithArray:self.navigationController.viewControllers];
    self.navigationController.viewControllers = navigationArray;

    // I am sure the constant should be set outside the animation block. It won't happen until next run loop, which will be inside the block. 
    self.popUpViewYConstraint.constant = -200.0f;
    // setNeedsUpdateConstraints should be called on the view to which the constraint is added, not the view affected by the constraint.
    [self.baseView setNeedsUpdateConstraints];

    [UIView animateWithDuration:2.0f animations:^{
         // I think this should be topmost view
         [self.view layoutIfNeeded];
    } completion:^(BOOL finished){
        [navigationArray removeObjectAtIndex: 1];
        [self.baseView removeFromSuperview];
        [self.view removeFromSuperview];
    }];
 }


文章来源: Issue with UIView Auto Layout Animation