0

I would like to make toolbar with my custom shape. I have been trying all day to fill my shape with UIBlurEffect.

My shape code:

struct CRectangle: Shape {
func path(in rect: CGRect) -> Path {
    var path = Path()
    path.addArc(center: CGPoint(x: rect.midX, y: rect.minY), radius: 20, startAngle: .zero, endAngle: .init(degrees: 180), clockwise: true)
    path.move(to: CGPoint(x: rect.midX+20, y: rect.minY))
    path.addLine(to: CGPoint(x: rect.maxX, y: rect.minY))
    path.addLine(to: CGPoint(x: rect.maxX, y: rect.maxY))
    path.addLine(to: CGPoint(x: rect.minX, y: rect.maxY))
    path.addLine(to: CGPoint(x: rect.minX, y: rect.minY))
    path.addLine(to: CGPoint(x: rect.midX-20, y: rect.minY))
    return path
}

It works pretty well, but it is without UIBlurEffect:

struct ContentView: View {
    var body: some View {
        ZStack{
            CRectangle()
                .fill(Color.black)
                .frame(width: 300, height: 100)
        }
        
    }
}

On the forum I found the code for UIBlurEffect and i added to my program, but it works not correctly, because the upper part remains empty.

struct ContentView: View {
    var body: some View {
        ZStack{
            CRectangle()
                .stroke()
                .frame(width: 300, height: 100)
                .background(VisualEffectView(effect: UIBlurEffect(style: .light)))
        }
        
    }
}
struct VisualEffectView: UIViewRepresentable {
    var effect: UIVisualEffect?
    func makeUIView(context: UIViewRepresentableContext<Self>) -> UIVisualEffectView { UIVisualEffectView() }
    func updateUIView(_ uiView: UIVisualEffectView, context: UIViewRepresentableContext<Self>) { uiView.effect = effect }
}
  • you are facing with 2 issue, which every one is big one and need deferent topic! first your path has issue, second your VisualEffectView has issue, 2 big problem together – ios coder Feb 10 '21 at 01:47
  • This is my first post. I am sill learning that’s why I make mistakes. Could you at least guide me to the solution? Maybe there is another way to solution this “two big problem”? – Robert Adamczyk Feb 10 '21 at 05:59
  • no problem, i just wanted tell my idea to get closer to answer, as I said I think you should chop our issue to small parts that can be answered, in the way you asked, multi issue effecting together and it makes harder to get an answer – ios coder Feb 10 '21 at 06:04
  • I understand. I have to correct first my path. I thought it is properly done. I will do a new topic with my path as you suggested. Will this correct? – Robert Adamczyk Feb 10 '21 at 08:39
  • there are so many good persons here to help, but you and I should not make the issue so complicated to be solved, as I wanted help, noticed your path has issue at fist place, but no big deal make new question – ios coder Feb 10 '21 at 08:43
  • After work, I decided to try my hand. I was writing and writing code watching the changes and finally got it. Path.addArc was out of the frame. It is so obvious now. Now .clipShape(CRectangle()) is working – Robert Adamczyk Feb 10 '21 at 19:59
  • @RobertAdamczyk Glad you worked it out. It would be great if you could post your solution as an answer and accept the same, this way other users would benefit from your learnings. Happy Coding. – atulkhatri Jul 12 '23 at 18:58

0 Answers0