简体   繁体   中英

ScrollView with Paging

I have added 3 UIViewController in my ScrollView , like this

[self addChildViewController:[self.storyboard instantiateViewControllerWithIdentifier:@"view1"]];
[self addChildViewController:[self.storyboard instantiateViewControllerWithIdentifier:@"view2"]];
[self addChildViewController:[self.storyboard instantiateViewControllerWithIdentifier:@"view3"]];

How can i find which is visible ?

You only use - (void)addChildViewController:(UIViewController *)childController to build your own containment view controller (eg your own tabBarController), not to add a subview to a scrollview. For more information on view controller containment you should read https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/CreatingCustomContainerViewControllers/CreatingCustomContainerViewControllers.html and for a short summary you could read When to use addChildViewController vs pushViewController

If you want to add a view to a scrollview, you should simply use - (void)addSubview:(UIView *)view (eg [self.scrollView addSubview:viewOfViewControllerWithIdentifierView1] ). Also make sure you set the scrollviews contentSize and the subviews frame. However, if you want a scrollview to page between different view controllers, I'd recommend you take a look at UIPageViewController: https://developer.apple.com/library/ios/documentation/uikit/reference/UIPageViewControllerClassReferenceClassRef/UIPageViewControllerClassReference.html

You can check this by the calculating the contentoffset of you scroll view. For this You have to add the Delegate Methode of Scrollview.

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{

    page_control.currentPage=scrollView.contentOffset.x/320;
}

In above code I have multiple ChildView controller having Width of 320px means complete iPhone screen width. And page_control is the Page Controller for Which I'm selecting the current Page.

Let me ask If you have any confusion.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM