Ios 如何将UIButton转换为圆形?
当用户点击一个Ios 如何将UIButton转换为圆形?,ios,swift,uiview,Ios,Swift,Uiview,当用户点击一个ui按钮时,我试图将其动画化为圆形。但是当我尝试这样做时,我将ui视图的顶部设置为平面。在下面粘贴我的代码 private enum SignInButtonState { case clicked case normal } private var currentSignInButtonState: SignInButtonState = .normal private func updateSignInButton(to state:
ui按钮时,我试图将其动画化为圆形。但是当我尝试这样做时,我将ui视图的顶部设置为平面。在下面粘贴我的代码
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
}
}
}
你能给我们看一下currentSignInButtonState
拐角半径
是可设置动画的属性,你不需要变换。@mohmdads它被添加到代码中,now@vpoltave我使用“变换”将宽度调低以匹配高度。要获得1:1的纵横比,请附上结果的图像,好吗?我不明白。解决方案和我发布的问题之间有什么区别?you'r flag搞错了,我也不知道你在哪里调用我从didTap上假设的这个函数button@SidharthJDev试试看,它对我来说也很好,即使你在点击后从didLoad调用它也很好。您的错误是在您的案例中将其设置为clicked(单击),在您的案例被单击时将其设置为normal(正常)normal@SidharthJDev检查视频您的代码在状态TAT的标志设置中有一个错误,与您的代码有什么不同尝试在单击时调用它