我正在使用CGAffineTransform缩小点击按钮。按钮的宽度会缩小,但会从中间左右会合。但是,我希望按钮从右侧缩小。一个奇怪的比喻就像按钮是一张纸,我点了一个匹配的右侧,它烧了纸,向左摧毁它。我如何实现这样的功能?
func animateStartButtonExit() {
UIView.animate(withDuration: 0.5,
animations: {
self.startButtonOutlet.transform = CGAffineTransform(scaleX: 0.1, y: 1)
},
completion: { _ in
UIView.animate(withDuration: 0.5) {
self.startButtonOutlet.isHidden = true
self.startButtonOutlet.transform = CGAffineTransform.identity
}
})
}
答案 0 :(得分:1)
一切正常。我刚刚测试过。
class ViewController: UIViewController {
@IBOutlet weak var button: UIButton!
@IBOutlet weak var widthConstraint: NSLayoutConstraint!
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func click(_ sender: UIButton) {
widthConstraint.constant = 10
UIView.animate(withDuration: 5, animations: {
self.view.layoutIfNeeded()
}, completion: { _ in
self.button.isHidden = true
// For example, back to the normal width
self.widthConstraint.constant = 168
})
}
}
答案 1 :(得分:0)
您可以尝试使用右侧的按钮设置动画,
UIView.animate(withDuration: 5,
animations: {
button.frame = CGRect(x: button.frame.minX, y: button.frame.minY, width: button.frame.size.width / 2, height: button.frame.size.height / 2)
},
completion: { _ in
UIView.animate(withDuration: 0.5) {
button.isHidden = true
button.frame = CGRect(x: button.frame.minX, y: button.frame.minY, width: button.frame.size.width * 2, height: button.frame.size.height * 2)
}
})
答案 2 :(得分:0)
如果您使用的是自动布局,请设置尾随空格约束以使宽度为0。
即
rightConstraint.constant = rightConstraint.constant + width
UIView.animate(withDuration: 5,
animations: {
button.layoutIfNeeded()
},
completion: nil
)