UIScrollView custom swap distance

I have a UIImage (image width> 320px) in a UIScrollView (covers the full screen width) and needs a swap effect.

All the tutorials / examples I found explain how to do this with multiple UIImages or UIVIews, but I only have 1 UIImage in a UIScrollView. My goal is to create a roulette effect. and should the scrollview "show" on every ribbon marker?

How to make paging at distances less than the width of the UIScrollView ?!

+2


a source to share


1 answer


I can't think of a cleaner solution than doing math on the fly. If the desired page length is 100 pixels than the UIImage "slide" so that it stays relative to the actual page. So akin to:

CGFloat pageWidth = 100;
int numPages = ceilf(myImageView.bounds.size.width / pageWidth);
scrollView.contentSize = CGSizeMake(numPages * scrollView.bounds.size.width,
                                  scrollView.bounds.size.height);

      

Then we subclass the scroll method layoutSubviews

:



- (void)layoutSubviews {
    int numPages = ceilf(myImageView.bounds.size.width / pageWidth);
    CGRect visibleBounds = [self bounds];
    CGFloat start = visibleBounds.origin.x;
    CGFloat offset = start / pageWidth;
    myImageView.frame = CGRectMake(offset, myImageView.frame.origin.y,
                                   myImageView.frame.size.width,
                                   myImageView.frame.size.height);
}   

      

I haven't tested this at all, but hopefully it gets you working on a production track. What this should do is move the image whenever you scroll so that it looks like each "page" only moves the 100px image.

+1


a source







All Articles