'Draggable Frame/Slider

This is probably a rookie mistake, but I'm trying to create an "expandable" frame in SwiftUI, i.e. a frame that could be extended via a draggable "slider". I'm thinking that the easiest way could be specifying a frame, and a rectangle as "slider" to drag.

Here's an MRE:

import SwiftUI

fileprivate enum Constants {
    static let radius: CGFloat = 16
    static let indicatorHeight: CGFloat = 6
    static let indicatorWidth: CGFloat = 60
    
}

struct ContentView: View {
    
    @GestureState private var dragAmount = CGSize.zero
    private var frameheight : CGFloat = 200
    
    var body: some View {
        VStack{
        Text("Hello, world!")
                .frame(height: frameheight + dragAmount.height)
        Divider()
        RoundedRectangle(cornerRadius: Constants.radius)
            .fill(Color.secondary)
            .frame(
                width: Constants.indicatorWidth,
                height: Constants.indicatorHeight
            )
            .gesture(
                DragGesture().updating($dragAmount) { value, state, transaction in
                    state = value.translation
                    print("Height: \(state), drag Amount: \(dragAmount)")
                }
                )
            Divider()
            Text("Hello, world!")
            Text("Hello, world!")
            Spacer()
            
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

Looks ok... but it doesn't work. When I move the mouse up and down, my "print" statement indicates that dragAmount changes, but my frame size doesn't. Interestingly, changing "+ dragAmount.height" to " + dragAmount.width" does change the height, but only temporarily... and only if you drag the rectangle left/right.

Any insights why? And, of course, suggestions how to fix are welcome! :)

Thanks, Philipp



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source