How can I let UIScrollView (pagingEnabled) to move

2019-08-30 22:13发布

问题:

Ok. here is the story:

I have a UIWebView in full screen, width = 768

It loads several images and those images are in one line, which means the UIWebView will scroll only horizontally.

Now, I set the UIScrollView inside the UIWebView to pagingEnabled = YES. So the scroll on the UIWebView will move page by page.

The problem is that every image's width is about 900. I won't scale them down and if I scroll the UIWebView, from the 2nd page on, always 132points of previous image will show. This is not good for me.

So how can I manipulate the UIWebView or the UIScrollView inside the UIWebView so that each scroll will move a page of 900 points, if the view's frame width is 768?

The constraints are:

  1. I can't change its contentSize

  2. I can't change its bounds or frame

I say these constraints because UIWebView will change them on its own purpose when it loads contents.

Anyone would like to join this brain storming?

Thanks

回答1:

I'm thinking you're going to need to set a UIScrollView delegate that tracks touches beginning and ending. More specifically, scrollViewDidEndDragging:willDecelerate is what I'm thinking you'll need to use, as it fires the instant a user lifts their hand from the device.

First, pagingEnabled=NO seems obvious, we're going to have to control our own paging. Which, I don't feel is too tough. Track the direction of the scrolling in scrollViewDidScroll:, so that we have some global set: BOOL isGoingRight = YES or NO (NO means the last movement was to the left) float PAGESIZE = 900.0;

Here's how my scrollview delegate method would look like that handles paging.

- (void) scrollViewDidEndDragging:(UIScrollView*)_scrollView willDecelerate:(BOOL)willDecelerate {
    float offsetProportion = _scrollView.contentOffset.x/_scrollView.frame.size.width;
    //depending upon our direction, round up or down
    int page = isGoingRight? ceilf(offsetProportion): floorf(offsetProportion);
    //manually finish the scroll
    [_scrollView scrollRectToVisible:CGRectMake(page*PAGESIZE, 0, _scrollView.frame.size.width, _scrollView.frame.size.height) animated:YES];
}

I didn't test this, you may or may not need to disable touches at points.