structContentView:View{
var body: some View{Text("Hello World!").visualEffect { initial, geometry in
initial
.blur(radius:8).opacity(0.9).scaleEffect(.init(width:2, height:2))}}}
structContentView:View{@State private var isScaled =false
var body: some View{VStack{Button("Scale"){
isScaled.toggle()}Text("Hello World!").visualEffect { initial, geometry in
initial.scaleEffect(CGSize(
width: isScaled ?2:1,
height: isScaled ?2:1))}.animation(.smooth, value: isScaled)}}}
四、完整示例
将如下的代码放入 Swift 文件中,然后在 Xcode 中打开并运行,选择合适的模拟器。请注意,由于视觉效果和动画效果,最好在模拟器上查看效果。
import SwiftUIstructContentView:View{
var body: some View{Text("Hello World!").visualEffect { initial, geometry in
initial.offset(geometry.size)}}}structContentViewWithEffects:View{
var body: some View{Text("Hello World!").visualEffect { initial, geometry in
initial
.blur(radius:8).opacity(0.9).scaleEffect(.init(width:2, height:2))}}}structContentViewWithAnimation:View{@State private var isScaled =false
var body: some View{VStack{Button("Scale"){
isScaled.toggle()}Text("Hello World!").visualEffect { initial, geometry in
initial.scaleEffect(CGSize(
width: isScaled ?2:1,
height: isScaled ?2:1))}.animation(.smooth, value: isScaled)}}}structContentView_Previews:PreviewProvider{static var previews: some View{ContentView()ContentViewWithEffects()ContentViewWithAnimation()}}