先看下要实现的效果
- Scrollview高级用法,实现分页滚动
实现方案:
一、定义页面struct
struct PageContent : Identifiable,Hashable,Equatable{static func == (lhs: PageContent, rhs: PageContent) -> Bool {return lhs.id == rhs.id}var hashValue: Int { return id.hashValue }func hash(into hasher: inout Hasher){ }var id : UUID = UUID()var title : Stringvar isSelect : Bool = falsevar view : AnyViewmutating func updateSelect(_ select : Bool) {self.isSelect = select}
}
二、定义ViewModel的Class
class HDHomeBottomViewModel : ObservableObject {@Published var pages = [PageContent]()
}
三、制作分布社图Struct
struct PagerView : View {@State private var index: Int = 0@State private var offset: CGFloat = 0@State private var tabOffset : CGFloat = 0@StateObject var vm = HDHomeBottomViewModel()private var pages : [PageContent]init(pages : [PageContent],selectIndex : Int) {self.pages = pages