Prevent uiscrollview from scrolling further then c

2019-03-21 07:10发布

This question already has an answer here:

I have a UIScrollView which has subviews that create a content with a width of about 7000. Now I want the UIScrollView to not scroll any further then 2000.

I tried:

[scrollView setContentSize:CGSizeMake(768.0, 2000.0)];  
[scrollView setClipsToBounds:YES];

But I can still scroll to the end, how do I prevent this?

7条回答
男人必须洒脱
2楼-- · 2019-03-21 07:58

As others have said, implement the uiscrollviewdelegate

implement these methods:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView

- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate

- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView

and hopefully that will fix the jumping issue you were having

note, put in these methods similar code to the other posts like

static float stopx = 2000.0f;

if (scrollView.contentOffset.x >= stopx) {
    CGPoint stop = scrollView.contentOffset;
    stop.x = stopx;
    [scrollView setContentOffset:stop animated:NO];
}

At first performance will possibly be compromised, if this is true, begin taking out certain delegate methods until you get the desired behavior

查看更多
登录 后发表回答