不同形状的UIView

问题描述 投票:0回答:0

我们如何创建这个形状的视图

尝试使用 CAShapeLayer 创建这些视图并旋转角度,但结果看起来像

非常感谢任何帮助。这是我使用的代码:

    extension UIView {
    func drawCustomShape(type: Int) {
        let shapeLayer = CAShapeLayer()
        shapeLayer.frame = self.bounds
        shapeLayer.fillColor = UIColor.white.cgColor
        self.layer.addSublayer(shapeLayer)
        if type == 1 {
            self.layer.transform = CATransform3DMakeRotation(CGFloat(-30 * Double.pi/180), 0, 0, 1)
        } else if type == 2 {
            self.layer.transform = CATransform3DMakeRotation(CGFloat(1 * Double.pi/180), 0, 0, 1)
        } else if type == 3 {
            self.layer.transform = CATransform3DMakeRotation(CGFloat(30 * Double.pi/180), 0, 0, 1)
        }
        
        //path
        let path = UIBezierPath()
        
        //topLeft
        path.move(to: CGPoint(x: 0, y: 50))
        
        //topRight
        path.addLine(to: CGPoint(x: self.frame.size.width, y: 0))
       
        //bottomRight
        path.addLine(to: CGPoint(x: self.frame.size.width, y: self.frame.size.height))
       
        //bottomLeft
        path.addLine(to: CGPoint(x:  0, y:  self.frame.size.height-50))
        
        path.close()
        shapeLayer.path = path.cgPath
    }
}
swift core-graphics
© www.soinside.com 2019 - 2024. All rights reserved.