Connect Two ScrollView’s Together with different Content Offset Values


Connect Two ScrollView’s Together with different Content Offset Values



To Scroll 2 different scrollView together, Many questions have been already answered regarding using the scrollViewDidScroll Method and passing the content offset of one scrollview to other.



But My Question here is a bit different, Let’s say I have 2 ScrollView A and B both with horizontal scrolling only.



When the view loads ScrollView A has contentOffSet say (x,y) and B scrollview’s content offset : (m,n).



As per my understanding content Offset is the new (x,y) value while scrolling.



Now I can’t pass the content Offset value of A to B here to scroll them together as they loads at different points due to content requirement.I need the exact x points displaced while scrolling in A, then may be pass it to B.



I have also tried getting the velocity from pangesture of A and passing it to B, which doesn’t work smoothly.



How can I achieve a smooth scrolling for both views ?





Just a question why do you need to make use of 2 different scrollViews? get content in one scrollView, During slow scrolling it may lag a little
– iOS Geek
Jul 2 at 4:49





I can't get content in a single scroll view, A is CollectionView and it's scrollview's offset is set to let's say (9000,0) at viewDidLoad. View B is scroll view and with completely different content with offset at (0,0). Now when user scrolls A it goes to (9999,0) so now I can't really pass this offset to B, I need to pass 999 x points to B that ways I can add to it's offset's x value. Basically I am just curious to know if by any way I can get the x points displaced while scrolling like by subtracting previous offset with current one. I am not able to fetch those exact values.
– Tarun Tanwar
Jul 3 at 6:33




1 Answer
1



It's not entirely clear what you're trying to do. But if you want to be updated about when scroll view A changes it's contentOffset you can subclass UIScrollView and pass the data through a delegate or a closure.


contentOffset


UIScrollView


class ScrollView: UIScrollView {
var contentOffsetChanged: ((CGPoint)->())?

override var contentOffset: CGPoint {
didSet {
if let contentOffsetChanged = contentOffsetChanged {
contentOffsetChanged(contentOffset)
}
}
}
}



Update



After reading the comment you left me and the one you wrote to iOS Geek, it seems contradictory. There are a few possibilities for how your math would work out so don't take my exact solution as the answer, but more of the design. I think this is the design you're interested in.


class Controller: UIViewController, UIScrollViewDelegate {
let scrollView = UIScrollView()
var scrollViewB: UIScrollView?
var initialOffset: CGPoint = .zero

override func viewDidLoad() {
super.viewDidLoad()

scrollView.delegate = self
}

func loadScrollViewB() {
initialOffset = scrollView.contentOffset
scrollViewB = UIScrollView()
}

func scrollViewDidScroll(_ scrollView: UIScrollView) {
if scrollView == self.scrollView {
var contentOffset: CGPoint = .zero
contentOffset.x = initialOffset.x - scrollView.contentOffset.x
scrollViewB?.contentOffset = contentOffset
}
}
}





Thanks for your help here, this thing I know how to get updates about change in offset, but let me clear out what I am trying to do. Let's say I have a ScrollView A, and user scrolls/drags it horizontally. Initially its content offset is (0,0) and after scroll it goes on to (60,0). So I want to get x=60 i.e the points displaced on x. Otherwise yes as mentioned by ios Geek in comment above, I can switch to alternate routes for achieving the same.
– Tarun Tanwar
Jul 3 at 6:26






Thanks ,this guides me in right direction.
– Tarun Tanwar
Jul 4 at 7:02






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

api-platform.com Unable to generate an IRI for the item of type

How to set up datasource with Spring for HikariCP?

Display dokan vendor name on Woocommerce single product pages