Home > Back-end >  SwiftUI make image bigger on tap
SwiftUI make image bigger on tap

Time:05-21

I'm trying to make an app and i want to make the image bigger when tapped. And when tapped outside the picture it would go back to normal. How is that possible? Found out there is .onTapGesture() but can't seem to find how to make the image bigger with that.

CodePudding user response:

Simply use .scaleEffect with a condition to scale up and down the view e.g

import SwiftUI

struct ContentView: View {
    @State private var selected: Bool = false

    var body: some View {
        VStack {
            Image(systemName: "camera.fill")
                .resizable()
                .onTapGesture {
                    selected.toggle()
                }
                .scaleEffect(self.selected ? 1.5 : 1)
        }
        .frame(width: 100, height: 100, alignment: .center)
    }
}

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