【发布时间】:2020-03-20 02:14:22
【问题描述】:
我有一个水平的ScrollView,其中包含一些Rectangles。我想要实现的是淡出每个Rectangle,然后隐藏(删除)ScrollView。
我已经部分实现了:
我说的部分原因是,如您所见,Rectangles 已删除,但 ScrollView 仍然存在(请参阅属于 ScrollView 的蓝色边框框,我添加了一些填充以使其更明显)。
我在想我可能需要嵌套转换(每个Rectangle 都有一个.move(edge:) 转换,ScrollView 应该有另一个“外部”转换-但这也可能是错误的方法。
任何想法如何实现此动画并在动画结束时转换(删除)ScrollView?我尝试了不同的选择,但我失败了。注意:黑色的Rectangle是固定的。
示例代码如下:
struct TestAnimation: View {
@State var show : Bool = true
var colors : [Color] = [.red, .orange, .yellow, .green, .blue]
var body: some View {
VStack(alignment: .leading) {
Spacer()
Color.purple
.frame(height: 100)
.overlay(
Text("Tap Me!").foregroundColor(.white))
.onTapGesture {
self.show.toggle()
}
HStack(alignment: .bottom) {
Rectangle()
.fill(Color.black)
.frame(width: 70, height: 100)
.overlay(Text("A").foregroundColor(.white).font(.largeTitle))
.padding(8)
ScrollView(.horizontal, showsIndicators: false) {
HStack(alignment: .bottom) {
if show {
self.cellForItemAt(idx: 2)
self.cellForItemAt(idx: 3)
self.cellForItemAt(idx: 4)
self.cellForItemAt(idx: 5)
}
}
.frame(height: 100)
.padding(4)
.border(Color.red)//HStack
}//Scrollview
.padding(4)
.border(Color.blue)
}
.padding(4)
.border(Color.gray)//Hstack
}
}
func cellForItemAt(idx: Int) -> some View {
return Rectangle()
.fill(colors[idx-1])
.frame(width: 70, height: 100)
.transition(AnyTransition.move(edge: .bottom).combined(with: .opacity).animation(self.ripple(idx: idx)))
.animation(ripple(idx: idx))
}
func ripple(idx: Int) -> Animation {
Animation
.easeInOut(duration: 0.8)
.delay(0.20 * Double(colors.count - idx) : Double(idx))
}
}
【问题讨论】:
-
如果你将
scrollview包裹在if show {}中,你可以给它一个延迟动画0.20 * 颜色计数。 -
@MuhandJumah,这行不通。我已经试过了。或者你是如何实现的?
-
我已经发布了我的答案,希望它对你有用。祝你好运!
标签: swiftui transition swiftui-animation