我可以在 UIImage 中使用更多的切片线吗?

Can I use more slicing lines with UIImage?

我想动态更改矩形比例但保留半圆的比例。半圆应与底部对齐并水平居中:

如何向 Xcode 中的 UIImage 添加更多切片线?

我已经在 android 的 Nine Patch 编辑器中制作了我想要的东西,但它与 iOS 不兼容。

无法像您希望的那样拉伸 UIImage,但您可以通过编程方式进行。我已经在里面实现了一个带有 CAShapeLayer 的自定义 UIView:

import UIKit

class CustomShapeView: UIView {

    private let shapeLayer = CAShapeLayer()
    var radius = CGFloat(0)
    var shadowRadius = CGFloat(5)

    override func draw(_ rect: CGRect) {
        backgroundColor = UIColor.clear

        shapeLayer.frame = self.bounds
        shapeLayer.fillColor = UIColor.white.cgColor
        shapeLayer.path = createShapePath().cgPath

        shapeLayer.shadowPath = shapeLayer.path
        shapeLayer.shadowColor = UIColor.black.cgColor
        shapeLayer.shadowOffset = .zero
        shapeLayer.shadowRadius = shadowRadius
        shapeLayer.shadowOpacity = 0.5

        layer.addSublayer(shapeLayer)
    }

    func createShapePath () -> UIBezierPath {
        let path = UIBezierPath()

        let w = self.bounds.size.width
        let h = self.bounds.size.height
        let circleLever: CGFloat = radius * 0.552

        path.move(to: CGPoint(x: w/2 - radius, y: h))
        path.addLine(to: CGPoint(x: 0, y: h))
        path.addLine(to: CGPoint(x: 0, y: 0))
        path.addLine(to: CGPoint(x: w, y: 0))
        path.addLine(to: CGPoint(x: w, y: h))
        path.addLine(to: CGPoint(x: w/2 + radius, y: h))

        path.addCurve(to: CGPoint(x: w/2, y: h - radius), controlPoint1:CGPoint(x: w/2 + radius, y: h - circleLever), controlPoint2:CGPoint(x: w/2 + circleLever, y: h - radius))
        path.addCurve(to: CGPoint(x: w/2 - radius, y: h), controlPoint1:CGPoint(x: w/2 - circleLever, y: h - radius), controlPoint2:CGPoint(x: w/2 - radius, y: h - circleLever))
        path.close()
        path.move(to: CGPoint(x: w/2 - radius, y: h))

        return path
    }        
}

您可以设置任意大小和任意半径,圆心将始终位于视图的底部边缘。如果您愿意,您也可以更改 shadowRadius

Interface Builder 中的设置:

结果: