【问题标题】:animating autolayout `leftAnchor` constraint change动画自动布局`leftAnchor`约束更改
【发布时间】:2017-05-19 23:25:31
【问题描述】:

我有一个 UIView,我正在初始化它以在 100% 的视图右侧启动“屏幕外”。我通过以下方式做到这一点:

[childView.leftAnchor constraintEqualToAnchor: superview.rightAnchor
                                     constant: 1.0].active = YES;

然后我想从左侧“滑动”视图。我想可能会做以下事情:

[UIView animateWithDuration: 5
                 animations: ^{
                     [childView.leftAnchor constraintEqualToAnchor: superview.leftAnchor
                                                               constant: 1.0].active = YES;
                 }];

可能会这样做,但它会立即发生(没有动画,它只是“出现”)。

是否可以使用自动布局锚点来提供动画?

【问题讨论】:

    标签: ios objective-c autolayout nslayoutconstraint


    【解决方案1】:
    [childView.leftAnchor constraintEqualToAnchor: superview.leftAnchor constant: 1.0].active = YES;
    [UIView animateWithDuration: 5
                     animations: ^{
                         [childView layoutIfNeeded];
                     }];
    

    应该这样做。约束更改本身不应在动画块中,而应在布局调用中。您还应该禁用/删除您所做的初始约束,因为这个新约束与其直接冲突(您可能会在控制台中看到警告)。

    【讨论】:

      猜你喜欢
      • 2014-05-26
      • 1970-01-01
      • 2014-01-27
      • 1970-01-01
      • 1970-01-01
      • 2016-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多