2017-05-19 64 views
1

我有一个UIView,我正在初始化,以100%的视角右侧开始“离屏”。我通过以下方式执行此操作:动画自动布局`leftAnchor`约束变化

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

然后,我想从左侧“滑动”视图。我想也许这样做:

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

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

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

回答

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

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