简体   繁体   English

SwiftUI 嵌入时忽略堆叠属性动画

[英]SwiftUI stacked property animations ignored when embedded

Going by examples we can see that it is possible to animate different properties with different animations.通过示例,我们可以看到可以使用不同的动画为不同的属性设置动画。 For example:例如:

Button("Tap me") {self.isShowingRed.toggle()}
    .frame(width: 200, height: 200)
    .background(isShowingRed ? Color.red : Color.blue)
    .animation(.easeIn(duration: 2.5))
    .clipShape(RoundedRectangle(cornerRadius: isShowingRed ? 50 : 0))
    .animation(Animation.easeInOut(duration: 0.1).repeatCount(5))

This code will animate the button background from red to blue in 2.5 seconds, while animating the corner radius from 0 to 50 with 5 repetitions.此代码将在 2.5 秒内将按钮背景从红色变为蓝色,同时将圆角半径从 0 变为 50,重复 5 次。

The problem appears as soon as the view is embedded:一旦嵌入视图,问题就会出现:

VStack {
    Button("Tap me") {self.isShowingRed.toggle()}
        .frame(width: 200, height: 200)
        .background(isShowingRed ? Color.red : Color.blue)
        .animation(.easeIn(duration: 2.5))
        .clipShape(RoundedRectangle(cornerRadius: isShowingRed ? 50 : 0))
        .animation(Animation.easeInOut(duration: 0.1).repeatCount(5))
    } 
}

When the button is embedded only the first animation is used, in this case both the color and the radius will be animated in 2.5 seconds with no repetitions.嵌入按钮时,仅使用第一个 animation,在这种情况下,颜色和半径都将在 2.5 秒内进行动画处理,没有重复。

Even if I make the button a separate component, the same problem persists.即使我将按钮设为单独的组件,同样的问题仍然存在。

Am I doing something wrong or is this a SwiftUI bug?我做错了什么还是这是 SwiftUI 错误?

Edit: I'm using Xcode 11.1 and testing on the simulator.编辑:我正在使用 Xcode 11.1 并在模拟器上进行测试。

As I observed when something unexpected happens when there is.background, then issue is in it... In your use-case the animation must be applied to background content and this solves the problem.正如我所观察到的,当存在.background 时发生意外情况时,问题就出在其中......在您的用例中,animation 必须应用于背景内容,这可以解决问题。

Here is example I used and it animates as you wanted with and w/o container.这是我使用的示例,它可以根据您的需要进行动画处理,并且没有容器。

import SwiftUI

struct TestButtonAnimation: View {
    @State private var isShowingRed = false
    var body: some View {
        VStack {
            Button("Tap me") {self.isShowingRed.toggle()}
                .frame(width: 200, height: 200)
                .background(
                    Group {isShowingRed ? Color.red : Color.blue}
                    .animation(.easeIn(duration: 2.5))
                )
                .clipShape(RoundedRectangle(cornerRadius: isShowingRed ? 50 : 0))
                .animation(Animation.easeInOut(duration: 0.1).repeatCount(5))
        }
    }
}

struct TestButtonAnimation_Previews: PreviewProvider {
    static var previews: some View {
        TestButtonAnimation()
    }
}

Tested with: Xcode 11.1经测试:Xcode 11.1

You may try this way .animation(.default) in the container您可以在容器中尝试这种方式.animation(.default)

  var body: some View {
  VStack{
  Button("Tap me") {self.isShowingRed.toggle()}
 .frame(width: 200, height: 200)
 .background(isShowingRed ? Color.red : Color.blue)
 .animation(.easeIn(duration: 2.5))
.clipShape(RoundedRectangle(cornerRadius: isShowingRed ? 50 : 0))
.animation(Animation.easeInOut(duration: 0.1).repeatCount(5))
}.animation(.default)
}

After updating to Xcode 11.2.1 this issue was resolved.更新到 Xcode 11.2.1 后,此问题已解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM