高度约束动画'jumping'
Height constraint animation 'jumping'
我一直在创建自己的 UIControl 子类以用于我的调整 iDunnoU。我已经完成了 UIControl,但 expand/collapse 动画除外。这个动画的问题是它 'jumps' down/up 当 expanding/collapsing 时,而不是像我原来的模型那样平滑地散开(见下文)。
我已将代码上传到 GitHub 存储库,找到 here. The code for adding the control to the superview can be found here, the code for setting up the height constraint can be found here, and the code for animating the height constraint can be found here。
UIView.animate()
可能有点棘手——您需要在正确的视图上调用 .layoutIfNeeded()
。
将 iDUMenuButton
class 中的 isExpanded / didSet
替换为:
var isExpanded = false {
didSet {
if isExpanded != oldValue {
if isExpanded {
becomeFirstResponder()
let haptics = UIImpactFeedbackGenerator(style: .rigid)
haptics.impactOccurred()
}
guard let sv = self.superview else {
// shouldn't happen, but let's be thorough
fatalError("Self must have a superview!!!")
}
// not needed
//self.layoutIfNeeded()
UIView.animate(withDuration: 0.3) {
self.heightConstraint.isActive = !self.isExpanded
// call .layoutIfNeeded() on self's superview
//self.layoutIfNeeded()
sv.layoutIfNeeded()
self.layer.shadowOpacity = self.isExpanded ? 1 : 0
self.buttons.forEach { [=10=].setBadgeHidden(hidden: !self.isExpanded, animated: true) }
}
delegate?.menuButton(self, isExpandedDidUpdate: isExpanded)
}
}
}
我一直在创建自己的 UIControl 子类以用于我的调整 iDunnoU。我已经完成了 UIControl,但 expand/collapse 动画除外。这个动画的问题是它 'jumps' down/up 当 expanding/collapsing 时,而不是像我原来的模型那样平滑地散开(见下文)。
我已将代码上传到 GitHub 存储库,找到 here. The code for adding the control to the superview can be found here, the code for setting up the height constraint can be found here, and the code for animating the height constraint can be found here。
UIView.animate()
可能有点棘手——您需要在正确的视图上调用 .layoutIfNeeded()
。
将 iDUMenuButton
class 中的 isExpanded / didSet
替换为:
var isExpanded = false {
didSet {
if isExpanded != oldValue {
if isExpanded {
becomeFirstResponder()
let haptics = UIImpactFeedbackGenerator(style: .rigid)
haptics.impactOccurred()
}
guard let sv = self.superview else {
// shouldn't happen, but let's be thorough
fatalError("Self must have a superview!!!")
}
// not needed
//self.layoutIfNeeded()
UIView.animate(withDuration: 0.3) {
self.heightConstraint.isActive = !self.isExpanded
// call .layoutIfNeeded() on self's superview
//self.layoutIfNeeded()
sv.layoutIfNeeded()
self.layer.shadowOpacity = self.isExpanded ? 1 : 0
self.buttons.forEach { [=10=].setBadgeHidden(hidden: !self.isExpanded, animated: true) }
}
delegate?.menuButton(self, isExpandedDidUpdate: isExpanded)
}
}
}