قالب وردپرس درنا توس
Home / IOS Development / IOS Scrolls a UIScrollView based on the contentOffset of another goes out of sync: iOSProgramming

IOS Scrolls a UIScrollView based on the contentOffset of another goes out of sync: iOSProgramming



I asked this question about stackoverflow with pictures and demo code to explain my problem:

https://stackoverflow.com/questions/53473495/ios-scrolling-one-uiscrollview-based-off-contentoffset-of- another-slowly-goes-ou

EDIT: I have attached a simple demo project dropbox link bottom

I have a user interface with a UICollectionView at the top and a roller sketch at the bottom. I want the scrolling in collection view to scroll in the scroll screen also in sync. I have disabled user interaction in scroll view, so only the collection view can affect the scroll in it.

Each collection item is 150px for this test purpose.

The UIF viewer in the scroll screen is screen width in size. So for each scroll of a collective display object, I have to scroll through the scroll display by screen width. To accomplish this, I calculate the distance that the collective display shift has changed, then divide it with the cell width (150) and multiply it with the scroll screen width.

I'm trying to get the following user interface:

Start:

Enter image description here

Roll collector display to cell 1:

Enter image description here

Roll collector display to cell 2:

Enter image description here

This works fine the first time, but when I roll the collection back and forth a few times for longer distances (let's say cell 10 -> 0 -> 10 -> 0 and so on), the screen goes out of synchronization with "small" distances. To illustrate this, notice how it is the "yellow" color from the other UIView on the right-hand side of the scroll display:

Enter image description here

I can see this issue by NSLogging the distance to the scroll view also how it begins to sync with 0.5 after a few times):

  2018-11-25 19: 24: 28.273278-0500 ScrollViewMatchTest [19412:1203912] Finished: 0
2018-11-25 19: 24: 31.606521-0500 ScrollViewMatchTest [19412:1203912] Finished: 0.5
2018-11-25 19: 24: 55.173709-0500 ScrollViewMatchTest [19412:1203912] Finished: 1.5
2018-11-25 19: 25: 03.007528-0500 ScrollViewMatchTest [19412:1203912] Finished: 1.5
2018-11-25 19: 25: 07.841096-0500 ScrollViewMatchTest [19412:1203912] Finished: 2.5
2018-11-25 19: 26: 57.634429-0500

I'm not quite sure what causes this problem and I've tried many ways to fix it, but in vain. I can sort out a solution (to reset the displacement and bring it back in sync when you scroll), but I'd like to know why this problem is not synced.

I've linked a single demo project to illustrate this issue too (run the app and scroll back and forth aggressively in the top scroll view a few times):

https://www.dropbox.com/s/e2bzgo6abq5wmgw/ScrollViewMatchTest .zip? dl = 0 [19659002] Here is the code:

  #import "ViewController.h"
#define countOfItems 50

@interface ViewController () {
CGFloat previousOffset_Header;
CGFloat previousOffset_Scrollview;
}

@end

@implementation ViewController

- (void) viewDidLoad {
[super viewDidLoad];

[self.myCollectionView registerNib:[UINib nibWithNibName:@"MyCell" bundle:nil] for CellWithReuseIdentifier: @ "cell"];
[self.myCollectionView reloadData];

for (NSInteger i = 0; i <countOfItems; i ++) {
UIView * myView = [[UIView alloc] initWithFrame: CGRectMake (I * self.myScrollView.frame.size.width, 0, self.myScrollView.frame.size.width, self.myScrollView.frame.size.height)];
myView.backgroundColor = i% 2 == 0 [UIColor blueColor]: [UIColor yellowColor];
[self.myScrollView addSubview:myView];

}
self.myScrollView.contentSize = CGSizeMake (countOfItems * self.myScrollView.frame.size.width, self.myScrollView.frame.size.height);


}

- (NSInteger) collectionView: (UICollectionView *) collectionView numberOfItemsInSection: (NSInteger) section {
return countOfItems;
}

- (UICollectionViewCell *) collectionView: (UICollectionView *) collectionView cellForItemAtIndexPath: (NSIndexPath *) indexPath {

MyCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
cell.backgroundColor = indexPath.item% 2 == 0? [UIColor blueColor]: [UIColor yellowColor];
cell.myLabel.text = [NSString stringWithFormat:@"%ld",indexPath.item];

return cell;
}

- (void) scrollViewWillBeginDragging: (UIScrollView *) scrollView {
if (scrollView == self.myCollectionView) {
previousOffset_Header = self.myCollectionView.contentOffset.x;
previousOffset_Scrollview = self.myScrollView.contentOffset.x;
}
}

- (void) scrollViewDidScroll: (UIScrollView *) scrollView {
if (scrollView == self.myCollectionView) {
[NSObject cancelPreviousPerformRequestsWithTarget:self];
[self performSelector:@selector(scrollViewDidEndScrollingAnimation:) withObject:scrollView afterDelay:0.1 inModes:@[NSRunLoopCommonModes]];
CGFloat offsetToMoveBy = (self.myCollectionView.contentOffset.x-previousOffset_Header) * (self.myScrollView.frame.size.width / 150);
previousOffset_Scrollview = previousOffset_Scrollview + offsetToMoveBy;

[self.myScrollView setContentOffset:CGPointMake(previousOffset_Scrollview, self.myScrollView.contentOffset.y) animated:NO];
previousOffset_Header = self.myCollectionView.contentOffset.x;

}
}

- (void) scrollViewDidEndScrollingAnimation: (UIScrollView *) scrollView
{
[NSObject cancelPreviousPerformRequestsWithTarget:self];
NSLog (@ "Done:% g", self.myScrollView.contentOffset.x);
}

@end

Source link