完美的 Swift3 Boing
Perfect Swift3 Boing
为栏打开设置动画...
@IBOutlet var barHeight: NSLayoutConstraint!
barHeight.constant = barShut?30:100
self.view.layoutIfNeeded()
t = !barShut?30:100
UIView.animate(withDuration: 0.15,
delay: 0,
options: UIViewAnimationOptions.curveEaseOut,
animations: { () -> Void in
self.barHeight.constant = t
self.view.layoutIfNeeded()
},
completion: {_ in
Screen.barShut = !Screen.barShut
}
)
太好了...
但是你怎么让它变成这样呢?
(我知道的唯一方法是使用 CADisplayLink,并使用几行代码实现 spring 衰减。)这在 UIKit 中可用吗?
您可以使用 UIView 内置的 spring animation method:
func toggleBar() -> Void {
self.view.layoutIfNeeded()
let newHeight:CGFloat = !barShut ? 30:100
barShut = !barShut
barHeightConstraint.constant = newHeight
UIView.animate(withDuration: 1.5, delay: 0, usingSpringWithDamping: 0.2, initialSpringVelocity: 3, options: [], animations: {
self.view.layoutIfNeeded()
}, completion: nil)
}
您需要比 0.15 秒更长的动画持续时间,以使弹跳看起来逼真;我认为我的值看起来不错,但您可以使用它们来获得您想要的确切效果。
由于动画持续时间较长,我发现我可以点击触发 open/shut 的按钮,而之前的动画仍然是 运行。在完成块中设置 barShut
意味着该栏不会对所有点击做出反应。我将开关移到了动画之外以解决这个问题。
为栏打开设置动画...
@IBOutlet var barHeight: NSLayoutConstraint!
barHeight.constant = barShut?30:100
self.view.layoutIfNeeded()
t = !barShut?30:100
UIView.animate(withDuration: 0.15,
delay: 0,
options: UIViewAnimationOptions.curveEaseOut,
animations: { () -> Void in
self.barHeight.constant = t
self.view.layoutIfNeeded()
},
completion: {_ in
Screen.barShut = !Screen.barShut
}
)
太好了...
但是你怎么让它变成这样呢?
(我知道的唯一方法是使用 CADisplayLink,并使用几行代码实现 spring 衰减。)这在 UIKit 中可用吗?
您可以使用 UIView 内置的 spring animation method:
func toggleBar() -> Void {
self.view.layoutIfNeeded()
let newHeight:CGFloat = !barShut ? 30:100
barShut = !barShut
barHeightConstraint.constant = newHeight
UIView.animate(withDuration: 1.5, delay: 0, usingSpringWithDamping: 0.2, initialSpringVelocity: 3, options: [], animations: {
self.view.layoutIfNeeded()
}, completion: nil)
}
您需要比 0.15 秒更长的动画持续时间,以使弹跳看起来逼真;我认为我的值看起来不错,但您可以使用它们来获得您想要的确切效果。
由于动画持续时间较长,我发现我可以点击触发 open/shut 的按钮,而之前的动画仍然是 运行。在完成块中设置 barShut
意味着该栏不会对所有点击做出反应。我将开关移到了动画之外以解决这个问题。