有什么方法可以使用缓动曲线为高度约束设置动画吗?我正在考虑使用我想要的值和时间设置 CAKeyFrameAnimation
的某种方法,并以某种方式影响高度约束。
我不确定是否有一个单独的动画类可以用于约束关键帧,或者我是否缺少 CAKeyFrameAnimation
允许我使用该类的东西,或者如果这是不可能的。
编辑:这是我正在尝试为我工作的东西,如果有人知道我是否走在正确的道路上,我将不胜感激一些指导:
self.heightConstraint.constant = newHeight
let animation = CAKeyframeAnimation(keyPath: "frame.size.height")
animation.values = [self.frame.size.height, newHeight]
animation.keyTimes = [0, 1]
animation.duration = self.animationDuration
animation.delegate = self
self.layer.add(animation, forKey: "heightChange")
newHeight
可以是我想要的高度。但这只是在没有任何动画的情况下使 View 更高。我是否正确使用它?这可能与约束有关吗?
编辑 2: 我应该补充一点,我想使用比 Apple 作为 UIView.animate(withDuraiton:...
的一部分提供的默认值更复杂的缓动函数。
最佳答案
不需要使用 CAKeyframeAnimation。 UIView 动画会缓和动画到一个约束,就像任何其他动画一样:
myConstraintOutlet.constant = someNewValue
UIView.animate(
duration: 0.5,
delay: 0.0,
options: .curveEaseInOut, //Use ease-in, ease-out timing.
animations: {
self.view.layoutIfNeeded()
},
completion: nil)
编辑:
如果你想要自定义时间,你也可以使用基于 UIView 的关键帧动画和 animateKeyframes(withDuration:delay:options:animations:completion:)
方法(UIView 动画要容易得多使用比 CAAnimations
。)
编辑#2:
或者,如果您只需要一条不同的三次缓动曲线,您也可以这样做。请参阅此链接的最后一点:https://medium.com/@RobertGummesson/a-look-at-uiview-animation-curves-part-3-edde651b6a7a
关键是来自该链接的代码片段:
circleView.transform = CGAffineTransformMakeScale(0, 0)
let timingFunction = CAMediaTimingFunction(controlPoints: 5/6, 0.2, 2/6, 0.9)
CATransaction.begin()
CATransaction.setAnimationTimingFunction(timingFunction)
UIView.animateWithDuration(1) {
self.circleView.transform = CGAffineTransformIdentity
}
CATransaction.commit()
(这不是我的代码,是 Robert Gummesson 的,来自上面的链接。编写它的所有功劳归于他。)
关于ios - 使用缓动曲线动画高度约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45063989/