如何在Swift 4中为UIImage约束设置动画 [英] How to animate a UIImage constraints in Swift 4

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

问题描述

我正在制作纸牌应用程序,并且需要制作动画,以便纸牌可以更改其约束以移动到另一个地方。

I am making a card app and I need to make an animation so that a card would change its constraints to move to another place. How would I do this for a UIImage.

推荐答案

首先,您必须将约束拖放到视图控制器中,做一个出口。使用 UIImageView UITableView 等的相同方法。

First of all you have to drag and drop your constraint into your view controller, to make an outlet. The same way you are doing it with UIImageView or UITableView, etc.

实际上,对于那些看过WWDC(我猜是2014年)的人,苹果已经解释了如何以正确的方式对UI进行动画处理。

Actually, for those who have watched WWDC (2014 I guess), Apple have explained how to animate UI in the proper way.

首先,调用 layoutIfNeeded()方法以确保视图上的所有内容都已布局。在可以更改约束之后,紧接着在动画块中调用 layoutIfNeeded()方法,以新值对约束进行布局。

Firstly, you have to call layoutIfNeeded() method to make sure that everything on your view have laid out. After you can change your constraint and right after that in your animation block you call layoutIfNeeded() method to layout your constraint with the new value.

因此代码应如下所示:

view.layoutIfNeeded() // to make sure your view have laid out

self.constraint.constant = //change your constant to any value

UIView.animate(withDuration: 1.0) {
   self.view.layoutIfNeeded() //layout your constraint with the new value
}

这篇关于如何在Swift 4中为UIImage约束设置动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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