【问题标题】:Swift: Reset frame of a UIView to its constraints after animationSwift:动画后将 UIView 的帧重置为其约束
【发布时间】:2018-01-17 00:30:32
【问题描述】:
我正在使用在viewDidLoad 中以编程方式添加的约束来设置 UIImageView。
稍后,在用户与屏幕上的某些按钮进行交互后,我通过使用 CAKeyframeAnimation with an array ofCGRect` 的移动值更改其框架来移动视图。
现在在应用程序生命周期的后期,我希望 UIImageView 返回到它最初放置的位置(使用我没有更改的约束)。有没有办法将框架重置为其初始约束?
【问题讨论】:
标签:
ios
swift
uiview
constraints
cakeyframeanimation
【解决方案1】:
可以使用removeAnimation、文档here 恢复为原始值。所以如果使用设置你的动画(比如move),比如:
let animation = CAKeyframeAnimation()
animation.keyPath = "position.y"
animation.values = [0, 200]
animation.keyTimes = [0, 1]
animation.duration = 1
animation.isAdditive = true
animation.fillMode = kCAFillModeForwards
animation.isRemovedOnCompletion = false
innerView.layer.add(animation, forKey: "move")
那么当你需要恢复时,你可以调用:
innerView.layer.removeAnimation(forKey: "move")