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
599 views
in Technique[技术] by (71.8m points)

swift - How to rescale PKDrawing in SwiftUI on screen rotation?

Im currently making an app usig PencilKit and SwiftUI. If the screen is rotated, the canvas (and the drawing) should be rescaled acordingly. I do this with the Geometry reader and calculating the size.

    NavigationView {
                GeometryReader { g in
                   HStack {
                    CanvasView(canvasView: $canvasView)
                        .frame(width: g.size.width/1.5, height: (g.size.width/1.5)/1.5)
                    placeholder(placeholder: scaleDrawing(canvasHeight: (g.size.width/1.5)/1.5))
                   }
                 }
    }


func scaleDrawing(canvasHeight : CGFloat) ->  Bool {
    
    let factor = canvasHeight / lastCanvasHeight

    let transform = CGAffineTransform(scaleX: factor, y: factor)
    
    canvasView.drawing = canvasView.drawing.transformed(using: transform)
    
    lastCanvasHeight = canvasHeight

    return true
}

The drawing however gets not scaled.
My solution was to create a placeholder view which has a boolean as a parameter. Also a method that scales the drawing with the height of the Canvas as an input.
This works but i think its pretty hacky and not the best solution, but this was the only solution that worked.
Is there a better way to do this? What am i missing?

question from:https://stackoverflow.com/questions/65876506/how-to-rescale-pkdrawing-in-swiftui-on-screen-rotation

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

1 Reply

0 votes
by (71.8m points)
Waitting for answers

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

...