Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.7k views
in Technique[技术] by (71.8m points)

swiftui - How to update points on path after the view has been modified

struct road: View {
    
    @State private var percentage: CGFloat = .zero
    @State private var ypoint: Double = 200
    @State private var xpoint: Double = .zero
    @State private var newRadius: Double = 0.0
    @State private var newStartAng: Double = 0
    @State private var newEndAng: Double = 0
    @State private var isClock: Bool = true
    
    func newMove() {
        let rds = newRadius
        let angle = Double(abs((newEndAng / 4) - 90))
        
        if isClock {
            newStartAng = newStartAng - 180
            if newStartAng < 0 {
                newStartAng += 360
            }
        } else {
            newStartAng = newStartAng + 180
            if newStartAng > 360 {
                newStartAng -= 360
            }
        }
        
        if ypoint > 350 {
            isClock = true
        } else if ypoint < 35 {
            isClock = false
        } else {
            let rand = Int.random(in: 0...10)
            if rand < 6 {
                isClock = false
            } else {
                isClock = true
            }
        }
        
        if isClock {
            newEndAng = Double.random(in: 0...0)
        } else {
            newEndAng = Double.random(in: newStartAng...360)
        }
        
        newRadius = Double.random(in: (rds - 15)...(rds + 15))
        if newRadius < 0 {
            newRadius = Double.random(in: (rds - 5)...(rds + 15))
        }
        if newRadius < 0 {
            newRadius = Double.random(in: 2...(rds + 15))
        }
        
        xpoint = xpoint + (rds * cosDegreesToRad(degree: angle) + (newRadius * cosDegreesToRad(degree: angle)))
        ypoint = ypoint - (rds * sinDegreesToRad(degree: angle) + (newRadius * sinDegreesToRad(degree: angle)))
    }
    func theRoad() -> Path {
        var path = Path()
        path.move(to: CGPoint(x: 0, y: 200))
        
        newMove()
        
        path.addArc(center: CGPoint(x: xpoint, y: ypoint), radius: CGFloat(newRadius), startAngle: .degrees(newStartAng), endAngle: .degrees(newEndAng), clockwise: isClock)
        return path
    }
    
    var body: some View {
        theRoad()
        .trim(from: 0, to: percentage)
        .stroke(style: StrokeStyle(lineWidth: 3.0, lineCap: .round))
        .fill(Color.black)
        .animation(.linear(duration: 50.0))
        .onAppear {
            self.percentage = 1.0
        }
        
    }
}

The error I get is modifying state during view update, this will cause unexpected behavior.

The plan is to draw a line across the screen randomly, I am not sure how to wait until the view is updated to update these points. I do not want to use a button or another form of toggle, any help is greatly appreciated.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...