Skip to content

Commit 63de22a

Browse files
authored
doc: Customizing Button with ButtonStyle transforming-views/demo19/README.md (#43)
1 parent 418d0ef commit 63de22a

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

example/transforming-views/demo19/README.md

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,30 @@ struct ContentView: View {
3737
.buttonStyle(BlueButton())
3838
}
3939
}
40-
```
40+
```
41+
我们传递的按钮配置包括按钮当前是否被按下,因此我们可以使用它来调整按钮。
42+
43+
例如,我们可以创建第二种样式,使按钮在按下时变大:
44+
45+
```swift
46+
struct GrowingButton: ButtonStyle {
47+
func makeBody(configuration: Configuration) -> some View {
48+
configuration.label
49+
.padding()
50+
.background(.blue)
51+
.foregroundStyle(.white)
52+
.clipShape(Capsule())
53+
.scaleEffect(configuration.isPressed ? 1.2 : 1)
54+
.animation(.easeOut(duration: 0.2), value: configuration.isPressed)
55+
}
56+
}
57+
58+
struct ContentView: View {
59+
var body: some View {
60+
Button("Press Me") {
61+
print("Button pressed!")
62+
}
63+
.buttonStyle(GrowingButton())
64+
}
65+
}
66+
```

0 commit comments

Comments
 (0)