ios UIView对角边框位于一角

yqkkidmi  于 2022-11-19  发布在  iOS
关注(0)|答案(3)|浏览(131)

我有一个视图,需要在一侧显示一个斜角。当视图的背景色如下所示时,我已经完成了该操作:

但我也需要它显示一个清晰的背景。设置它的背景清晰,并添加一个边框后,这是输出:

下面是我用来创建对角的自定义视图的代码:

class PointedView: UIImageView {
    @IBInspectable var borderColor: UIColor = UIColor.clear {
        didSet {
            layer.borderColor = borderColor.cgColor
        }
    }
    
    @IBInspectable var borderWidth: CGFloat = 0 {
        didSet {
            layer.borderWidth = borderWidth
        }
    }
    
    @IBInspectable
    /// Percentage of the slant based on the width
    var slopeFactor: CGFloat = 15 {
        didSet {
            updatePath()
        }
    }
    
    private let shapeLayer: CAShapeLayer = {
        let shapeLayer = CAShapeLayer()
        shapeLayer.lineWidth = 0
        
        // with masks, the color of the shape layer doesn’t matter;
        // it only uses the alpha channel; the color of the view is
        // dictate by its background color
        shapeLayer.fillColor = UIColor.white.cgColor
        return shapeLayer
    }()
    
    override func layoutSubviews() {
        super.layoutSubviews()
        updatePath()
    }
    
    private func updatePath() {
        let path = UIBezierPath()
        
        // Start from x = 0 but the mid point of y of the view
        path.move(to: CGPoint(x: 0, y: bounds.midY*2))
        
        // Create the top slanting line
        path.addLine(to: CGPoint(x: bounds.minX, y: bounds.minY))
        
        // Straight line from end of slant to the end of the view
        path.addLine(to: CGPoint(x: bounds.maxX, y: bounds.minY))
        
        // Straight line to come down to the bottom, perpendicular to view
        path.addLine(to: CGPoint(x: bounds.maxX, y: ((bounds.maxY*3)/4) + 20))
        
        // Go back to the slant end position but from the bottom
        path.addLine(to: CGPoint(x: (bounds.maxX*3)/4, y: bounds.maxY))
        
        // Close path back to where you started
        path.close()
        
        shapeLayer.path = path.cgPath
        layer.mask = shapeLayer
    }
}

有没有可能解决这个问题?

1zmg4dgp

1zmg4dgp1#

class PointedView: UIView {

override init(frame: CGRect) {
    super.init(frame: frame)
    setup()
}
required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    setup()
}
func setup() {
    let shapeLayer = CAShapeLayer()
    shapeLayer.path = createBezierPath().cgPath
    shapeLayer.strokeColor = UIColor.blue.cgColor
    shapeLayer.fillColor = UIColor.blue.cgColor
    shapeLayer.lineWidth = 1.0
    shapeLayer.position = CGPoint(x: 10, y: 10)
    self.layer.addSublayer(shapeLayer)
}

func createBezierPath() -> UIBezierPath {
    let path = UIBezierPath()
    
    // Start from x = 0 but the mid point of y of the view
    path.move(to: CGPoint(x: 0, y: bounds.midY*2))
    
    // Create the top slanting line
    path.addLine(to: CGPoint(x: bounds.minX, y: bounds.minY))
    
    // Straight line from end of slant to the end of the view
    path.addLine(to: CGPoint(x: bounds.maxX, y: bounds.minY))
    
    // Straight line to come down to the bottom, perpendicular to view
    path.addLine(to: CGPoint(x: bounds.maxX, y: ((bounds.maxY*3)/4) + 20))
    
    // Go back to the slant end position but from the bottom
    path.addLine(to: CGPoint(x: (bounds.maxX*3)/4, y: bounds.maxY))
    
    // Close path back to where you started
    path.close() // draws the final line to close the path
    return path
}
}
wwwo4jvm

wwwo4jvm2#

通过沿着与原始形状相同的路径绘制另一个CAShapeLayer(),设法解决了这个问题。

let borderLayer = CAShapeLayer()
borderLayer.path = path.cgPath
borderLayer.lineWidth = 2
borderLayer.strokeColor = borderColor.cgColor
borderLayer.fillColor = UIColor.clear.cgColor
borderLayer.frame = bounds
    
layer.addSublayer(borderLayer)

llycmphe

llycmphe3#

如果您使用的是带有@IBInspectable的StoryBoard,则可以尝试如下enter image description here

相关问题