How to rescale PKDrawing in SwiftUI on screen rotation?
Asked Answered
T

0

6

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?

Tereasaterebene answered 24/1, 2021 at 22:8 Comment(3)
I've been struggling with a similar issue to this for days now and your solution worked excellently for me!Paillette
Did you find any good solution?Accuse
No unfortunately not...Tereasaterebene

© 2022 - 2024 — McMap. All rights reserved.