如何将 UIButton 转换为圆形?
How can I transform UIButton to circle?
我正在尝试在用户单击时将 UIButton
动画化为圆形。但是当我尝试这样做时,UIView
的顶部变得平坦。在下面粘贴我的代码。
private enum SignInButtonState {
case clicked
case normal
}
private var currentSignInButtonState: SignInButtonState = .normal
private func updateSignInButton(to state: SignInButtonState) {
switch state {
case .clicked:
signinButton.setTitle("", for: .normal)
self.currentSignInButtonState = .clicked
UIView.animate(withDuration: 0.5) {
self.signinButton.transform = CGAffineTransform(scaleX: self.signinButton.frame.height/self.signinButton.frame.width, y: 1)
self.signinButton.layer.cornerRadius = self.signinButton.frame.height/2
}
case .normal:
signinButton.setTitle("Sign In", for: .normal)
self.currentSignInButtonState = .normal
UIView.animate(withDuration: 0.5) {
self.signinButton.transform = CGAffineTransform(scaleX: 1, y: 1)
self.signinButton.layer.cornerRadius = 5.0
}
}
}
我也试过添加
self.clipsToBounds = true
self.layer.masksToBounds = true
都没有帮助。尝试将形状更改为圆形时添加 UIButton 的图像(黄色按钮)
在你更新后,我发现了你的宽度,问题出在转换中,因为它使 .cornerRadius
行为不正常,尝试另一种方法可以解决它
- 我可以看到你的宽度==查看宽度
所以我们需要创建另一个具有常量值的宽度约束
并在代码中为两者创建 IBOutlets
,并在单击时更改每个的优先级并在更改角半径时更改值,检查下面的
@IBOutlet weak var secondWidthConstant: NSLayoutConstraint! // this is the constant one
@IBOutlet weak var widthOfBtn: NSLayoutConstraint! // this is the one that has == to super.view
private func updateSignInButton(to state: SignInButtonState) {
switch state {
case .clicked:
signinButton.setTitle("", for: .normal)
self.currentSignInButtonState = .normal
widthOfBtn.priority = UILayoutPriority(rawValue: 750) // this one is the constraint that is == to the width of the view
secondWidthConstant.priority = UILayoutPriority(rawValue: 1000) // this is the constant value constraint
secondWidthConstant.constant = self.signinButton.frame.height // change the constant after the priorty is set to higher than the == one
UIView.animate(withDuration: 0.5, animations: {
self.signinButton.layoutIfNeeded()
self.signinButton.layer.cornerRadius = (self.signinButton.frame.height / 2)
}) { (_) in
}
case .normal:
widthOfBtn.priority = UILayoutPriority(rawValue: 1000) //switch back priorty
secondWidthConstant.priority = UILayoutPriority(rawValue: 750) //switch back priorty
signinButton.setTitle("Sign In", for: .normal)
self.currentSignInButtonState = .clicked
UIView.animate(withDuration: 0.5) {
self.signinButton.layoutIfNeeded()
self.signinButton.layer.cornerRadius = 5.0
}
}
}
我正在尝试在用户单击时将 UIButton
动画化为圆形。但是当我尝试这样做时,UIView
的顶部变得平坦。在下面粘贴我的代码。
private enum SignInButtonState {
case clicked
case normal
}
private var currentSignInButtonState: SignInButtonState = .normal
private func updateSignInButton(to state: SignInButtonState) {
switch state {
case .clicked:
signinButton.setTitle("", for: .normal)
self.currentSignInButtonState = .clicked
UIView.animate(withDuration: 0.5) {
self.signinButton.transform = CGAffineTransform(scaleX: self.signinButton.frame.height/self.signinButton.frame.width, y: 1)
self.signinButton.layer.cornerRadius = self.signinButton.frame.height/2
}
case .normal:
signinButton.setTitle("Sign In", for: .normal)
self.currentSignInButtonState = .normal
UIView.animate(withDuration: 0.5) {
self.signinButton.transform = CGAffineTransform(scaleX: 1, y: 1)
self.signinButton.layer.cornerRadius = 5.0
}
}
}
我也试过添加
self.clipsToBounds = true
self.layer.masksToBounds = true
都没有帮助。尝试将形状更改为圆形时添加 UIButton 的图像(黄色按钮)
在你更新后,我发现了你的宽度,问题出在转换中,因为它使 .cornerRadius
行为不正常,尝试另一种方法可以解决它
- 我可以看到你的宽度==查看宽度
所以我们需要创建另一个具有常量值的宽度约束
并在代码中为两者创建 IBOutlets
,并在单击时更改每个的优先级并在更改角半径时更改值,检查下面的
@IBOutlet weak var secondWidthConstant: NSLayoutConstraint! // this is the constant one
@IBOutlet weak var widthOfBtn: NSLayoutConstraint! // this is the one that has == to super.view
private func updateSignInButton(to state: SignInButtonState) {
switch state {
case .clicked:
signinButton.setTitle("", for: .normal)
self.currentSignInButtonState = .normal
widthOfBtn.priority = UILayoutPriority(rawValue: 750) // this one is the constraint that is == to the width of the view
secondWidthConstant.priority = UILayoutPriority(rawValue: 1000) // this is the constant value constraint
secondWidthConstant.constant = self.signinButton.frame.height // change the constant after the priorty is set to higher than the == one
UIView.animate(withDuration: 0.5, animations: {
self.signinButton.layoutIfNeeded()
self.signinButton.layer.cornerRadius = (self.signinButton.frame.height / 2)
}) { (_) in
}
case .normal:
widthOfBtn.priority = UILayoutPriority(rawValue: 1000) //switch back priorty
secondWidthConstant.priority = UILayoutPriority(rawValue: 750) //switch back priorty
signinButton.setTitle("Sign In", for: .normal)
self.currentSignInButtonState = .clicked
UIView.animate(withDuration: 0.5) {
self.signinButton.layoutIfNeeded()
self.signinButton.layer.cornerRadius = 5.0
}
}
}