开发者

In SwiftUI, get warning while using animation() in TavView

I want implement OnBoard Screen in SwiftUI. For that reason, I used TabView. It works perfectly. But I got this warning 'animation' was deprecated in iOS 15.0. How I implement withAnimation or animation(_:value:) to fulfil my goal.

          VStack(spacing: 34) {
                TabView(selection: $currentIndex) {
                    ForEach(viewModel.getOnBoards(), id: \.id) { onboard in
                        OnBoardItemView(onBoard: onboard)
                    }
                }
                .tabViewStyle(PageTabViewStyle(indexDisplayMode: .never))
                .animation(.easeInOut(duration: 1.0))
                .transition(.slide)
                
                Spacer()
                
                ItemIndicatorView(
                    totalNumberOfItem: 3,
                    current: $currentIndex
开发者_StackOverflow中文版                )
                
                ControllerView(
                    currentIndex: $currentIndex,
                    totalNumberOfItem: 3,
                    onboard: viewModel.getOnBoardItemAt(position: currentIndex-1)
                )

            }


Starting from iOS 15, animation(_:) is deprecated but you can use animation(_:value:) by passing currentIndex into value parameter instead if you prefer the implicit animation approach. The code snippet will be as follows.

TabView(selection: $currentIndex) {
    ForEach(viewModel.getOnBoards(), id: \.id) { onboard in
        OnBoardItemView(onBoard: onboard)
    }
}
.tabViewStyle(PageTabViewStyle(indexDisplayMode: .never))
.animation(.easeInOut(duration: 1.0), value: currentIndex)
.transition(.slide)

But you can also move to explicit animation using withAnimation to animate slide transition. For this approach, you need to wrap the line of code to switch the value of currentIndex inside withAnimation block. The code snippet will be as follows.

withAnimation(.easeInOut(duration: 1.0)) {
    currentIndex = newValue
}

I hope this will help you to understand how to animate explicitly or implicitly.


You need tell Xcode what exactly should be animated! With given a variable that conform to Equatable protocol. That could be State or Binding or any other wrapper that allow you to update the value of it.

VStack(spacing: 34) {
            TabView(selection: $currentIndex) {
                ForEach(viewModel.getOnBoards(), id: \.id) { onboard in
                    OnBoardItemView(onBoard: onboard)
                }
            }
            .tabViewStyle(PageTabViewStyle(indexDisplayMode: .never))
            .animation(Animation.easeInOut(duration: 1.0))
            .transition(.slide)
            
            Spacer()
            
            ItemIndicatorView(
                totalNumberOfItem: 3,
                current: $currentIndex
            )
            
            ControllerView(
                currentIndex: $currentIndex,
                totalNumberOfItem: 3,
                onboard: viewModel.getOnBoardItemAt(position: currentIndex-1)
            )

        }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜