🅿️ViewModifier
╱🚧 under construction
// ViewModifier (protocol)
protocol ViewModifier {
// input type of body()
typealias Content
// output type of body()
associatedtype Body: View
// ⭐️ only requirement
func body(content: Self.Content) -> Self.Body
}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