說我有這個觀點:
struct CircleView: View {
var body: some View {
Circle()
}
}
在另一種觀點中,我有這樣的事情:
var body: some View {
GeometryReader { geo in
ZStack {
//some other views
if someState == .showCircle {
CircleView()
}
}
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
.edgesIgnoringSafeArea(.all)
}
}
當someState變為 時.showCircle,我想讓 CircleView 從螢屏右側影片到左半球可見的位置,因此它位于螢屏的一半,并使 ZStack 中的其余內容變暗。設定這種影片的最佳方法是什么?
uj5u.com熱心網友回復:
您可以在圓上使用.transition()修飾符,在非圓的.blur()其他視圖上使用修飾符,并結合..animation()
此外,在“非圓形”視圖上,您??需要添加另一個修飾符:.allowsHitTesting(),以避免用戶與模糊的視圖進行互動。
請記住,當您觸發時someState,您必須使用.withAnimation()閉包,否則圓圈不會滑入。
這是代碼的樣子(我添加了一些東西ZStack只是為了提供一個例子)。為了方便起見,我還在someState示例中將變數設為布林值,但在您的情況下,您可以只檢查列舉。
圓形視圖
struct CircleView: View {
@Binding var someState: Bool
var body: some View {
Circle()
.foregroundColor(.red)
.overlay(Text("All is blur"))
.onTapGesture {
someState.toggle()
}
// These modifiers are necessary to animate the circle
.transition(.move(edge: .trailing))
.animation(.easeInOut, value: someState)
}
}
另一種觀點
@State private var someState = false
var body: some View {
GeometryReader { geo in
ZStack {
VStack {
Button {
// Without this closure, the circle does not slide in
withAnimation {
someState.toggle()
}
} label: {
Text("Show circle")
}
.padding()
Spacer()
Text("Bottom")
.padding()
}
// This modifier blurs the view that is not the circle
.blur(radius: someState ? 5 : 0)
// This modifier animates the blur effect
.animation(.easeInOut, value: someState)
// This is necessary to block the view from accepting user taps
// when the circle is showing
.allowsHitTesting(!someState)
if someState {
CircleView(someState: $someState)
// Stop the circle half-way
.offset(x: UIScreen.main.bounds.width / 2, y: 0)
}
}
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
.ignoresSafeArea()
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/452931.html
下一篇:連接組件之間的SwiftUI互動
