8

I am creating an ImageViewer similar to the Photos app. I have a PagingScrollView that is the view of a view controller (created in loadView). The pages are UIScrollViews with UIImageView subviews. Everything works hunky dory until I try to 'skip' (set content offset) to an index besides the first one. If I skip to say, image 'i', the image is correctly displayed but if the user performs a single tap gesture the contentOffset resets itself back to display image '0'. The error does not happen with other gestures such as drag and it does not happen with touch if you do another gesture like drag first.

This is my loadView of the 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'm trying 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);
}
3
  • Maybe post the gesture recognizers? Everything here looks fine to me. Does the tap invoke the contentOffset-change of some sort? Commented Jun 13, 2011 at 22:54
  • I am not adding any gesture recognizers of my own. The tap is being captured by the paging scroll view and it resets its contentOffset back to display page 0. While it does this I get the scrollViewDidScroll delegate message Commented Jun 14, 2011 at 15:57
  • I am experiencing the same issue, in iOS 6. for iOS 5 it works fine. Also this bug disappear once I disable pagingEnabled or if I disable the scroll animation. Any one know a better way to fix this ? Commented Jun 7, 2013 at 8:14

2 Answers 2

13

Are you scrolling horizontally or vertically ? your offset logic
CGPointMake((index * (self.view.frame.size.width)), 0); is will move to 0 of Y axis.

Here is the approach i adopt to find offset related issues:

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

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

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

This will help you identify the offset values being set (in most cases, the required offset is overriden by another call to setContentOffset: , some where in the code; lets say the current offset is (130,0) and you want to set the offset (300,0) it happens in steps (if animated) , you might get logs like

Offset = {130,0}  
Offset = {190,0}  
Offset = {220,0}  
Offset = {270,0}  
Offset = {300,0}

If there is some other call to setContentOffset(animated), lets say it sets offset to (0,0), the logs you get would 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}

If you notice the second pattern in Logs, you can be sure that there is some un intended call to setContentOffset.
3) Once you identify there is a call to unintended call to setContentOffset, try to find the source of it (setting breakpoint to setContentOffset might help), and can be addressed on a case to case basis. for ex:
_adjustContentOffsetIfNecessary, is an internal method called when frame is set for UIScrollView. This can be the reason (try applying previous offset after setting the frame).

Sign up to request clarification or add additional context in comments.

3 Comments

I am only scrolling horizontally. The contentSize.height of the paging scrollView is small enough so that vertical scrolling is disabled so basically I just want the yPos of the contentOffset to stay the same. The problem is that the x portion of the offset is resetting itself on the first touch
thanks for the detailed suggestion. I don't have time to test it at the moment, but it sounds like a solid suggestion and will do so when I can. I'll update the thread with the results when I have them =D
@monkybonk05 update us if this solves your issue; if not , let us know what the logs/inferences to go a level deep.
1

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

pagingEnabled 

of the UIScrollView... When your UIScrollview is resetting to a specific place when setting ContentOffset, please check that your pagingEnabled is disabled...

1 Comment

I have similar issue. I use paging and I want to set offset. Even when I disable it temporarly it's still not set.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.