Coder Social home page Coder Social logo

jankase / simultaneouslyscrollview Goto Github PK

View Code? Open in Web Editor NEW

This project forked from stonko1994/simultaneouslyscrollview

0.0 0.0 0.0 11 KB

Simultaneously scrolling ScrollViews with SwiftUI support! โ†•๏ธ๐Ÿš€

License: MIT License

Swift 100.00%

simultaneouslyscrollview's Introduction

SimultaneouslyScrollView

Simultaneously scrolling ScrollViews with SwiftUI support


Build SwiftLint

Installation

Swift Package Manager is a tool for managing the distribution of Swift frameworks. It integrates with the Swift build system to automate the process of downloading, compiling, and linking dependencies.

Using Xcode

To integrate using Xcode 13, open your Project file and specify it in Project > Package Dependencies using the following URL:

https://github.com/stonko1994/SimultaneouslyScrollView.git

Usage

import SimultaneouslyScrollView

Synchronize multiple UIScrollViews

  1. Create an SimultaneouslyScrollViewHandler instance by using the factory method and the create function:
    let simultaneouslyScrollViewHandler = SimultaneouslyScrollViewHandlerFactory.create()
  2. Register UIScrollViews that should be synchronized:
    simultaneouslyScrollViewHandler.register(scrollView: scrollView)

SwiftUI support

To enable simultaneously scrolling in SwiftUI we need to utilize another library that allows access to the underlying UIScrollView for a SwiftUI.ScrollView.

SwiftUI-Introspect ๐Ÿš€

Synchronize multiple ScrollViews

  1. Follow the installataion steps from SwiftUI-Introspect

  2. Import Introspect in addition to SimultaneouslyScrollView

    import SimultaneouslyScrollView
    import Introspect
  3. Access the UIScrollView from your ScrollView and register it to the SimultaneouslyScrollViewHandler.

    ScrollView {
        ...
    }
    .introspectScrollView { simultaneouslyScrollViewHandler.register(scrollView: $0) }
  4. That's it ๐Ÿฅณ๐ŸŽ‰

    I recommend storing the simultaneouslyScrollViewHandler inside some view-model. E.g. an @ObservedObject or a @StateObject.

How it works

SwiftUI doesn't provide any API to specify the contentOffset for ScrollViews. Therefore we need to access the underlying UIKit element and set the contentOffset there. This is where SwiftUI-Introspect comes in handy by providing access to the UIKit elements.

As every redraw of the View creates a new ScrollView and a new UIScrollView instance, it is important not to store strong references of the registered UIScrollViews. The SimultaneouslyScrollViewHandler manages this using a custom implementation using the WeakObjectStore.

When a ScrollView is scrolled by the user, the SimultaneouslyScrollViewHandler gets notified about this via the UIScrollViewDelegate. When this happens, the contentOffset of every other registered UIScrollView will be adapted to the new contentOffset of the currently scrolled UIScrollView.

Example

I use this package in one of my own Apps that is currently in the Appstore. So there shouldn't be any issues using this in any production code except the possibility that new SwiftUI versions could break SwiftUI-Introspect.

Please note that this introspection method might break in future SwiftUI releases. Future implementations might not use the same hierarchy, or might not use UIKit elements that are being looked for. Though the library is unlikely to crash, the .introspect() method will not be called in those cases.

Download Scoretastic ๐Ÿฅณ


Buy Me A Coffee

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.