Shrinking a UIScrollView to focus on a certain page

I have a UIScrollView with horizontal pagination - similar to the weather app. Under certain circumstances, I want to prevent the user from scrolling horizontally. Simply disabling the scrolling is a little jarring - I would prefer to duplicate the experience of scrolling past the last page - a little scrolling is permitted, but since there is no more content, the scrollview bounces back.

How might I go about implementing this? Should I be changing the contentSize of the ScrollView? How do I set up the pagination so that there is effectively one 'page' but there is still content displayed to the immediate left and right?

Answers


In order to always make the UIScrollView bounce, even with just one page, add the following line:

[self.scrollView setAlwaysBounceHorizontal:YES];

In addition to my comment about shrinking the contentSize, of course.


EDIT:

OK, let me first explain want we want to achieve. The User is scrolling happily between the pages in the UIScrollView, and suddenly, we want the UIScrollView to stick to the current page, which may or may not be the first page.

Our UIScrollView has the pages laid out, with each page represented by a single UIView or it's subclasses (it may have subviews itself):

Now, to perform the task explained, here is the code:

CGFloat pageWidth = _scrollView.bounds.size.width;
int currentPage = (_scrollView.contentOffset.x / pageWidth);

[_scrollView setContentSize:CGSizeMake(pageWidth, _scrollView.contentSize.height)];
// move each page to the left:
for (UIView* page in _scrollView.subviews) {
    [page setFrame:CGRectOffset(page.frame, -currentPage * pageWidth, 0)];
}

NOTE: If you have a complicated view hierarchy, just wrap it in a UIView that has a bounds size equal to the _scrollView.contentSize with all the pages. The rest I leave it as an exercise for the reader (I always wanted to say that xD)


Need Your Help

Make regex for exact matching two string words

regex asp.net-mvc asp.net-mvc-3 asp.net-web-api asp.net-web-api2

I am working on Attribute Routing of Web Api 2 . I am trying to implement a Route which contains a regex

How to make something global in Swift?

ios swift

I have a UILabel that I created in my viewDidLoad function. Then, I have a function that is outside of the viewDidLoad function. I need to be able to access the UILabel.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.