Close Menu
  • Home
  • AI
  • Big Data
  • Cloud Computing
  • iOS Development
  • IoT
  • IT/ Cybersecurity
  • Tech
    • Nanotechnology
    • Green Technology
    • Apple
    • Software Development
    • Software Engineering

Subscribe to Updates

Get the latest technology news from Bigteetechhub about IT, Cybersecurity and Big Data.

    What's Hot

    The Download: How AI really works, and phasing out animal testing

    November 17, 2025

    Deep Network Troubleshooting: An Agentic AI Solution

    November 17, 2025

    Today’s NYT Connections: Sports Edition Hints, Answers for Nov. 17 #420

    November 17, 2025
    Facebook X (Twitter) Instagram
    Facebook X (Twitter) Instagram
    Big Tee Tech Hub
    • Home
    • AI
    • Big Data
    • Cloud Computing
    • iOS Development
    • IoT
    • IT/ Cybersecurity
    • Tech
      • Nanotechnology
      • Green Technology
      • Apple
      • Software Development
      • Software Engineering
    Big Tee Tech Hub
    Home»iOS Development»ios – SwiftUI ScrollView scrollPosition not updating on autoscroll
    iOS Development

    ios – SwiftUI ScrollView scrollPosition not updating on autoscroll

    big tee tech hubBy big tee tech hubNovember 3, 2025023 Mins Read
    Share Facebook Twitter Pinterest Copy Link LinkedIn Tumblr Email Telegram WhatsApp
    Follow Us
    Google News Flipboard
    ios – SwiftUI ScrollView scrollPosition not updating on autoscroll
    Share
    Facebook Twitter LinkedIn Pinterest Email Copy Link


    The following code fails to update scrollPosition in SwiftUI ScrollView when the user auto-scrolls, i.e. swipes the finger to scroll and picks up the finger immediately. While the scroll view continues scrolling till it decelerates and stops as the finger has been lifted, the scrollPosition is not seen updated in this time. I have not been able to figure out why it is happening.

    import SwiftUI
    import UIKit
    
    struct ScrubberConfig2 {
        var count:Int
        var majorTickInterval:Int
        var spacing:CGFloat
        let labelFormatter: ((Int) -> String)?
        
        init(count: Int, majorTickInterval: Int, spacing: CGFloat, labelFormatter: ((Int) -> String)? = nil) {
            self.count = count
            self.majorTickInterval = majorTickInterval
            self.spacing = spacing
            self.labelFormatter = labelFormatter
        }
    }
    
    struct VerticalScrubber2: View {
        var config: ScrubberConfig2
        @Binding var value: Int
        
        let tickHeight: CGFloat = 5 // πŸ‘ˆ added
        
        @State private var isUserInteracting: Bool = false // New state to track user interaction
        
        @State private var isScrollPositionSet = false // πŸ‘ˆ added
        @State private var scrollPosition:Int?
        
        var body: some View {
            GeometryReader { geometry in
                let verticalPadding = (geometry.size.height - tickHeight) / 2
                
                ZStack(alignment: .trailing) {
                    ScrollView(.vertical, showsIndicators: false) {
                        VStack(spacing: config.spacing) {
                            ForEach(0...config.count, id: \.self) { index in
                                horizontalTickMark(for: index)
                                    .frame(height: tickHeight)
                                    .id(index)
                            }
                        }
                        .frame(width: 80)
                        .scrollTargetLayout()
                    }
                    .defaultScrollAnchor(.center)
                    .scrollTargetBehavior(.viewAligned)
                    .scrollPosition(id: $scrollPosition, anchor: .center)
                    .contentMargins(.vertical, verticalPadding)
            
                    Capsule()
                        .frame(width: 32, height: 3)
                        .foregroundColor(.accentColor)
                        .shadow(color: .accentColor.opacity(0.3), radius: 3, x: 0, y: 1)
                }
                .frame(width: 100)
                .onAppear { 
                    scrollPosition = value
                }
                .onChange(of: value, { oldValue, newValue in
                    if scrollPosition != newValue, !isUserInteracting {
                        print("πŸ“₯ Value changed: \(oldValue) β†’ \(newValue)")
                        scrollPosition = newValue
                    }
                })
                .onChange(of: scrollPosition, initial: true, { oldValue, newValue in
                    if let newValue = newValue, value != newValue, isUserInteracting {
                        print("πŸ‘† ScrollPosition changed: \(oldValue ?? -1) β†’ \(newValue)")
                        value = newValue
                    }
                })
                .onScrollPhaseChange { oldPhase, newPhase in
                    if newPhase == .interacting {
                        isUserInteracting = true
                    } else {
                        isUserInteracting = false
                    }
                }
            }
        }
        
        private func horizontalTickMark(for index: Int) -> some View {
               let isMajorTick = index % config.majorTickInterval == 0
               
               return HStack(spacing: 8) {
                   Rectangle()
                       .fill(isMajorTick ? Color.accentColor : Color.gray.opacity(0.5))
                       .frame(width: isMajorTick ? 24 : 12, height: isMajorTick ? 2 : 1)
                   
                   if isMajorTick {
                       Text(labelText(for: index))
                           .font(.system(size: 12, weight: .medium))
                           .foregroundColor(.primary)
                           .fixedSize()
                   }
               }
               .frame(maxWidth: .infinity, alignment: .trailing)
               .padding(.trailing, 8)
           }
           
           private func labelText(for index: Int) -> String {
               if let formatter = config.labelFormatter {
                   return formatter(index)
               } else {
                   // Default: show index / steps * 5
                   let tickValue = index
                   return "\(tickValue)"
               }
           }
    }
    
    struct VerticalScrubberPreview2: View {
        @State private var value: Int = 0
        private let config = ScrubberConfig2(count: 100, majorTickInterval: 5, spacing: 5)
        
        var body: some View {
            Text("Vertical Scrubber (0–100 in steps of 5)")
                .font(.title2)
                .padding()
            
            HStack(spacing: 30) {
                VerticalScrubber2(config: config, value: $value)
                    .frame(width:120, height: 300)
                    .background(Color(.systemBackground))
                    .border(Color.gray.opacity(0.3))
    
                VStack {
                    Text("Current Value:")
                        .font(.headline)
                    Text("\(value)")
                        .font(.system(size: 36, weight: .bold))
                        .padding()
                }
                
                Spacer()
            }
            .padding()
        }
    }
    
    #Preview {
        VerticalScrubberPreview2()
    }
    
    



    Source link

    autoscroll iOS ScrollPosition ScrollView SwiftUI Updating
    Follow on Google News Follow on Flipboard
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email Copy Link
    tonirufai
    big tee tech hub
    • Website

    Related Posts

    ios – UserDefaults doesn’t store its value

    November 16, 2025

    ios – How to implement native Tab Bar search (.searchable) for a Dictionary view in SwiftUI?

    November 15, 2025

    react native – How can I use single finger scroll reliably in iOS in ScrollList while also allowing swipe left and swipe right?

    November 14, 2025
    Add A Comment
    Leave A Reply Cancel Reply

    Editors Picks

    The Download: How AI really works, and phasing out animal testing

    November 17, 2025

    Deep Network Troubleshooting: An Agentic AI Solution

    November 17, 2025

    Today’s NYT Connections: Sports Edition Hints, Answers for Nov. 17 #420

    November 17, 2025

    ZnO Nanoparticles with 2 % Silver: A Game-Changer for Sensing

    November 17, 2025
    About Us
    About Us

    Welcome To big tee tech hub. Big tee tech hub is a Professional seo tools Platform. Here we will provide you only interesting content, which you will like very much. We’re dedicated to providing you the best of seo tools, with a focus on dependability and tools. We’re working to turn our passion for seo tools into a booming online website. We hope you enjoy our seo tools as much as we enjoy offering them to you.

    Don't Miss!

    The Download: How AI really works, and phasing out animal testing

    November 17, 2025

    Deep Network Troubleshooting: An Agentic AI Solution

    November 17, 2025

    Subscribe to Updates

    Get the latest technology news from Bigteetechhub about IT, Cybersecurity and Big Data.

      • About Us
      • Contact Us
      • Disclaimer
      • Privacy Policy
      • Terms and Conditions
      © 2025 bigteetechhub.All Right Reserved

      Type above and press Enter to search. Press Esc to cancel.