【发布时间】:2018-04-26 19:51:39
【问题描述】:
我想要一个按钮在用户触摸按钮时展开(放大),并在触摸离开时缩回正常。类似于 iOS 11 之前的 iOS 股票计算器。这是我创建按钮的子类的类。它使用 touchesbegan 和 touchesended 来启动、暂停和反转动画,但它没有正确收缩,也没有动画。
class UIOutlineButton: UIButton {
var squishAnimation:UIPropertyAnimator!
override public func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesBegan(touches, with: event)
squishAnimation = UIViewPropertyAnimator(duration: 0.5, dampingRatio: 30, animations: {
let scale = self.transform.scaledBy(x: 0.8, y: 0.8)
let rotate = self.transform.rotated(by: 0.3)
self.transform.concatenating(rotate)
self.transform = scale
})
squishAnimation.isReversed = true
squishAnimation.startAnimation()
self.next?.touchesBegan(touches, with: event)
}
override public func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesEnded(touches, with: event)
colorAnimation = UIViewPropertyAnimator(duration: 0.5, dampingRatio: 30, animations: {
})
squishAnimation = UIViewPropertyAnimator(duration: 0.5, dampingRatio: 30, animations: {
self.transform = CGAffineTransform.identity
})
squishAnimation.stopAnimation(true)
squishAnimation.startAnimation()
}
}
【问题讨论】:
-
好的,所以把它分解成几个步骤。您可以使用触地内部事件来触发增长吗?然后补内+补外使其缩回正常?对于动画,请查看 UIView 方法,例如
animate(duration:animations)。看看你能不能解决。如果没有,请发布您的代码并告诉我们它如何无法满足您的需求,我们会为您提供帮助。