【问题标题】:Add views to parent swiftui view and then remove them programatically将视图添加到父 swiftui 视图,然后以编程方式删除它们
【发布时间】:2022-01-24 00:50:08
【问题描述】:

现在我有一个用 SwiftUI 编写的带有一些按钮的父视图。我想要做的是在按下按钮时以编程方式在整个父视图之上创建和覆盖一些新的子视图,然后让它们淡出,将它们从视图中删除。由于 SwiftUI 是基于状态的,我不知道该怎么做。我是否必须跟踪使用@State 变量创建的每个视图?我的代码现在如下所示:


struct ContentView: View {
    
    var key: String
    
    @State private var touches: [Touch] = []
    
    struct Touch: Identifiable {
        let id = UUID()
        let location: CGPoint
    }
    
    var body: some View {
        ZStack {
            Color.blue
            ripplesLayer
        }
        .gesture(
            DragGesture(minimumDistance: 0)
                .onEnded { value in
                    touches.append(
                        Touch(location: value.location)
                    )
                }
        )
        .edgesIgnoringSafeArea(.all)
    }
    
    var ripplesLayer: some View {
        ForEach(touches.suffix(15)) { touch in
            RippleView(key: key)
                .position(
                    x: touch.location.x,
                    y: touch.location.y
                )
        }
    }
    
    struct RippleView: View {
        
        var key: String
        
        @State private var isHidden = false
        @State private var size: CGFloat = 50
        
        var body: some View {
            Circle()
                .fill(Color.white.opacity(isHidden ? 0 : 0.5))
                .frame(width: size, height: size)
                .transition(.opacity)
                .animation(.easeOut(duration: 0.5))
                .onAppear {
                    Sound.play(file: key, fileExtension: ".wav")
                    withAnimation {
                        isHidden = true
                        size = 200
                    }
                }
        }
    }
}

我有这个结构,它在 this answer 中引用 foreach 循环中的波纹子视图,但不知道如何跟踪多个消失的视图,甚至不知道如何首先实现它。多点触控已启用并且。我希望能够动态创建很多子视图。

struct FreePiano: View {
    
    @State var numberOfPianos = 0
    
    var whiteKeys = ["c1", "d1", "e1", "f1", "g1", "a1", "b1", "c2", "d2", "e2", "f2", "g2", "a2", "b2", "c3"]
    var blackKeys = ["c#1", "d#1", "f#1", "g#1", "a#1", "c#2", "d#2", "f#2", "g#2", "a#2"]
    
    var body: some View {
        
        ZStack {
        
        HStack {
            VStack {
                ForEach(whiteKeys, id: \.self) { whiteKey in
                    FreePianoKey(numberOfImages: $numberOfPianos, key: whiteKey)
                }
            }
            VStack {
                ForEach(blackKeys, id: \.self) { blackKey in
                    FreePianoKey(numberOfImages: $numberOfPianos, key: blackKey)
                }
            }
        }
         
            ForEach(0 ... numberOfPianos, id, \.self) { _ in
                
                ContentView()
                    .allowsHitTesting(false)
                    .frame(width: 300, height: 300)
                    .position(x: 100, y:100)
                
            }
            

            
        }
    }
}

像 SpriteKit 这样的东西对我想做的事情会更容易吗?

【问题讨论】:

    标签: swift swiftui


    【解决方案1】:

    我在我的应用程序中做同样的事情。在父视图中,我保留了一个状态变量来跟踪子视图。如果您的应用程序一次只允许一个视图,您可以使用一个简单的枚举状态变量(我称之为 AppState)。然后,在启动子视图时,您将更改 AppState。

    或者,状态变量可以是更复杂的东西,比如堆栈。或者,在一次允许多个子视图的情况下,它可以是所有子窗口及其状态的元组数组(例如,0 = 关闭,1 = 打开)。

    【讨论】:

      猜你喜欢
      • 2011-10-07
      • 1970-01-01
      • 2011-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多