Created
September 4, 2020 04:44
-
-
Save kazuooooo/a654ec98c55a806eeeaa9a8d9e6307cc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import SwiftUI | |
import PositionScrollView | |
/// Extended ScrollView which can controll position | |
public struct MinimalHorizontalExample: View, PositionScrollViewDelegate { | |
/// Page size of Scroll | |
var pageSize = CGSize(width: 200, height: 300) | |
// Create PositionScrollViewModel | |
// (Need to create in parent view to bind the state between this view and PositionScrollView) | |
@ObservedObject var psViewModel = PositionScrollViewModel( | |
pageSize: CGSize(width: 200, height: 300), | |
horizontalScroll: Scroll( | |
scrollSetting: ScrollSetting(pageCount: 5, afterMoveType: .fitToNearestUnit), | |
pageLength: 200 | |
) | |
) | |
public var body: some View { | |
return VStack { | |
PositionScrollView( | |
viewModel: self.psViewModel, | |
delegate: self | |
) { | |
HStack(spacing: 0) { | |
ForEach(0...4, id: \.self){ i in | |
ZStack { | |
Rectangle() | |
.fill(Color.gray) | |
.border(Color.white) | |
.frame( | |
width: self.pageSize.width, height: self.pageSize.height | |
) | |
Text("Page\(i)") | |
.foregroundColor(Color.white) | |
.font(.system(size: 24, weight: .heavy, design: .default)) | |
} | |
} | |
} | |
} | |
Text("page: \(self.psViewModel.horizontalScroll?.page ?? 0)") | |
Text("position: \(self.psViewModel.horizontalScroll?.position ?? 0)") | |
} | |
} | |
struct SampleView_Previews: PreviewProvider { | |
static var previews: some View { | |
return MinimalHorizontalExample() | |
} | |
} | |
// Delegate methods of PositionScrollView | |
public func onScrollStart() { | |
print("onScrollStart") | |
} | |
public func onChangePage(page: Int) { | |
print("onChangePage to page: \(page)") | |
} | |
public func onChangePosition(position: CGFloat) { | |
print("position: \(position)") | |
} | |
public func onScrollEnd() { | |
print("onScrollEnd") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment