ios 弯曲底杆

bwleehnv  于 2023-02-17  发布在  iOS
关注(0)|答案(1)|浏览(110)

我想使弯曲的底部酒吧这是不是看起来完美的圆由下面的代码

struct customShape: Shape {
    
    var xAxis : CGFloat
    
    var animatableData: CGFloat{
        get { return xAxis}
        set { xAxis = newValue}
    }
    func path(in rect: CGRect) -> Path {
        
        return Path { path in
            
            path.move(to: CGPoint(x: 0, y: 0))
            path.addLine(to: CGPoint(x: rect.width, y: 0))
            path.addLine(to: CGPoint(x: rect.width, y: rect.height))
            path.addLine(to: CGPoint(x: 0, y: rect.height))
            
            let center = xAxis
            
            path.move(to: CGPoint(x: center - 43, y: 0))
            
            let to1 = CGPoint(x: center, y: 50)
            let center1 = CGPoint(x: center - 30, y: 0)
            let center2 = CGPoint(x: center - 46, y: 42)
            
            let to2 = CGPoint(x: center + 52, y: 0)
            let center3 = CGPoint(x: center + 57, y: 47)
            let center4 = CGPoint(x: center + 35, y: 0)
            
            path.addCurve(to: to1, control1: center1, control2: center2)
            path.addCurve(to: to2, control1: center3, control2: center4)
         
        }
        
    }
    
}

kpbwa7wx

kpbwa7wx1#

您正在尝试用贝塞尔曲线手柄构造一个圆。看起来您不是在计算值,而是在猜测和尝试。这将至少非常困难,如果不是不可能解决的话。尝试添加一个弧而不是曲线。
可能的实施:

struct customShape: Shape {
    
    var xAxis : CGFloat
    var radius: CGFloat = 40
    
    var animatableData: CGFloat{
        get { return xAxis}
        set { xAxis = newValue}
    }
    func path(in rect: CGRect) -> Path {
        
        return Path { path in

            path.move(to: CGPoint(x: 0, y: 0))
            // add a line to the starting point of the circle
            path.addLine(to: CGPoint(x: xAxis - radius, y: 0))
            
            // add the arc with the centerpoint of (xAxis,0) and 180°
            path.addArc(center: .init(x: xAxis, y: 0), radius: radius, startAngle: .init(degrees: 180), endAngle: .init(degrees: 0), clockwise: true)

            // complete the rectangle
            path.addLine(to: .init(x: rect.size.width, y: 0))
            path.addLine(to: CGPoint(x: rect.width, y: rect.height))
            path.addLine(to: CGPoint(x: 0, y: rect.height))
            path.closeSubpath()
        }
    }
}

相关问题