如何同时绘制不同动画曲线的AutoLayout约束?
例如,用线性曲线动画widthAnchor
和heightAnchor
,用spring动画动画centerXAnchor
和centerYAnchor
(在相同的视图上)。两种动画都需要同时进行。
我只知道如何同时给每个人动画(调用view.layoutIfNeeded()
is UIView.animate
),但是如何将它们分开动画呢?
发布于 2018-04-20 09:39:19
你必须执行如下操作:-
UIView.animate(withDuration: 0.2, animations: {
//perform animation of height
}, completion: { _ in
UIView.animate(withDuration: 0.2, animations: {
//perform animation of width
}, completion: nil)
})
如果您想要添加一些动画选项,那么使用下面的代码:--
UIView.animateWithDuration(0.2, delay: 0.2, options: UIViewAnimationOptions."Set your Animationoption", animations: {
//perform animation of height
}, completion: { _ in
UIView.animateWithDuration(0.2, delay: 0.2, options: UIViewAnimationOptions."Set your Animationoption", animations: {
//perform animation of width
}, completion: nil)
})
两者并驾齐驱:-
UIView.animateWithDuration(0.2, delay: 0.2, options: UIViewAnimationOptions."Set your Animationoption", animations: {
//perform animation of width
}, completion: nil)
UIView.animateWithDuration(0.2, delay: 0.2, options: UIViewAnimationOptions."Set your Animationoption", animations: {
//perform animation of height
}, completion: nil)
谢谢。
发布于 2018-07-04 05:19:28
您应该尝试创建CABasicAnimation
,其中fromValue
设置为每个视图的当前值,并在此之后调用layoutIfNeeded()
,而不使用动画块。后者将改变视图的框架。如果在布局步骤之后添加动画,则修改应该是动画。
这可能是这样的
let animation1 = CABasicAnimation()
let animation2 = CABasicAnimation()
animation1.fromValue = view1.center
animation1... // Set further properties to configure the animation
animation2.fromValue = ... // Same with second view
// and so on
self.view.layoutIfNeeded()
view1.layer.add(animation1, forKey: "center")
view2.layer.add(animation2, forKey: ...)
我还没试过,但我很乐意听听它是否管用。
https://stackoverflow.com/questions/49938172
复制相似问题