5

我正在尝试使用以下代码在动画中增加自定义键盘的高度。但我无法弄清楚为什么会立即发生变化,忽略动画。

//In viewDidAppear

[self.view needsUpdateConstraints];
[UIView animateWithDuration:2 animations:^{ 
     CGFloat _expandedHeight = 500;
     NSLayoutConstraint *_heightConstraint = [NSLayoutConstraint 
          constraintWithItem: self.view 
          attribute: NSLayoutAttributeHeight 
          relatedBy: NSLayoutRelationEqual
          toItem: nil 
          attribute: NSLayoutAttributeNotAnAttribute
          multiplier: 0.0 
          constant: _expandedHeight];
     [self.view addConstraint: _heightConstraint];
     [self.view layoutIfNeeded];
}];
4

3 回答 3

0

首先在动画块外添加约束,然后调用updateConstraints,在动画块内,调用layoutIfNeeded。希望这会起作用

 CGFloat _expandedHeight = 500;
 NSLayoutConstraint *_heightConstraint = [NSLayoutConstraint 
      constraintWithItem: self.view 
      attribute: NSLayoutAttributeHeight 
      relatedBy: NSLayoutRelationEqual
      toItem: nil 
      attribute: NSLayoutAttributeNotAnAttribute
      multiplier: 0.0 
      constant: _expandedHeight];
 [self.view addConstraint: _heightConstraint];
[self.view needsUpdateConstraints];
[UIView animateWithDuration:2 animations:^{ 

 [self.view layoutIfNeeded];
}];
于 2014-10-18T13:57:57.523 回答
0

下面是我用来为线条的中心 X 位置设置动画的代码。我已经使用 IBOutlet NSLayoutConstraint 将这条线的中心 X 与一些参考视图的中心连接起来。为了使用自动布局发生动画,需要更新约束并在动画块内调用 layoutIfNeeded。这绝对是带有自动布局的动画的想法。

    blueScrollerCenterXConstraint.constant = (btn.tag - 4000)*btn.frame.size.width;
   [blueScroller updateConstraints];

   [UIView animateWithDuration:0.3 animations:^{
    [blueScroller layoutIfNeeded];
}];
于 2015-03-20T08:59:27.083 回答
0
  1. 您不应该在那里添加约束。

  2. 添加它并禁用它(或将优先级设置得非常低)。

  3. 之后,将优先级更改为 999,并在动画块中调用 layoutIfNeded。

于 2017-06-23T23:08:05.303 回答