gpt4 book ai didi

ios - UIButton 具有可与 UIViewPropertyAnimator 互插的动画

转载 作者:行者123 更新时间:2023-11-30 12:00:41 24 4
gpt4 key购买 nike

我希望按钮在用户触摸按钮时展开(放大),并在触摸离开时缩小到正常状态。类似于iOS 11之前的iOS股票计算器。这是我创建按钮的子类的类。它使用touchesbegan和touchesend来启动、暂停和反转动画,但它没有正确收缩,也没有动画。

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,然后在短暂的延迟后将其返回到原始大小:

  /// Expands & Then Returns A UIView To It's Original Size
///
/// - Parameters:
/// - view: UIView
/// - duration: Double
/// - enable: (Void)
func animate(view: UIView, duration: Double, enable: @escaping () -> ()){

view.isUserInteractionEnabled = false

UIView.animate(withDuration: duration, animations: {

view.transform = CGAffineTransform(scaleX: 0.8, y: 0.8)

}) { (continueSequence) in

UIView.animate(withDuration: duration, animations: {

view.transform = .identity

}, completion: { (seqeunceFinished) in
enable()
})
}
}

然后您可以在 IBAction 中调用它,如下所示:

@IBAction func enlargeButton(_ sender: UIButton){

sender.isUserInteractionEnabled = false

enlarge(view: sender duration: 1) { sender.isUserInteractionEnabled = true }
}

关于ios - UIButton 具有可与 UIViewPropertyAnimator 互插的动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47276057/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com