在iOS9.0中对锚定样式约束进行动画处理 [英] Animate anchor style constraints in iOS9.0

查看:81
本文介绍了在iOS9.0中对锚定样式约束进行动画处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用锚点样式NSLayoutConstraints(如以下答案所示: Swift |添加约束))如何为子视图设置动画?

Using the anchor style NSLayoutConstraints (as in this answer: Swift | Adding constraints programmatically) how can I animate the subviews?

对于那些懒惰的人,代码如下:

For those who are lazy here is the code:

override func viewDidLoad() {
    super.viewDidLoad()

    let newView = UIView()
    newView.backgroundColor = UIColor.redColor()
    newView.translatesAutoresizingMaskIntoConstraints = false
    view.addSubview(newView)

    let horizontalConstraint = newView.centerXAnchor.constraintEqualToAnchor(view.centerXAnchor)
    let vertivalConstraint = newView.centerYAnchor.constraintEqualToAnchor(view.centerYAnchor)
    let widthConstraint = newView.widthAnchor.constraintEqualToAnchor(nil, constant: 100)
    let heightConstraint = newView.heightAnchor.constraintEqualToAnchor(nil, constant: 100)
    NSLayoutConstraint.activateConstraints([horizontalConstraint, vertivalConstraint, widthConstraint, heightConstraint])
}

具体地说,如果我想正确滑动newView,我该怎么做?谢谢.

Specifically, if I want to slide newView right how would I do it? Thanks.

推荐答案

您要设置约束,然后在动画块中使用layoutIfNeeded更新:

You'd set your constraint, and then within an animation block, update with layoutIfNeeded:

self.horizontalConstraint.constant += 10 // move right 10 px.
UIView.animateWithDuration(0.5) {
    self.view.layoutIfNeeded()
}

这篇关于在iOS9.0中对锚定样式约束进行动画处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆