在Swift中,如何像自动滑动页面一样为UIView设置动画? [英] In Swift, how do I animate a UIView with auto-layout like a page sliding in?

查看:113
本文介绍了在Swift中,如何像自动滑动页面一样为UIView设置动画?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试创建一个UIView,该UIView表示一个页面,该页面的大小与设备屏幕的大小相同.由于该应用程序支持方向,因此我正在使用自动布局来构造它.

I try to create a UIView that is representing a page whose size is the same as the device screen. Since the app supports orientation, I am using AutoLayout to construct it.

在我尝试为页面设置动画以使其从右侧滑入之前,该方法工作正常.经过研究,这是我能想到的最好的方法:

It works fine until I try to animate the page to slide in from the right. Here is the best I could come up with after some research:

myView = UIView()
myView.backgroundColor = UIColor.orangeColor()
parentView.addSubview(myView)

myView.translatesAutoresizingMaskIntoConstraints = false
myView.leftAnchor.constraintEqualToAnchor(parentView.rightAnchor).active = true
myView.widthAnchor.constraintEqualToAnchor(parentView.widthAnchor).active = true
myView.heightAnchor.constraintEqualToAnchor(parentView.heightAnchor).active = true

UIView.animateWithDuration(Double(1.0), animations: {
    self.myView.leftAnchor.constraintEqualToAnchor(self.parentView.leftAnchor).active = true
    self.myView.layoutIfNeeded()
})

使用上面的代码,页面myView从左上角滑入,这不是我期望的,并且日志中也显示Unable to simultaneously satisfy constraints.

With the code above, the page myView slides in from the top-left corner, which is not what I am expecting and the log also says Unable to simultaneously satisfy constraints.

任何人建议帮助我并纠正我以更好地理解自动版式和动画,非常感谢.谢谢.

Any advise to help me and correct me for better understanding AutoLayout and animation is very much appreciated. Thanks.

推荐答案

在我最初的问题中,我以为我会覆盖现有规则(左锚点),但实际上我正在为左锚点创建新规则,因此冲突和无法动画.

In my original question, I thought I was overwriting an existing rule (the left anchor) but actually I was creating a new rule for the left anchor, therefore the conflict and failure to animate.

在@vacawama的帮助下

With the help from @vacawama in the other question about changing AutoLayout rule, it is working now as below:

override func viewDidLoad() {
    super.viewDidLoad()

    let myView = UIView()
    myView.backgroundColor = UIColor.orangeColor()
    myView.translatesAutoresizingMaskIntoConstraints = false
    view.addSubview(myView)

    myView.topAnchor.constraintEqualToAnchor(view.topAnchor).active = true
    myView.widthAnchor.constraintEqualToAnchor(view.widthAnchor).active = true
    myView.heightAnchor.constraintEqualToAnchor(view.heightAnchor).active = true

    var leftConstraint = myView.leftAnchor.constraintEqualToAnchor(view.rightAnchor)
    leftConstraint?.active = true

    myView.layoutIfNeeded()

    /* try to deactivate a rule and create a new rule, then animate it */
    leftConstraint?.active = false
    leftConstraint = myView.leftAnchor.constraintEqualToAnchor(view.leftAnchor)
    leftConstraint?.active = true

    UIView.animateWithDuration(1.0) { self.view.layoutIfNeeded() }
}

但是,如果我将最后一行更改为在myView上调用layoutIfNeeded(),则它不是动画(欢迎任何评论).我实际上想在子视图而不是父视图上调用它,因为我不希望所有子视图都具有动画(在完整的用例中).因此,它可能需要一些解决方法,但是基本概念已经存在,并且已经解决了原始问题.

But if I change the last line to call layoutIfNeeded() on myView instead, it's not animating (any comment is welcome). I actually want to call it on a subview instead of a parent view because I don't want all subviews to animate (in a complete use case). So it probably needs some workaround, but the basic concept is there and have solved the original question.

这篇关于在Swift中,如何像自动滑动页面一样为UIView设置动画?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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