简体   繁体   English

SwiftUI 的新 animation 方法只能与 onTapGesture 结合使用

[英]SwiftUI's new animation method only works combined with onTapGesture

I am trying to animate an arrow to make it flip 180 degrees when a button is tapped.我正在尝试为箭头设置动画,使其在点击按钮时翻转 180 度。 But it only works when I use the onTapGesture method with nothing else inside.但它仅在我使用 onTapGesture 方法且内部没有其他内容时才有效。 It also does not work if I change the rotation angle value somewhere else in the code.如果我在代码中的其他地方更改旋转角度值,它也不起作用。 For instance:例如:

This piece of code is properly animated这段代码是正确的动画

Image(systemName: "arrow.up.circle")
                                .resizable()
                                .foregroundColor(.white)
                                
                                .frame(width: 50, height: 50)

                                .rotationEffect(Angle.degrees(self.rotationAngle))
                                .animation(.easeIn, value: self.rotationAngle)
                                .onTapGesture {
                                    self.rotationAngle += 180

                                }

This one is not这个不是

  Image(systemName: "arrow.up.circle")
                                .resizable()
                                .foregroundColor(.white)
                                
                                .frame(width: 50, height: 50)

                                .rotationEffect(Angle.degrees(self.rotationAngle))
                                .animation(.easeIn, value: self.rotationAngle)
                                .onTapGesture {
                                    self.currentArrow.toggle()
                                    self.rotationAngle += 180
                                }

Does anyone know why?有谁知道为什么?

The .animation(.easeIn, value: self.rotationAngle) modifier explicitly tells the view to animate when self.rotationAngle changes. .animation(.easeIn, value: self.rotationAngle)修饰符明确地告诉视图在self.rotationAngle改变时进行动画处理。

However, there's no such instruction for the currentArrow property changes, so the view doesn't animate when it changes.但是,对于currentArrow属性更改没有这样的指令,因此视图在更改时不会动画。

To fix it, you can do either of two things:要修复它,您可以执行以下两项操作之一:

A. Add a similar modifier for the currentArrow property: A. 为currentArrow属性添加一个类似的修饰符:

.animation(.easeIn, value: currentArrow)

B. Toggle the value within a withAnimation block: B. 在withAnimation块中切换值:

withAnimation {
    self.currentArrow.toggle()
}

Answer: As @vadim-belyaev mentioned, using withAnimation will fix the animation but it only works if nothing else is inside the block.答:正如@vadim-belyaev 所提到的,使用withAnimation将修复 animation 但它仅在块内没有其他内容时才有效。 This will happen similarly with onTapGesture My solution is as follows:这与onTapGesture类似,我的解决方案如下:

Image(systemName: "arrow.up.circle")
                                .resizable()
                                .foregroundColor(.white)
                                
                                .frame(width: 50, height: 50)

                                .rotationEffect(Angle.degrees(self.rotationAngle))
                                .animation(.easeIn, value: self.rotationAngle)
                                .onTapGesture {
                                    withAnimation {
                                        self.rotationAngle += 180

                                    }
                                }
                        

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

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