0

I am using the code below and its working fine but i need to make the overlay rectangle with the borders show up as in the image below instead of having the black opacity 0.3. Any help appreciated.

enter image description here

Code found in another post in StackOverflow

    struct CropFrame: Shape {
        let isActive: Bool
        func path(in rect: CGRect) -> Path {
            guard isActive else { return Path(rect) } // full rect for non active
    
            let size = CGSize(width: UIScreen.screenWidth * 0.7, height: UIScreen.screenHeight/5)
            let origin = CGPoint(x: rect.midX - size.width / 2, y: rect.midY - size.height / 2)
            return Path(CGRect(origin: origin, size: size).integral)
        }
    }
    
    struct CropImage: View {
    
        @State private var currentPosition: CGSize = .zero
        @State private var newPosition: CGSize = .zero
        @State private var clipped = false
    
        var body: some View {
            VStack {
                ZStack {
                    Image("test_pic")
                        .resizable()
                        .scaledToFit()
                        .offset(x: self.currentPosition.width, y: self.currentPosition.height)
    
                    Rectangle()
                        .fill(Color.black.opacity(0.3))
                        .frame(width: UIScreen.screenWidth * 0.7 , height: UIScreen.screenHeight/5)
                        .overlay(Rectangle().stroke(Color.white, lineWidth: 3))
                }
                .clipShape(
                    CropFrame(isActive: clipped)
                )
                .gesture(DragGesture()
                    .onChanged { value in
                        self.currentPosition = CGSize(width: value.translation.width + self.newPosition.width, height: value.translation.height + self.newPosition.height)
                }
                .onEnded { value in
                    self.currentPosition = CGSize(width: value.translation.width + self.newPosition.width, height: value.translation.height + self.newPosition.height)
    
                    self.newPosition = self.currentPosition
                })
    
    
                Button (action : { self.clipped.toggle() }) {
                    Text("Crop Image")
                        .padding(.all, 10)
                        .background(Color.blue)
                        .foregroundColor(.white)
                        .shadow(color: .gray, radius: 1)
                        .padding(.top, 50)
                }
            }
        }
    }
Halil Ozel
  • 2,482
  • 3
  • 17
  • 32
stefanosn
  • 3,264
  • 10
  • 53
  • 79
  • 1
    Here's a good starting point: https://stackoverflow.com/questions/59656117/swiftui-add-inverted-mask. Here's a quick sample of how it might work: https://gist.github.com/Baglan/1f8ac704e41fc98d6f891d777b63f566. Let me know if more is needed :) – Baglan Mar 08 '23 at 07:16
  • I don't really understand your question here. If you don't want to use `.fill(Color.black.opacity(0.3))` then use something else like `.fill(Color.clear)` – workingdog support Ukraine Mar 08 '23 at 07:22

0 Answers0