UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Making an inverted mask?

Forums > SwiftUI

Hi!

I'm trying to make an inverted mask in my application. That is, I want to grey out the area outside the path instead of inside (you'll hopefully see what i mean in the code). I've tried finding this answer on my own (it's how i found the "oeFill" solution below), but so far the solutions I've tried hasn't worked. Does anyone have any suggestions?

Relevant code in my view:

struct MyView: View {
    var body: some View {
        VStack(alignment:.leading,spacing: 0){
            HStack(){
                Image(systemName: "camera.fill").style(.valueLarge)
                    .frame(minWidth: 0, idealWidth: 200, maxWidth: 200, minHeight: 0, idealHeight: 200, maxHeight: .infinity, alignment: .center)
                    .background(Color.gray)
                    .overlay(Rectangle()
                        .fill(ColorManager.black)
                        .opacity(0.5)
                        .frame(width: 200, height: 200)
                        .mask(CameraOverlayMask().fill(style: FillStyle(eoFill: true))))
            }
        }
    }
}

Code in CameraOverlayMask:

struct CameraOverlayMask : Shape {
    func path (in rect: CGRect) -> Path {
        var path = Path()

        path.addArc(center: CGPoint(x:0.8*rect.maxX, y: 0.8*rect.maxY),radius: rect.width/16, startAngle: .degrees(0), endAngle: .degrees(90), clockwise: false)
        path.addArc(center: CGPoint(x:0.2*rect.maxX, y: 0.8*rect.maxY),radius: rect.width/16, startAngle: .degrees(90), endAngle: .degrees(180), clockwise: false)
        path.addArc(center: CGPoint(x:0.2*rect.maxX, y: 0.2*rect.maxY),radius: rect.width/16, startAngle: .degrees(180), endAngle: .degrees(270), clockwise: false)
        path.addArc(center: CGPoint(x:0.8*rect.maxX, y: 0.2*rect.maxY),radius: rect.width/16, startAngle: .degrees(270), endAngle: .degrees(360), clockwise: false)

        return path
    }
}

2      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.