gpt4 book ai didi

ios - Swift - 圆角半径和阴影问题

转载 作者:IT王子 更新时间:2023-10-29 04:58:01 33 4
gpt4 key购买 nike

我正在尝试创建一个带有圆角阴影 的按钮。无论我如何切换,按钮都不会正确显示。我试过 masksToBounds = falsemasksToBounds = true,但是要么角半径有效但阴影无效,要么阴影有效但角半径不剪切按钮的角。

import UIKit
import QuartzCore

@IBDesignable
class Button : UIButton
{
@IBInspectable var masksToBounds: Bool = false {didSet{updateLayerProperties()}}
@IBInspectable var cornerRadius : CGFloat = 0 {didSet{updateLayerProperties()}}
@IBInspectable var borderWidth : CGFloat = 0 {didSet{updateLayerProperties()}}
@IBInspectable var borderColor : UIColor = UIColor.clearColor() {didSet{updateLayerProperties()}}
@IBInspectable var shadowColor : UIColor = UIColor.clearColor() {didSet{updateLayerProperties()}}
@IBInspectable var shadowOpacity: CGFloat = 0 {didSet{updateLayerProperties()}}
@IBInspectable var shadowRadius : CGFloat = 0 {didSet{updateLayerProperties()}}
@IBInspectable var shadowOffset : CGSize = CGSizeMake(0, 0) {didSet{updateLayerProperties()}}

override func drawRect(rect: CGRect)
{
updateLayerProperties()
}

func updateLayerProperties()
{
self.layer.masksToBounds = masksToBounds
self.layer.cornerRadius = cornerRadius
self.layer.borderWidth = borderWidth
self.layer.borderColor = borderColor.CGColor
self.layer.shadowColor = shadowColor.CGColor
self.layer.shadowOpacity = CFloat(shadowOpacity)
self.layer.shadowRadius = shadowRadius
self.layer.shadowOffset = shadowOffset
}
}

最佳答案

以下 Swift 5/iOS 12 代码显示了如何设置 UIButton 的子类,允许创建带有圆角和阴影的实例:

import UIKit

final class CustomButton: UIButton {

private var shadowLayer: CAShapeLayer!

override func layoutSubviews() {
super.layoutSubviews()

if shadowLayer == nil {
shadowLayer = CAShapeLayer()
shadowLayer.path = UIBezierPath(roundedRect: bounds, cornerRadius: 12).cgPath
shadowLayer.fillColor = UIColor.white.cgColor

shadowLayer.shadowColor = UIColor.darkGray.cgColor
shadowLayer.shadowPath = shadowLayer.path
shadowLayer.shadowOffset = CGSize(width: 2.0, height: 2.0)
shadowLayer.shadowOpacity = 0.8
shadowLayer.shadowRadius = 2

layer.insertSublayer(shadowLayer, at: 0)
//layer.insertSublayer(shadowLayer, below: nil) // also works
}
}

}

根据您的需要,您可以在 Storyboard 中添加一个 UIButton 并将其类设置为 CustomButton 或者您可以创建一个 CustomButton 以编程方式。以下 UIViewController 实现展示了如何以编程方式创建和使用 CustomButton 实例:

import UIKit

class ViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()

let button = CustomButton(type: .system)
button.setTitle("Button", for: .normal)
view.addSubview(button)

button.translatesAutoresizingMaskIntoConstraints = false
let horizontalConstraint = button.centerXAnchor.constraint(equalTo: view.centerXAnchor)
let verticalConstraint = button.centerYAnchor.constraint(equalTo: view.centerYAnchor)
let widthConstraint = button.widthAnchor.constraint(equalToConstant: 100)
let heightConstraint = button.heightAnchor.constraint(equalToConstant: 100)
NSLayoutConstraint.activate([horizontalConstraint, verticalConstraint, widthConstraint, heightConstraint])
}

}

前面的代码在 iPhone 模拟器中生成了下面的图像:

enter image description here

关于ios - Swift - 圆角半径和阴影问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24644802/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com