AAH
AAH

Reputation: 127

Show only a percentage of an image, swiftUI

I'm building an application and I want to be able to display a percentage of an image. For example, in the below image the user needs '7 Earths', so we can show 7 full images, however if they need 7.5 earths I want to be able to show 7.5 images. see screenshot here

Does anyone know how to show a percentage of the image? How would you show only 50% of an image (i.e cut in half vertically)?

At the moment I'm just doing Image("logo") , to show the whole thing.

Thanks

Upvotes: 1

Views: 613

Answers (1)

jnpdx
jnpdx

Reputation: 52625

You can use mask to cut off part of the image:

struct ContentView : View {
    var body: some View {
        HStack {
            Image(systemName: "folder.fill")
                .resizable()
                .frame(width: 150, height: 150)
            Image(systemName: "folder.fill")
                .resizable()
                .frame(width: 150, height: 150)
                .mask(Rectangle().padding(.trailing, 75)) //<-- Here
        }
    }
}

Upvotes: 2

Related Questions