如何复制 Apple Maps App UIButtons?

How can I replicate Apple Maps App UIButtons?

我想让按钮看起来像这样:

有谁知道如何在 Swift 中制作这样的按钮? (特别是上图,一帧内有 2 或 3 个按钮,用细线分隔)。

谢谢!!!

创建视图并遮住角

yourView.layer.maskedCorners = [.layerMaxXMaxYCorner, .layerMinXMaxYCorner] // this is horizontal , you can change for vertical

在里面放一个堆栈视图,然后用按钮填充它,给它们间距0,就是这样

幸运的是,所有图像都存在于 SF Symbols 中,因此 none 必须从头开始绘制。

您可以创建自定义视图:

class CustomView: UIImageView {
    init(frame: CGRect, corners: CACornerMask, systemName: String) {
        super.init(frame: frame)
        self.createBorders(corners: corners)
        self.createImage(systemName: systemName)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func createBorders(corners: CACornerMask) {
        self.contentMode = .scaleAspectFill
        self.clipsToBounds = false
        self.layer.cornerRadius = 20
        self.layer.maskedCorners = corners
        self.layer.masksToBounds = false
        self.layer.shadowOffset = .zero
        self.layer.shadowColor = UIColor.gray.cgColor
        self.layer.shadowRadius = 20
        self.layer.shadowOpacity = 0.2
        self.backgroundColor = .white
        let shadowAmount: CGFloat = 2
        let rect = CGRect(x: 0, y: 2, width: self.bounds.width + shadowAmount * 0.1, height: self.bounds.height + shadowAmount * 0.1)
        self.layer.shadowPath = UIBezierPath(rect: rect).cgPath

    }
    
    func createImage(systemName: String) {
        let image = UIImage(systemName: systemName)!
        let renderer = UIGraphicsImageRenderer(bounds: self.frame)
        let renderedImage = renderer.image { (_) in
            image.draw(in: frame.insetBy(dx: 30, dy: 30))
        }
        self.image = renderedImage.withRenderingMode(.alwaysTemplate)
    }
}

并像这样使用它:

let size = CGSize(width: 100, height: 100)
let frame = CGRect(origin: CGPoint(x: 100, y: 100), size: size)

let infoView = CustomView(frame: frame, corners: [.layerMinXMinYCorner, .layerMaxXMinYCorner], systemName: "info.circle")
let locationView = CustomView(frame: frame, corners: [], systemName: "location")
let twoDView = CustomView(frame: frame, corners: [.layerMinXMaxYCorner, .layerMaxXMaxYCorner], systemName: "view.2d")
let binocularsView = CustomView(frame: frame, corners: [.layerMinXMinYCorner, .layerMinXMaxYCorner, .layerMaxXMinYCorner, .layerMaxXMaxYCorner], systemName: "binoculars.fill")

let stackView = UIStackView(arrangedSubviews: [infoView, locationView, twoDView, binocularsView])
stackView.frame = CGRect(origin: CGPoint(x: 100, y: 100), size: CGSize(width: 100, height: 400))
stackView.distribution = .fillEqually
stackView.axis = .vertical
stackView.setCustomSpacing(20, after: twoDView)
view.addSubview(stackView)