将拐角半径应用于减小按钮

时间:2018-07-02 16:58:34

标签: 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()
    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用于.normal状态,但是当按下按钮时效果消失。实际上,我想向按钮添加阴影,并且可以添加阴影本身,但是我不知道如何组合阴影和圆角边缘。

现在看起来

looks now

我想看

I want to see

我不能使用button.backgroundColor代替button.setBackgroundColor,因为那样单击时按钮不会减少。我几周前发现了这一点: What is the method "backgroundRect(forBounds:)" of UIButton used for?

最终代码。只有阴影不起作用。但是拐角半径也只适用于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
}
} 

2 个答案:

答案 0 :(得分:1)

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

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

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

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
    }
}

答案 1 :(得分:0)

最后,此代码有效

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)
        }
    }
}

}

感谢所有帮助过我的人。