如何将UIPageControl作为 UIViewController的容器
7个回答
展开全部
创建界面首先我们需要在 Interface Builder 中在一个UIViewController里面创建一个UIPageControl与UIScrollView。当然你可以创建很多类似的UIViewController。设置PageViewController 类这里的 PageViewController 对象将包含一个 UIScrollView 与一个 UIPageControl。而且 UIScrollView 将会包含所有的 subview, 这些subview就是被UIPageControl控制的子页面。你也许会在 viewController 中添加一些算法来专门处理页面转换: addChildViewController:方法。然而在我这个案例中却不是这么做的,来一起看看代码:@interface PagerViewController : UIViewController @property (nonatomic, strong) IBOutlet UIScrollView *scrollView; @property (nonatomic, strong) IBOutlet UIPageControl *pageControl; - (IBAction)changePage:(id)sender; @end 在实现的代码文件中,首先我先不让它能够旋转屏幕。然后当 PagerViewController 预备显示时,我们需要标记一下当前哪一个view是被激活的,即被显示的,那么需要在 viewDidApplear 以及 viewWillDisappear方法中加入以下代码:- (BOOL)automaticallyForwardAppearanceAndRotationMethodsToChildViewControllers { return NO; } - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; UIViewController *viewController = [self.childViewControllers objectAtIndex:self.pageControl.currentPage]; if (viewController.view.superview != nil) { [viewController viewDidAppear:animated]; } } - (void)viewWillDisappear:(BOOL)animated { UIViewController *viewController = [self.childViewControllers objectAtIndex:self.pageControl.currentPage]; if (viewController.view.superview != nil) { [viewController viewWillDisappear:animated]; } [super viewWillDisappear:animated]; } - (void)viewDidDisappear:(BOOL)animated { UIViewController *viewController = [self.childViewControllers objectAtIndex:self.pageControl.currentPage]; if (viewController.view.superview != nil) { [viewController viewDidDisappear:animated]; } [super viewDidDisappear:animated]; }viewWillAppear 可能会有点复杂,因为我们也需要加载所有的子view到 scrollView中,而且必须确保scrollview的contentsize比子view要大。- (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; for (NSUInteger i =0; i < [self.childViewControllers count]; i++) { [self loadScrollViewWithPage:i]; } self.pageControl.currentPage = 0; _page = 0; [self.pageControl setNumberOfPages:[self.childViewControllers count]]; UIViewController *viewController = [self.childViewControllers objectAtIndex:self.pageControl.currentPage]; if (viewController.view.superview != nil) { [viewController viewWillAppear:animated]; } self.scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * [self.childViewControllers count], scrollView.frame.size.height); }为了将UIViewController的内容加载到UIScrolView的contentView中,需要如下代码:- (void)loadScrollViewWithPage:(int)page { if (page < 0) return; if (page >= [self.childViewControllers count]) return; // replace the placeholder if necessary UIViewController *controller = [self.childViewControllers objectAtIndex:page]; if (controller == nil) { return; } // add the controller's view to the scroll view if (controller.view.superview == nil) { CGRect frame = self.scrollView.frame; frame.origin.x = frame.size.width * page; frame.origin.y = 0; controller.view.frame = frame; [self.scrollView addSubview:controller.view]; } }处理滚动为了处理滚动,我们需要实现几个 UIScrollViewDelegate 的方法以及尽早声明方法 - (IBAction)changePage:(id)sender首先必须先知道滚动是如何实现的,要么是手势,要么就是点击了 UIPageControl 的一侧。来一起看看下面的代码:// At the begin of scroll dragging, reset the boolean used when scrolls originate from the UIPageControl - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView { _pageControlUsed = NO; } // At the end of scroll animation, reset the boolean used when scrolls originate from the UIPageControl - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { _pageControlUsed = NO; } - (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView { UIViewController *oldViewController = [self.childViewControllers objectAtIndex:_page]; UIViewController *newViewController = [self.childViewControllers objectAtIndex:self.pageControl.currentPage]; [oldViewController viewDidDisappear:YES]; [newViewController viewDidAppear:YES]; _page = self.pageControl.currentPage; }现在,为了当页面改变时也能更新当前的显示,我们只需要实现 scrollViewDidScroll delegate方法以及 changePage IBAction 方法。- (IBAction)changePage:(id)sender { int page = ((UIPageControl *)sender).currentPage; // update the scroll view to the appropriate page CGRect frame = self.scrollView.frame; frame.origin.x = frame.size.width * page; frame.origin.y = 0; UIViewController *oldViewController = [self.childViewControllers objectAtIndex:_page]; UIViewController *newViewController = [self.childViewControllers objectAtIndex:self.pageControl.currentPage]; [oldViewController viewWillDisappear:YES]; [newViewController viewWillAppear:YES]; [self.scrollView scrollRectToVisible:frame animated:YES]; // Set the boolean used when scrolls originate from the UIPageControl. See scrollViewDidScroll: above. _pageControlUsed = YES; } - (void)scrollViewDidScroll:(UIScrollView *)sender { // We don't want a "feedback loop" between the UIPageControl and the scroll delegate in // which a scroll event generated from the user hitting the page control triggers updates from // the delegate method. We use a boolean to disable the delegate logic when the page control is used. if (_pageControlUsed || _rotating) { // do nothing - the scroll was initiated from the page control, not the user dragging return; } // Switch the indicator when more than 50% of the previous/next page is visible CGFloat pageWidth = self.scrollView.frame.size.width; int page = floor((self.scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1; if (self.pageControl.currentPage != page) { UIViewController *oldViewController = [self.childViewControllers objectAtIndex:self.pageControl.currentPage]; UIViewController *newViewController = [self.childViewControllers objectAtIndex:page]; [oldViewController viewWillDisappear:YES]; [newViewController viewWillAppear:YES]; self.pageControl.currentPage = page; [oldViewController viewDidDisappear:YES]; [newViewController viewDidAppear:YES]; _page = page; } }最后处理旋转首先在shouldAutorotateToInterfaceOrientation方法中返回 YES,同时也需要传输一下3个消息给当前被显示的子 UIViewController。
所以来看看以下处理方式- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return YES; } - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration { UIViewController *viewController = [self.childViewControllers objectAtIndex:self.pageControl.currentPage]; [viewController willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration]; _rotating = YES; } - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration { UIViewController *viewController = [self.childViewControllers objectAtIndex:self.pageControl.currentPage]; [viewController willAnimateRotationToInterfaceOrientation:toInterfaceOrientation duration:duration]; self.scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * [self.childViewControllers count], scrollView.frame.size.height); NSUInteger page = 0; for (viewController in self.childViewControllers) { CGRect frame = self.scrollView.frame; frame.origin.x = frame.size.width * page; frame.origin.y = 0; viewController.view.frame = frame; page++; } CGRect frame = self.scrollView.frame; frame.origin.x = frame.size.width * _page; frame.origin.y = 0; [self.scrollView scrollRectToVisible:frame animated:NO]; } - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation { _rotating = NO; UIViewController *viewController = [self.childViewControllers objectAtIndex:self.pageControl.currentPage]; [viewController didRotateFromInterfaceOrientation:fromInterfaceOrientation]; }
所以来看看以下处理方式- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return YES; } - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration { UIViewController *viewController = [self.childViewControllers objectAtIndex:self.pageControl.currentPage]; [viewController willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration]; _rotating = YES; } - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration { UIViewController *viewController = [self.childViewControllers objectAtIndex:self.pageControl.currentPage]; [viewController willAnimateRotationToInterfaceOrientation:toInterfaceOrientation duration:duration]; self.scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * [self.childViewControllers count], scrollView.frame.size.height); NSUInteger page = 0; for (viewController in self.childViewControllers) { CGRect frame = self.scrollView.frame; frame.origin.x = frame.size.width * page; frame.origin.y = 0; viewController.view.frame = frame; page++; } CGRect frame = self.scrollView.frame; frame.origin.x = frame.size.width * _page; frame.origin.y = 0; [self.scrollView scrollRectToVisible:frame animated:NO]; } - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation { _rotating = NO; UIViewController *viewController = [self.childViewControllers objectAtIndex:self.pageControl.currentPage]; [viewController didRotateFromInterfaceOrientation:fromInterfaceOrientation]; }
展开全部
将UIPageControl作为 UIViewController的容器方法如下:
创建界面
首先需要在 Interface Builder
中在一个UIViewController里面创建一个UIPageControl与UIScrollView。当然你可以创建很多类似的UIViewController。
设置 PageViewController 类
这里的 PageViewController 对象将包含一个 UIScrollView 与一个 UIPageControl。而且
UIScrollView 将会包含所有的 subview, 这些subview就是被UIPageControl控制的子页面。
处理滚动
为了处理滚动,需要实现几个 UIScrollViewDelegate 的方法以及尽早声明方法 -
(IBAction)changePage:(id)sender
首先必须先知道滚动是如何实现的,要么是手势,要么就是点击了 UIPageControl 的一侧。
最后处理旋转
首先在shouldAutorotateToInterfaceOrientation方法中返回 YES,同时也需要传输一下3个消息给当前被显示的子
UIViewController。
willAnimateRotationToInterfaceOrientation:duration:
willRotateToInterfaceOrientation:duration:
didRotateFromInterfaceOrientation:
创建界面
首先需要在 Interface Builder
中在一个UIViewController里面创建一个UIPageControl与UIScrollView。当然你可以创建很多类似的UIViewController。
设置 PageViewController 类
这里的 PageViewController 对象将包含一个 UIScrollView 与一个 UIPageControl。而且
UIScrollView 将会包含所有的 subview, 这些subview就是被UIPageControl控制的子页面。
处理滚动
为了处理滚动,需要实现几个 UIScrollViewDelegate 的方法以及尽早声明方法 -
(IBAction)changePage:(id)sender
首先必须先知道滚动是如何实现的,要么是手势,要么就是点击了 UIPageControl 的一侧。
最后处理旋转
首先在shouldAutorotateToInterfaceOrientation方法中返回 YES,同时也需要传输一下3个消息给当前被显示的子
UIViewController。
willAnimateRotationToInterfaceOrientation:duration:
willRotateToInterfaceOrientation:duration:
didRotateFromInterfaceOrientation:
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐于2016-01-04
展开全部
虽然看上去用 UIPageControl 在一系列 UIView或UIViewController中导航是很平常的事情,但实际上Apple公司并没有提供一个这样的方法或者演示Demo:
在最新的iOS版本中(5.0 现在已经不是最新的),Apple公司提供了很多如何用其他方式实现
UIViewController容器的方式(可以从这里参考),但悲剧的是它们与 UIPageControl 没多大关系。
那么我们今天就来专门解决这个问题。
创建界面
首先我们需要在 Interface Builder 中在一个UIViewController里面创建一个UIPageControl与UIScrollView。当然你可以创建很多类似的UIViewController。
设置 PageViewController 类
这里的 PageViewController 对象将包含一个 UIScrollView 与一个 UIPageControl。而且 UIScrollView 将会包含所有的 subview, 这些subview就是被UIPageControl控制的子页面。
你也许会在 viewController 中添加一些算法来专门处理页面转换: addChildViewController:方法。然而在我这个案例中却不是这么做的,来一起看看代码:
@interface PagerViewController : UIViewController
@property (nonatomic, strong) IBOutlet UIScrollView *scrollView;
@property (nonatomic, strong) IBOutlet UIPageControl *pageControl;
- (IBAction)changePage:(id)sender;
@end
在最新的iOS版本中(5.0 现在已经不是最新的),Apple公司提供了很多如何用其他方式实现
UIViewController容器的方式(可以从这里参考),但悲剧的是它们与 UIPageControl 没多大关系。
那么我们今天就来专门解决这个问题。
创建界面
首先我们需要在 Interface Builder 中在一个UIViewController里面创建一个UIPageControl与UIScrollView。当然你可以创建很多类似的UIViewController。
设置 PageViewController 类
这里的 PageViewController 对象将包含一个 UIScrollView 与一个 UIPageControl。而且 UIScrollView 将会包含所有的 subview, 这些subview就是被UIPageControl控制的子页面。
你也许会在 viewController 中添加一些算法来专门处理页面转换: addChildViewController:方法。然而在我这个案例中却不是这么做的,来一起看看代码:
@interface PagerViewController : UIViewController
@property (nonatomic, strong) IBOutlet UIScrollView *scrollView;
@property (nonatomic, strong) IBOutlet UIPageControl *pageControl;
- (IBAction)changePage:(id)sender;
@end
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
创建界面
首先我们需要在 Interface Builder 中在一个UIViewController里面创建一个UIPageControl与UIScrollView。当然你可以创建很多类似的UIViewController。
设置 PageViewController 类
这里的 PageViewController 对象将包含一个 UIScrollView 与一个 UIPageControl。而且 UIScrollView 将会包含所有的 subview, 这些subview就是被UIPageControl控制的子页面。
你也许会在 viewController 中添加一些算法来专门处理页面转换: addChildViewController:方法。然而在我这个案例中却不是这么做的,来一起看看代码:
1 @interface PagerViewController : UIViewController
2
3 @property (nonatomic, strong) IBOutlet UIScrollView *scrollView;
4 @property (nonatomic, strong) IBOutlet UIPageControl *pageControl;
5
6 - (IBAction)changePage:(id)sender;
7 @end
在实现的代码文件中,首先我先不让它能够旋转屏幕。然后当 PagerViewController 预备显示时,我们需要标记一下当前哪一个view是被激活的,即被显示的,那么需要在 viewDidApplear 以及 viewWillDisappear方法中加入以下代码:
01 - (BOOL)automaticallyForwardAppearanceAndRotationMethodsToChildViewControllers {
02 return NO;
03 }
04
05 - (void)viewDidAppear:(BOOL)animated {
06 [super viewDidAppear:animated];
07 UIViewController *viewController = [self.childViewControllers objectAtIndex:self.pageControl.currentPage];
08 if (viewController.view.superview != nil) {
09 [viewController viewDidAppear:animated];
10 }
11 }
12
13 - (void)viewWillDisappear:(BOOL)animated {
14 UIViewController *viewController = [self.childViewControllers objectAtIndex:self.pageControl.currentPage];
15 if (viewController.view.superview != nil) {
16 [viewController viewWillDisappear:animated];
17 }
18 [super viewWillDisappear:animated];
19 }
20
21 - (void)viewDidDisappear:(BOOL)animated {
22 UIViewController *viewController = [self.childViewControllers objectAtIndex:self.pageControl.currentPage];
23 if (viewController.view.superview != nil) {
24 [viewController viewDidDisappear:animated];
25 }
26 [super viewDidDisappear:animated];
27 }
viewWillAppear 可能会有点复杂,因为我们也需要加载所有的子view到 scrollView中,而且必须确保scrollview的contentsize比子view要大。
01 - (void)viewWillAppear:(BOOL)animated {
02 [super viewWillAppear:animated];
03
04 for (NSUInteger i =0; i < [self.childViewControllers count]; i++) {
05 [self loadScrollViewWithPage:i];
06 }
07
08 self.pageControl.currentPage = 0;
09 _page = 0;
10 [self.pageControl setNumberOfPages:[self.childViewControllers count]];
11
12 UIViewController *viewController = [self.childViewControllers objectAtIndex:self.pageControl.currentPage];
13 if (viewController.view.superview != nil) {
14 [viewController viewWillAppear:animated];
15 }
16
17 self.scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * [self.childViewControllers count], scrollView.frame.size.height);
18 }
为了将UIViewController的内容加载到UIScrolView的contentView中,需要如下代码:
01 - (void)loadScrollViewWithPage:(int)page {
02 if (page < 0)
03 return;
04 if (page >= [self.childViewControllers count])
05 return;
06
07 // replace the placeholder if necessary
08 UIViewController *controller = [self.childViewControllers objectAtIndex:page];
09 if (controller == nil) {
10 return;
11 }
12
13 // add the controller's view to the scroll view
14 if (controller.view.superview == nil) {
15 CGRect frame = self.scrollView.frame;
16 frame.origin.x = frame.size.width * page;
17 frame.origin.y = 0;
18 controller.view.frame = frame;
19 [self.scrollView addSubview:controller.view];
20 }
21 }
首先我们需要在 Interface Builder 中在一个UIViewController里面创建一个UIPageControl与UIScrollView。当然你可以创建很多类似的UIViewController。
设置 PageViewController 类
这里的 PageViewController 对象将包含一个 UIScrollView 与一个 UIPageControl。而且 UIScrollView 将会包含所有的 subview, 这些subview就是被UIPageControl控制的子页面。
你也许会在 viewController 中添加一些算法来专门处理页面转换: addChildViewController:方法。然而在我这个案例中却不是这么做的,来一起看看代码:
1 @interface PagerViewController : UIViewController
2
3 @property (nonatomic, strong) IBOutlet UIScrollView *scrollView;
4 @property (nonatomic, strong) IBOutlet UIPageControl *pageControl;
5
6 - (IBAction)changePage:(id)sender;
7 @end
在实现的代码文件中,首先我先不让它能够旋转屏幕。然后当 PagerViewController 预备显示时,我们需要标记一下当前哪一个view是被激活的,即被显示的,那么需要在 viewDidApplear 以及 viewWillDisappear方法中加入以下代码:
01 - (BOOL)automaticallyForwardAppearanceAndRotationMethodsToChildViewControllers {
02 return NO;
03 }
04
05 - (void)viewDidAppear:(BOOL)animated {
06 [super viewDidAppear:animated];
07 UIViewController *viewController = [self.childViewControllers objectAtIndex:self.pageControl.currentPage];
08 if (viewController.view.superview != nil) {
09 [viewController viewDidAppear:animated];
10 }
11 }
12
13 - (void)viewWillDisappear:(BOOL)animated {
14 UIViewController *viewController = [self.childViewControllers objectAtIndex:self.pageControl.currentPage];
15 if (viewController.view.superview != nil) {
16 [viewController viewWillDisappear:animated];
17 }
18 [super viewWillDisappear:animated];
19 }
20
21 - (void)viewDidDisappear:(BOOL)animated {
22 UIViewController *viewController = [self.childViewControllers objectAtIndex:self.pageControl.currentPage];
23 if (viewController.view.superview != nil) {
24 [viewController viewDidDisappear:animated];
25 }
26 [super viewDidDisappear:animated];
27 }
viewWillAppear 可能会有点复杂,因为我们也需要加载所有的子view到 scrollView中,而且必须确保scrollview的contentsize比子view要大。
01 - (void)viewWillAppear:(BOOL)animated {
02 [super viewWillAppear:animated];
03
04 for (NSUInteger i =0; i < [self.childViewControllers count]; i++) {
05 [self loadScrollViewWithPage:i];
06 }
07
08 self.pageControl.currentPage = 0;
09 _page = 0;
10 [self.pageControl setNumberOfPages:[self.childViewControllers count]];
11
12 UIViewController *viewController = [self.childViewControllers objectAtIndex:self.pageControl.currentPage];
13 if (viewController.view.superview != nil) {
14 [viewController viewWillAppear:animated];
15 }
16
17 self.scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * [self.childViewControllers count], scrollView.frame.size.height);
18 }
为了将UIViewController的内容加载到UIScrolView的contentView中,需要如下代码:
01 - (void)loadScrollViewWithPage:(int)page {
02 if (page < 0)
03 return;
04 if (page >= [self.childViewControllers count])
05 return;
06
07 // replace the placeholder if necessary
08 UIViewController *controller = [self.childViewControllers objectAtIndex:page];
09 if (controller == nil) {
10 return;
11 }
12
13 // add the controller's view to the scroll view
14 if (controller.view.superview == nil) {
15 CGRect frame = self.scrollView.frame;
16 frame.origin.x = frame.size.width * page;
17 frame.origin.y = 0;
18 controller.view.frame = frame;
19 [self.scrollView addSubview:controller.view];
20 }
21 }
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询