🅿️ViewModifier

╱🚧 under construction

SwiftUIviewsmodifier ⟩ ViewModifier (protocol)

protocol ViewModifier {
    typealias Content            // input type of body()
    associatedtype Body: View    // output type of body()
    // ⭐️ only requirement
   func body(content: Self.Content) -> Self.Body
}

💾 模板

import SwiftUI

// (internal) view modifier
struct MyViewModifier: ViewModifier {
    
    // ⭐️ new states
    @State private var blur = true              
    
    // body
    func body(content: Content) -> some View {
        // ⭐️ handle input `content`
        content
            .blur(radius: blur ? 20 : 0)
            .clipped()
            .onTapGesture { 
                withAnimation {
                    self.blur.toggle()
                }
            }
    }
}

// ⭐️ public helper
extension View {
    public func myModifier() -> some View { 
        modifier(MyViewModifier()) 
    }
}

Last updated

Was this helpful?