Create a Diagonal Custom UIView in Swift

后端 未结 2 1622
广开言路
广开言路 2020-12-29 17:44

I am working on designing custom UIimageview in swift. I want to create a UIimageview using beizerpath similar to this

The coding should be in swift. Any help is

2条回答
  •  执笔经年
    2020-12-29 17:50

    I'm using something like this and it worked fine, you can add any thing you want to the view

    import UIKit
    
    class CustomView: UIView {
    
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
        // Get Height and Width
        let layerHeight = layer.frame.height
        let layerWidth = layer.frame.width
        // Create Path
        let bezierPath = UIBezierPath()
        //  Points
        let pointA = CGPoint(x: 0, y: 0)
        let pointB = CGPoint(x: layerWidth, y: 0)
        let pointC = CGPoint(x: layerWidth, y: layerHeight)
        let pointD = CGPoint(x: 0, y: layerHeight*2/3)
        // Draw the path
        bezierPath.move(to: pointA)
        bezierPath.addLine(to: pointB)
        bezierPath.addLine(to: pointC)
        bezierPath.addLine(to: pointD)
        bezierPath.close()
        // Mask to Path
        let shapeLayer = CAShapeLayer()
        shapeLayer.path = bezierPath.cgPath
        layer.mask = shapeLayer
    }
    }
    

提交回复
热议问题