Home > Net >  Applying direct modification from GeometryEffect without using modifier function in SwiftUI
Applying direct modification from GeometryEffect without using modifier function in SwiftUI

Time:07-01

I am looking to apply direct effect of modification of GeometryEffect without using .modifier, for understanding what i am looking for, look at this below codes, there is a MyViewModifier which is a ViewModifier and I am applying it via .modifier to a Text with approach number 1, and also you can see a function called .myCustomViewModifier() which do the same work without using .modifier in approach number 2.

I am looking the same job for MyGeometryEffect, as you can see I am applying MyGeometryEffect via using .modifier, now i want get same result without using the .modifier, therefore I made MyCustomGeometryEffect and .myCustomGeometryEffect(), but not sure how can make it possible!

I need help to apply GeometryEffect modification without using .modifier.

struct ContentView: View {
    var body: some View {
        ExampleView()
            
    }
}

struct ExampleView: View {
    var body: some View {
        VStack {
            Text("Hello, world!")
                //.modifier(MyViewModifier())  // approach number 1
                .myCustomViewModifier()        // approach number 2
            
            Color.green
                //.modifier(MyGeometryEffect())  // approach number 1
                .myCustomGeometryEffect()        // approach number 2
        }
        .frame(width: 350, height: 100)
        .padding()
    }
}

struct MyViewModifier: ViewModifier {
    func body(content: Content) -> some View {
        content
            .font(Font.body.bold())
            .foregroundColor(.red)
    }
}

struct MyCustomViewModifier<MyContent: View>: View {
    let content: () -> MyContent
    init(content: @escaping () -> MyContent) {
        self.content = content
    }
    var body: some View {
        return content()
            .font(Font.body.bold())
            .foregroundColor(.red)
    }
}

extension View {
    func myCustomViewModifier() -> some View {
        return MyCustomViewModifier(content:  { self } )
    }
}

struct MyGeometryEffect: GeometryEffect {
    func effectValue(size: CGSize) -> ProjectionTransform {
        return ProjectionTransform(CGAffineTransform(translationX: 50, y: 0))
    }
}

struct MyCustomGeometryEffect<MyContent: View>: View {
    let content: () -> MyContent
    init(content: @escaping () -> MyContent) {
        self.content = content
    }
    var body: some View {
        return content()
        // Now how can I apply wished GeometryEffect here without using .modifier ?

    }
}

extension View {
    func myCustomGeometryEffect() -> some View {
        return MyCustomGeometryEffect(content:  { self } )
    }
}

CodePudding user response:

Well, implementation of GeometryEffect is private (thus comparison in question is not accurate), so exactly we rather cannot replicate its behavior.

However for same specific cases something like the following can be used

struct MyCustomGeometryEffect<MyContent: View>: View {
    let content: () -> MyContent
    init(content: @escaping () -> MyContent) {
        self.content = content
    }
    var body: some View {
        content()
          .projectionEffect(.init(CGAffineTransform(translationX: 50, y: 0)))
    }
}
  • Related