首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何插入具有交错缩放动画的一行按钮?

如何插入具有交错缩放动画的一行按钮?
EN

Stack Overflow用户
提问于 2020-07-30 04:42:09
回答 1查看 61关注 0票数 0

我正在尝试从Castro应用程序中重新创建以下动画...

( GIF会放慢速度,以便您可以看到效果)

正如你在上面的GIF中看到的,当点击单元格时,你有一排按钮出现。每个按钮都有放大和缩小效果。动画是交错的,这样第一个按钮首先完成,最后一个按钮最后完成。

我试过的..。

代码语言:javascript
复制
struct SwiftUIView: View {
    @State var show: Bool = false
    var body: some View {
        VStack {
            Button(action: {
                withAnimation {
                    show.toggle()
                }
            }, label: {
                Text("Button")
            })
            HStack {
                if show {
                    Button(action: {}, label: { Image(systemName: "circle") })
                        .transition(.scale)
                    Button(action: {}, label: { Image(systemName: "circle") })
                        .transition(.scale)
                    Button(action: {}, label: { Image(systemName: "circle") })
                        .transition(.scale)
                }
            }

        }
    }
}

正如你在上图中看到的.每个按钮都会放大,但仅当视图被移除时才会放大。另外,我不知道如何错开动画。

EN

回答 1

Stack Overflow用户

发布于 2020-07-30 05:52:06

尝试以下操作:

代码语言:javascript
复制
struct ContentView: View {
    @State var show = false
    @State var showArray = Array(repeating: false, count: 3)

    var body: some View {
        VStack {
            Button(action: toggleButtons) {
                Text("Button")
            }
            HStack {
                ForEach(showArray.indices, id: \.self) { index in
                    self.circleView(for: index)
                }
            }
        }
    }

    @ViewBuilder
    func circleView(for index: Int) -> some View {
        if show {
            ZStack {
                Image(systemName: "circle")
                    .opacity(.leastNonzeroMagnitude)
                    .animation(nil)
                if showArray[index] {
                    Image(systemName: "circle")
                        .transition(.scale)
                }
            }
        }
    }

    func toggleButtons() {
        showArray.indices.forEach { index in
            withAnimation {
                self.show = true
            }
            DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(index)) {
                withAnimation {
                    self.showArray[index].toggle()
                    if index == self.showArray.count - 1, self.showArray[index] == false {
                        self.show = false
                    }
                }
            }
        }
    }
}

它使用了一个小技巧来正确地对齐视图-在ZStack中有一个几乎没有不透明度的假Image

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63162103

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档