Search code examples
iphoneobjective-ciosuiscrollviewuiscrollviewdelegate

UIScrollView issue: forbid to "scroll to empty space"


I have UIScrollView with 3 pages. It's difficult for me to explain my issue, that's why I will try to explain it comparing with table view.

When you have table view, you can scroll to see space that doesn't exist. I mean if you see 1 cell, you can scroll down and see white space without cells. I don't know the definition, that's why I will call it "Scroll to empty space".

Now is the question. I want to implement following: when user sees 1 page, he can't "Scroll to empty space" and when user is in the last page, he can "Scroll to empty space"

My code is:

    self.scroll = [[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, size.width, size.height)] autorelease];
    scroll.pagingEnabled = YES;
    scroll.showsHorizontalScrollIndicator = NO;
    scroll.showsVerticalScrollIndicator = NO;
    scroll.scrollsToTop = NO;
    scroll.delegate = self;
    scroll.contentSize = CGSizeMake(size.width * kNumberOfPages, size.height);
    scroll.backgroundColor = [UIColor greenColor];

What properties should I set to provide required functionality.


Solution

  • That's called bouncing.

    UIScrollView has a property called bounces that you can set to NO to prevent it from bouncing:

        scroll.bounces = NO;
    

    bounces

    A Boolean value that controls whether the scroll view bounces past the edge of content and back again.

            @property(nonatomic) BOOL bounces
    

    Discussion

    If the value of this property is YES, the scroll view bounces when it encounters a boundary of the content. Bouncing visually indicates that scrolling has reached an edge of the content. If the value is NO, scrolling stops immediately at the content boundary without bouncing. The default value is YES.

    You can set the value of that property to the value you require in your scroll delegate – scrollViewDidScroll: method.