UIScrollView programmatically sets contentOffset problem

I am creating an ImageViewer similar to the Photos application. I have a PagingScrollView, which is a view controller view (created in loadView). Pages are UIScrollViews with subsets of UIImageView. Everything works hunky dory until I try to "skip" (set the content offset) to the index, except the first one. If I download, the image is "i", the image will be displayed correctly, but if the user performs a single click gesture, the contentOffset is reset back to display the image "0". The error does not occur with other gestures, such as drag and drop, and this does not occur when you touch, if you perform another gesture, for example, drag.

This is my loadView for ImageViewerController

- (void)loadView { //Make the outer spaging scroll view CGRect pagingScrollViewFrame = [self frameForPagingScrollView]; UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:pagingScrollViewFrame]; scrollView.pagingEnabled = YES; scrollView.backgroundColor = [UIColor blackColor]; scrollView.showsVerticalScrollIndicator = NO; scrollView.showsHorizontalScrollIndicator = NO; scrollView.contentSize = [self contentSizeForPagingScrollView]; scrollView.delegate = self; self.view = scrollView; } 

This is how I try to set the content offset

 #define myView (UIScrollView *)self.view [myView setContentOffset:[self contentOffsetForIndex:index]]; [..] - (CGPoint)contentOffsetForIndex:(NSUInteger)index { return CGPointMake((index * (self.view.frame.size.width)), 0); } 
+7
source share
2 answers

Do you scroll horizontally or vertically? your bias logic
CGPointMake((index * (self.view.frame.size.width)), 0); will move to 0 on the y axis. CHANGE
I was lazy writing the approach.
Here is the approach I take to find related questions:

a) program offset is set by setContentOffset :, scrollRectToVisible, setFrame (from UIScrollView, internally calls SetContentOffset). First check all this in your code if they are correct.

b) print the Logs for offset parameters set in the scrollViewDidScroll: method.

 - (void)scrollViewDidScroll:(UIScrollView *)scrollView { NSLog(@" Offset = %@ ",NSStringFromCGPoint(scrollView.contentOffset)); } 

This will help you determine the set offset values ​​(in most cases, the required offset is overridden by another call to setContentOffset: somewhere in the code, it allows you to say that the current offset (130.0), and you want to set the offset (300.0) in steps (if they are animated), you can get logs, for example, Offset = {130,0}
Offset = {190,0}
Offset = {220,0}
Offset = {270,0}
Offset = {300,0}
Offset = {130,0}
Offset = {190,0}
Offset = {220,0}
Offset = {270,0}
Offset = {300,0}

If there is another call to setContentOffset (animated), you can say that it sets the offset to (0,0), the logs you get will look like Offset = {130,0}
Offset = {190,0}
Offset = {90, 0}
Offset = {220,0}
Offset = {60,0}
Offset = {270,0}
Offset = {30,0}
Offset = {300,0}
Offset = {0,0}
Offset = {130,0}
Offset = {190,0}
Offset = {90, 0}
Offset = {220,0}
Offset = {60,0}
Offset = {270,0}
Offset = {30,0}
Offset = {300,0}
Offset = {0,0}

If you notice the second pattern in the logs, you can be sure that there is an incorrect call to setContentOffset.
3) After you have determined that there is an unintended call to setContentOffset, try to find its source (setting a breakpoint for setContentOffset may help) and can be considered in each case. eg:
_adjustContentOffsetIfNecessary , is an internal method called when a frame is set for UIScrollView. This may be the reason (try applying the previous offset after setting the frame).

Hope this helps (also when removing down) :)

+12
source

I have the same problem (kinda), but my solution to my problem was that I disabled

 pagingEnabled 

UIScrollView ... When your UIScrollview is reset to a specific place when setting up ContentOffset, make sure your pagingEnabled is disabled ...

+1
source

All Articles