UIScrollView and scrollRectToVisible: animated:

I have a UIScrollView page that views multiple full-screen images. I mosaic pages, queues, and reorienting UIViews dynamically, like scrolling page views through a collection of images, based on Apple sample code.

I have a toolbar button: scrollRectToVisible: animated: move the UIScrollView to a specific image. This works great.

The problem is that if you then make one touch in the UIScrollView, it scrolls back to the page that it was showing before the button was touched and the call to scrollRectToVisible: animated: scrolls the view.

If your touch moves, the UIScrollView scrolls as expected, and subsequent touches do not cause the UIScrollView to return to the original page.

How to prevent this behavior?

thanks

Jk

+4
source share
2 answers

You need to use the content offset, not scrollRectToVisible, for example:

[pagingScrollView setContentOffset:[self offsetForPageAtIndex:page] animated:YES]; 

where offsetForPageAtIndex looks like this:

 - (CGPoint)offsetForPageAtIndex:(NSUInteger)index { CGRect pagingScrollViewFrame = [self frameForPagingScrollView]; CGPoint offset; offset.x = (pagingScrollViewFrame.size.width * index); offset.y = 0; return offset; } 

This is based on Apple's "photocroller" code example from WWDC 2010, which had a frameForPagingScrollView that looks like this:

 - (CGRect)frameForPagingScrollView { CGRect frame = [[UIScreen mainScreen] bounds]; frame.origin.x -= PADDING; frame.size.width += (2 * PADDING); return frame; } 

A full copy of this version of the Photoscroller sample code is here:

https://github.com/jogu/WWDC-2010/tree/master/PhotoScroller

+12
source

Although Joseph's answer sent me the correct path, I had some strange behavior when turning the device. I found that using offset.x = (pagingScrollView.bounds.size.width * index); instead improved.

+3
source

All Articles