Swift 将“拐角半径”应用于“递减”按钮

Swift 将“拐角半径”应用于“递减”按钮,swift,uibutton,shadow,cornerradius,Swift,Uibutton,Shadow,Cornerradius,我有一个非常好看的按钮 import UIKit class NiseButtonVC: UIViewController { var button = MyShrinkingButton() var button2 = UIButton() override func viewDidLoad() { super.viewDidLoad() view.backgroundColor = .white button = MyShrinkingButton() but

我有一个非常好看的按钮

import UIKit
class NiseButtonVC: UIViewController {
var button = MyShrinkingButton()
var button2 = UIButton()
override func viewDidLoad() {
    super.viewDidLoad()
    view.backgroundColor = .white
    button = MyShrinkingButton()
    button.bounds = CGRect(x: 0, y: 0, width: 200, height: 80)

    button.center = view.center
    button.addTarget(self, action: #selector(buttonPressed), for: .touchUpInside)


    button.setTitle("button", for: .normal)
    button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 40)
    button.titleLabel?.adjustsFontSizeToFitWidth = true
    let backGroundImage = UIImage.from(color: .orange)
    button.setBackgroundImage(backGroundImage, for: .normal)
    button.adjustsImageWhenHighlighted = false
//        button.layer.cornerRadius = 10
//        button.layer.masksToBounds = true
    view.addSubview(button)
 }
@objc func buttonPressed(sender: UIButton){
    print("button pressed")

}
}

extension CGSize {
func sizeByDelta(dw:CGFloat, dh:CGFloat) -> CGSize {
    return CGSize(width:self.width + dw, height:self.height + dh)
}
}
class MyShrinkingButton: UIButton {

override func backgroundRect(forBounds bounds: CGRect) -> CGRect {
    var result = super.backgroundRect(forBounds:bounds)
    if self.isHighlighted {
        result = result.insetBy(dx: 3, dy: 3)
    }
    return result
}
override var intrinsicContentSize : CGSize {
    return super.intrinsicContentSize.sizeByDelta(dw:25, dh: 20)
}

override func titleRect(forContentRect bounds: CGRect) -> CGRect {
    var result = super.titleRect(forContentRect:bounds)
    if self.isHighlighted {
        result = result.insetBy(dx: 3, dy: 3)
    }
    return result
}

}

extension UIImage {
static func from(color: UIColor) -> UIImage {
    let rect = CGRect(x: 0, y: 0, width: 1, height: 1)
    UIGraphicsBeginImageContext(rect.size)
    let context = UIGraphicsGetCurrentContext()
    context!.setFillColor(color.cgColor)
    context!.fill(rect)
    let img = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return img!
}
}
但我希望按钮有圆形的边缘。我可以在正常状态下使用cornerRadius,但按下按钮后效果消失。事实上,我想添加阴影按钮,我可以添加阴影本身,但我不知道如何结合阴影和圆角边缘

现在看起来像

我想看看

我不能使用button.backgroundColor而不是button.setBackgroundColor,因为在单击时按钮不会减少。这是我几周前发现的:

最终代码。只有阴影不起作用。但拐角半径也仅适用于viewDidLoad:

import UIKit
class ViewController: UIViewController {

var button = ShrinkingButton()
override func viewDidLoad() {
    super.viewDidLoad()

    button = ShrinkingButton()
    button.bounds = CGRect(x: 0, y: 0, width: 200, height: 80)

    button.center = view.center
    button.setTitle("button", for: .normal)
    button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 40)
    button.titleLabel?.adjustsFontSizeToFitWidth = true
    button.backgroundColor = .orange
    button.setTitleColor(.white, for: .normal)
    button.adjustsImageWhenHighlighted = false
    button.layer.cornerRadius = 10
    button.layer.masksToBounds = true
    view.addSubview(button)
}
}


class ShrinkingButton: UIButton {

override var isHighlighted: Bool {
    didSet {
        UIView.animate(withDuration: 0.1) {
            self.isHighlighted ?     self.layer.setAffineTransform(CGAffineTransform(scaleX: 0.9, y: 0.9 )) :
                self.layer.setAffineTransform(.identity)
        }
    }
}

override func awakeFromNib() {
    super.awakeFromNib()

    layer.cornerRadius = 10

    layer.shadowOffset = CGSize(width: -1, height: 2)
    layer.shadowRadius = 5
    layer.shadowOpacity = 0.5
}
} 

该按钮将消失,因为您仅将背景图像设置为正常状态

这意味着对于任何其他状态,按钮都没有背景图像

更好的方法是设置背景颜色而不是图像

button.backgroundColor = .orange
如果你想使用背景图像,而不仅仅是一种颜色,那么也许你可以使用这个

button.setBackgroundImage(image, for: UIControlState())
使用showdow在压力机上创建收缩效果的步骤

import UIKit

class ShrinkingButton: UIButton {

    override var isHighlighted: Bool {
        didSet {
            UIView.animate(withDuration: 0.1) {
                self.isHighlighted ? self.layer.setAffineTransform(CGAffineTransform(scaleX: 0.9, y: 0.9 )) :
                self.layer.setAffineTransform(.identity)
            }
        }
    }

    override func awakeFromNib() {
        super.awakeFromNib()

        layer.cornerRadius = 10

        layer.shadowOffset = CGSize(width: -1, height: 2)
        layer.shadowRadius = 5
        layer.shadowOpacity = 0.5
    }
}

最后,此代码有效

import UIKit

class NiseButtonwhithShadowVC: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    // constants
    let radius: CGFloat = 20, dimension: CGFloat = 200, offset = 4
    let frame = CGRect(x: 0, y: 0, width: 200, height: 50)

    // custom view
    let customView = ShrinkingButton2(frame: frame)
    customView.setTitle("Button", for: .normal)

    // image layer
    let imageLayer = CALayer()
    imageLayer.backgroundColor = UIColor.orange.cgColor
    imageLayer.masksToBounds = true
    imageLayer.frame = frame
    imageLayer.cornerRadius = radius
    imageLayer.masksToBounds = true

    // rounded layer
    let roundedLayer = CALayer()
    roundedLayer.shadowColor = UIColor.gray.cgColor
    roundedLayer.shadowPath = UIBezierPath(roundedRect: CGRect(x: 0, y: 0, width: dimension, height: 50), cornerRadius: radius).cgPath
    roundedLayer.shadowOffset = CGSize(width: offset, height: offset)
    roundedLayer.shadowOpacity = 0.5
    roundedLayer.shadowRadius = 2
    roundedLayer.frame = frame

    // views and layers hierarchy
    customView.layer.addSublayer(imageLayer)
    customView.layer.insertSublayer(roundedLayer, below: imageLayer)
    view.addSubview(customView)

    // layout
    customView.center = CGPoint(x: view.bounds.midX, y: view.bounds.midY)

}

}

class ShrinkingButton2: UIButton {

override var isHighlighted: Bool {
    didSet {
        UIView.animate(withDuration: 0.1) {
            self.isHighlighted ? self.layer.setAffineTransform(CGAffineTransform(scaleX: 0.95, y: 0.95 )) :
                self.layer.setAffineTransform(.identity)
        }
    }
}

}

感谢所有帮助过我的人。

请展示,你的按钮应该是什么样子的?我尝试添加图像。但整个代码都在这里。如果你现在启动它,我会看看它的样子。如果取消注释第21行和第22行,您将看到它的外观。但当你点击按钮时,按钮应该保持比例。为此,你可以使用普通按钮并设置相同的背景色。这是非常简单和可取的,而不是创建自定义类。嗨,僵尸的答案可能是正确的。你的问题现在存在吗?不,我不能。具有背景色的普通按钮在按下时不会减少。不,我尝试了uicontrol状态(),但它仍然不起作用。使用utton.backgroundColor=.orange,按下按钮时,按钮甚至不会减少。@Sergey检查我的更新答案,以缩小按钮(应与背景色和layer.cornerRadius配合使用)是的,它可以工作。非常感谢。但有几个细微差别。这个按钮的反应有点慢——起初我以为它听起来很慢,但实际上慢了一点。当然,这不是一般的问题。我也看不到按钮的标题,尽管它已设置。@Sergey我通过更改持续时间使它快了一点,动画也在覆盖范围内。我没有注意到“UIView.animate(withDuration:0.1)”:(现在好多了,但我还是看不到标题。