SwiftUI:用于过渡和可动画修改器的不同动画

时间:2020-09-23 11:20:05

标签: animation swiftui

我想在视图的过渡上创建一个动画(在视图层次结构中插入/删除它时),并在某些可动画修改器上创建一个不同动画。

在此示例中,.transition easeInOut 动画,.rotationEffect修饰符的 spring 动画。

预期结果.easeInOut动画适用于过渡,.spring动画适用于旋转

实际结果.spring动画也适用于过渡:-(

struct MyView: View {
    @State var visible = false
    @State var rotated = false

    var body: some View {
        VStack {
            Spacer()

            if self.visible {
                Rectangle()
                    .frame(width: 10, height: 50)
                    .rotationEffect(self.rotated ? .degrees(90) : .zero)
                    .animation(.spring(response: 0.5, dampingFraction: 0.30, blendDuration: 1))
                    .transition(.offset(x: -200, y: 0))
                    .animation(.easeInOut)
            }

            Spacer()

            Button(action: {
                self.visible.toggle()
            }, label: { Text("[Visible]") })

            Button(action: {
                self.rotated.toggle()
            }, label: { Text("[Rotate]") })
        }
        .frame(width: 300, height: 300)
    }
}


PlaygroundPage.current.setLiveView(MyView())

struct Contents_Previews: PreviewProvider {
    static var previews: some View {
        MyView()
    }
}

enter image description here

1 个答案:

答案 0 :(得分:3)

要将version: "3.2" services: aggregator: restart: always build: context: . args: environement: development volumes: - ./:/aggregator depends_on: - mongodb mongodb: image: mongo ports: - 27017:27017 environment: MONGO_INITDB_ROOT_USERNAME: root MONGO_INITDB_ROOT_PASSWORD: root 动画仅应用于旋转,请将.spring添加到value: self.rotated修饰符:

.animation