ios – I wish to make scrollView infinite in swiftUI


I’ve arrange scroll view utilizing swiftUI, on first index I’ve present index is 1 and I would like if person swipe down the the scroll view ought to scroll to the final aspect in reverse and when person is on final index and swipe up the scroll view ought to scroll to first index. right here is implementation of scroll view — >>


personal struct OffsetPreferenceKey: PreferenceKey {
    static var defaultValue: CGPoint = .zero
    static func cut back(worth: inout CGPoint, nextValue: () -> CGPoint) {}
}

struct OffsettableScrollView<T: View> : View {
    @State var reader: ScrollViewProxy? = nil
    let axis: Axis.Set
    let showsIndicator: Bool
    let offsetChanged: (CGPoint) -> Void
    let content material: T
    
    init(axis: Axis.Set = .vertical, showsIndicator: Bool = false, offsetChanged: @escaping (CGPoint) -> Void = { _ in }, @ViewBuilder content material: () -> T) {
        self.axis = axis
        self.showsIndicator = showsIndicator
        self.offsetChanged = offsetChanged
        self.content material = content material()
        
    }
    
    var physique: some View {
        ScrollView(axis, showsIndicators: showsIndicator) {
            ScrollViewReader { reader in
                ZStack {
                    GeometryReader { proxy in
                        Colour.clear.desire(key: OffsetPreferenceKey.self,
                                               worth: proxy.body(in: .named("ScrollView")).origin)
                    }
                    .body(width: 0, peak: 0)
                    content material
                        .id("GameCell-top")
                        .tag("GameCell-top")
                        .onAppear {
                            self.reader = reader
                            AppData.shared().scrollViewOffsetClosure = {
                                self.reader?.scrollTo("GameCell-top", anchor: .prime)
                            }
                        }
                }
            }
        
        }
        .coordinateSpace(identify: "ScrollView")
        .onPreferenceChange(OffsetPreferenceKey.self, carry out: offsetChanged)
        .ignoresSafeArea()

    }
}

// and I name the scroll View right here like this - > 


 var textScrollView: some View {
            VStack(alignment: .heart, spacing: 0.0) {
                OffsettableScrollView { level in
                    debugPrint("textData count2", (textData.rely))

                    let negativePointY = -point.y
                    debugPrint("negativePointY",negativePointY)
                    var revisedPointY : CGFloat = 0.0
                    
                         revisedPointY = CGFloat((textData.rely + 1)/2) * UIScreen.screenHeight + negativePointY
                    
                
                    let visibleIndex =  Int(revisedPointY)/Int(UIScreen.screenHeight)
               

                } content material: {
                   
                        

  VStack(spacing: 0) {
            ForEach(0..<self.textData.rely, id: .self) { index in
                let textual content = (AppData.shared().appFont == "ScopeOne-Common") && (textData[index].textual content.hasSuffix("!")) ? "u{2800} (textData[index].textual content)u{2800}" : " (textData[index].textual content) "
                Textual content(textual content)
                    .padding(.trailing, 10)
                    .font(.customized(AppData.shared().appFont, measurement: 38))
                    .foregroundColor(.white)

                    .body(width: UIScreen.screenWidth, peak: UIScreen.screenHeight) // +10 added for scrolling situation in decrease variations then 15...
                    .multilineTextAlignment(.heart)
                    .minimumScaleFactor(0.5)
                    .ignoresSafeArea()

            }
        }

                }
                .onAppear {
                    UIScrollView.look().isPagingEnabled = true
                   
                 }
                .ignoresSafeArea()
            }
          
        }

See also  xctest - ios deep hyperlink testing

Leave a Reply