Pregunta

I have a uiscrollview and I put 4 uiviews to make an option. I put label 1 for uiview1 and label 2 for uiview2, for uiview 3 i put label 3 and uiview4 i put label 4.

After that I hide the 4 uiview so everytime I pulldown the screen the 4 uiviews will display base on how far the user pull.

Can anyone give an example on how to display the which uiview is selected, when you pull the screen in ios iphone?

¿Fue útil?

Solución

You can use contentOffset of scroll View to find how far user pulled scroll View.

Use scroll View delegate Method:

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

By checking frames of your views & scrollView contentOffset, you can determine which view is visible (i.e. uiview1, uiview2,..).

Edit:

Your logic should be like this:

if(uiview1.frame.origin.y - scrollView.contentOffset.y > uiview1.frame.origin.y + uiview1.frame.size.height)
{
    //your logic for selection of uiview1
}else if.......

Apply this conditions from last view to first view.

Otros consejos

For your reference, you can see a sample implementation of a page control from here. http://developer.apple.com/library/ios/#samplecode/PageControl/

For the implementation you want, to your surprise, the scrollview's width is actually smaller than 320 (or 480). The magic property to set is:

scrollView.clipsToBounds = NO The only problem with this implementation is that the scrollview get no touch events if the touch is outside the bounds of the scrollView. This can be fix by passing its parent hitTest event to scrollView.

Just to link to to a better explanation: UIScrollView horizontal paging like Mobile Safari tabs

Slightly different from what I recommend but does the same thing.

Edit:

I have a small project called LXPagingViews that does the above, hopefully in an out of the box manner (Do drop me a pull request or feedback in issue): https://github.com/lxcid/LXPagingViews

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top