horizontal scroll view that snaps - iphone

I am looking for a way so that when I swipe left or right on a horizontal scroll view it will snap to one of the elements in the center, assuming that three elements (in this case a UIImageView) will be shown at once. How do I do this?

Turn on the pagingEnabled property for UIScrollView
If the value of this property is YES, the scroll view stops on multiples of the scroll view’s bounds when the user scrolls. The default value is NO.
You can set it programatically or in interface builder.

Related

How to perform an interactive animation with Auto Layout in which you interpolate between different constraints?

I'd like to have an interactive animation where a view transitions from one set of constraints to another as the user drags up/down the screen. The background image should shrink vertically (easy), and the profile image should change from being horizontally and vertically aligned to the background image to being anchored to the top and left corners of the background image. Is this possible?
Yes, it is possible, and you can see this effect in the Avvo app, for example (in the lawyer profile screen). The trick here is to smoothly transition from one set of constraints to another.
Here's an outline of how to do this:
add a UIScrollView. Add the view you want to animate as a subview to the scroll view.
Make your view controller implement
UIScrollViewDelegate
In the delegate method
scrollViewDidScroll(_:), update the constraints' constants as
needed.
Also in that method, when contentOffset crosses a
threshold value, flip to a different set of constraints, by setting
their active property to true or false.
To keep the view (the headshot image, in my example), pinned to the top as you scroll, just continuously update its top spacing constraints based on the contentOffset.y value.
Achieving a perfectly smooth transition may take some trial and error, but it definitely can be done!
Here are the screenshots showing the transition as you scroll up:

Make Area Page-able

How Can I make the green area of the screen shot of my app be able to scroll with the paging animation and page indicators?I would like for me to add more icon-buttons to it with keeping the header the same size.
First you need a scroll view and your view has to be inside of the scroll view.
Then you need to enable paging on the scroll view
[scrollView setPagingEnabled:YES];
If the value of this property is YES, the scroll view stops on multiples of the scroll view’s bounds when the user scrolls. The default value is NO.
If you want to have page indicators you need to use UIPageControlwhich you will have to manage yourself as far as I know. You could always check after scrolling (see UIScrollViewDelegate ) which page you are on based on the contentOffset property of the scroll view.

UIScrollView in iOS doesn't response scroll to top

I have two UIScrollViews, one is horizental, the other is like page on the horizontal view. I want to click the status bar and have the vertical scrollView scroll to the top, but it doesn't work.
I searched for some information that said I must set UIScrollView.scrollsToTop=NO, but it doesn't work. Could somebody tell me why?
The scroll view only scrolls to the top if there is a single scroll view present with the scrollsToTop property set to YES.
Make sure it's set to NO on your horizontal scroll view and all the child, vertical scroll views contained within. Then, using the horizontal scroll view's delegate, as one vertical scroll view leaves the visible area, toggle the property to NO and toggle the incoming vertical scroll view's property to YES.

Content positioning into a UIScrollView

I'm trying to add a scroll view to an existing screen of my app to help scroll the screen upward when keyboard hides a text field into this view.
I have added the scroll view as a subview of the main view in Interface Builder. Then I added all other objects as subviews to this scrollview. I have set the scroll view size to 320x460 with x=0 and y=0. Now, I notice the layout is broken and all objects (labels,text fields overlap in the same place).
Do you know the proper way to position this scrollview in interface builder so that I can easily position the other objects?
Thx for helping,
Stephane
What you did is correct. It sounds like you moved the other UI elements into the scroll view after you positioned them. In this case they would default to the center position and be overlapped.
If you find it difficult to use dragging in Xcode's Interface Builder, try the position and size tab and type in the coordinates. Alternatively, reposition your UI elements in code.

Is there a way to achieve parallel scrolling of two tableview?

Let's say, i have two tableview inside a scrollview. After i scrolled up & down tableview1, and then touch and swipe to scroll the scrollview horizontally to tableview2, i want tableview2 to have the same vertical (up & down) position as tableview1. is there any way to achieve that?
thanks
You can put code in the UITableViews' viewWillDisplay: methods that grabs the other table view's scroll position (called a "content offset" in scroll view parlance) and calls setContentOffset:animated:. This way, whenever either scroll view will appear on the screen, it will automatically scroll to the other view's position.