views:

186

answers:

2

I have a paging UIScrollView that pages through multiple full screen images. I am tiling the pages, queuing and dequeuing the UIViews dynamically as the scroll view pages through the collection of images, based on Apple example code.

I have a toolbar button the calls scrollRectToVisible:animated: to move the UIScrollView to a specific image. That works perfectly.

The problem is that if you then do a single touch in the UIScrollView, it scrolls back to the page it was displaying before the button was touched and the scrollRectToVisible:animated: method call scrolled the view.

If your touch is moving, the UIScrollView scrolls as expected, and subsequent touches do not cause the UIScrollView to move back to the original page.

How do I prevent this behavior?

Thanks

jk

+1  A: 

You need to use content offset rather than scrollRectToVisible, eg:

[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;                                                                                                                                         
}                                                                                                                                       

(and if you need frameForPagingScrollView, that came from the apple "photoscroller" example code from this years WWDC, available from the developer site)

JosephH
Thanks, Joseph. That worked perfectly. I was already using frameForPagingScrollView, so I just had to add your offsetForPageAtIndex method and drop in the method call and voila!
Alpinista
A: 

Though Joseph's answer sent me the right way, I had some odd behaviour when rotating the device. I found that using offset.x = (pagingScrollView.bounds.size.width * index); instead worked better.

mikker