【问题标题】:Animating custom keyboard height in iOS 8在 iOS 8 中动画自定义键盘高度
【发布时间】:2014-12-11 02:43:26
【问题描述】:

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

//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];
}];

【问题讨论】:

    标签: ios ios8-extension ios-autolayout ios-keyboard-extension


    【解决方案1】:
    1. 不应在此处添加约束。

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

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

    【讨论】:

      【解决方案2】:

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

          blueScrollerCenterXConstraint.constant = (btn.tag - 4000)*btn.frame.size.width;
         [blueScroller updateConstraints];
      
         [UIView animateWithDuration:0.3 animations:^{
          [blueScroller layoutIfNeeded];
      }];
      

      【讨论】:

        【解决方案3】:

        首先在动画块外添加约束,然后调用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];
        }];
        

        【讨论】:

        • 当我尝试这个解决方案时,仍然没有考虑持续时间。
        • 有人找到解决办法了吗?
        • 我发现最好的方法是使用 CADisplayLink。此解决方案有效,但不够稳定。
        猜你喜欢
        • 1970-01-01
        • 2017-01-30
        • 2020-08-18
        • 1970-01-01
        • 2014-08-11
        • 2014-10-18
        • 2017-04-20
        • 2019-08-23
        • 1970-01-01
        相关资源
        最近更新 更多