【发布时间】:2021-03-16 19:15:10
【问题描述】:
我正在尝试使用水平轮播的图像重新创建展开应用订阅屏幕,这些图像会自动从右向左移动,并启用向右或向左移动的手势,然后动画再次继续。
我找到了一种使用 3 个修饰符为水平 ScrollView 设置动画的方法:
- 偏移
- 动画
- 出现
但是当我拖动轮播时,我找不到将 x 坐标更改为我离开的位置的方法。
如果有人知道如何解决这个问题,那将是救命的!
这是我编写的 SwiftUI 代码:
import SwiftUI
import AVKit
struct ContentView: View {
@State var scrollText = false
var body: some View {
ZStack {
VStack {
Color(#colorLiteral(red: 0, green: 0, blue: 0, alpha: 1))
.ignoresSafeArea(.all)
}
VStack {
VideoPlayer(player: AVPlayer(url: Bundle.main.url(forResource: "wave-1", withExtension: "mp4")!)) {
VStack {
Image("pro-text")
.resizable()
.frame(width: 150, height: .infinity)
.scaledToFit()
}
}
.ignoresSafeArea(.all)
.frame(width: .infinity, height: 300)
ScrollView(.horizontal, showsIndicators: false) {
HStack(spacing: 5) {
Image("benefit-1")
.resizable()
.frame(width: 120, height: 120)
Image("benefit-2")
.resizable()
.frame(width: 120, height: 120)
Image("benefit-3")
.resizable()
.frame(width: 120, height: 120)
Image("benefit-4")
.resizable()
.frame(width: 120, height: 120)
Image("benefit-5")
.resizable()
.frame(width: 120, height: 120)
}
.offset(x: scrollText ? -500 : 20)
.animation(Animation.linear(duration: 30).repeatForever(autoreverses: false))
.onAppear() {
self.scrollText.toggle()
}
}
Spacer()
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
【问题讨论】:
标签: ios swift animation swiftui scrollview