当程序导航链接插入时,没有任何动画。有没有办法让它像默认样式一样动画?
struct ContentView: View {
@State private var isShowingDetailView = false
var body: some View {
NavigationView {
VStack {
NavigationLink(destination: Text("Second View"), isActive: $isShowingDetailView) { EmptyView() }
Button("Tap to show detail") {
isShowingDetailView = true
}
}
.navigationTitle("Navigation")
}
}
}
谢谢!
解决! !
在我的实际代码中,有一个if-else(参见上面的代码)。因此,NavigationLink的动画不能工作。我把它拆了,它开始工作了。if isShowingDetailView { //remove if else
NavigationLink(destination: Text("Second View"), isActive: $isShowingDetailView) { EmptyView() }
}
在swift UI中添加动画的方法有很多。当使用scaleEffect
点击链接时,我已经制作了一个完全可定制的button style
来动画。您可以使用在单击导航链接时获得一个简单的动画。您可以根据需要修改此代码。
下面是构造Buttonstyle
的代码:
struct AnimatedButton: ButtonStyle {
func makeBody(configuration: Self.Configuration) -> some View {
configuration.label
.font(.title2) //change font style as well as
.foregroundColor(Color.white) // use your color
.frame(height: 48, alignment: .center) // use height and width as you need
.background(configuration.isPressed ? Color.green.opacity(0.5) : Color.green)
.cornerRadius(8)
.shadow(color: Color.gray, radius: 10, x: 0, y: 0) // for adding shadow
.scaleEffect(configuration.isPressed ? 0.9 : 1.0) // here is the scale vale you can modify to change animation scale
}
}
使用在导航Link:
VStack{
NavigationLink(
destination: Text("Details view"),
label: {
Text("Go Details")
.padding()
})
.buttonStyle(AnimatedButton())
}