Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIScrollView and scrollRectToVisible:animated:

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

like image 302
Alpinista Avatar asked Dec 08 '22 01:12

Alpinista


2 Answers

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

This is based off the Apple "photoscroller" example code 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 that version of the Photoscroller sample code is here:

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

like image 75
JosephH Avatar answered Dec 18 '22 13:12

JosephH


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.

like image 29
mikker Avatar answered Dec 18 '22 12:12

mikker