当前位置:编程学习 > C/C++ >>

Using UIPageControl as a container UIViewController

虽然看上去用 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:方法。然而在我这个案例中却不是这么做的,来一起看看代码:
[cpp]  
@interface  PagerViewController : UIViewController  
    
@property (nonatomic, strong) IBOutlet UIScrollView *scrollView;  
@property (nonatomic, strong) IBOutlet UIPageControl *pageControl;  
    
- (IBAction)changePage:(id)sender;  
 @end  
在实现的代码文件中,首先我先不让它能够旋转屏幕。然后当 PagerViewController 预备显示时,我们需要标记一下当前哪一个view是被激活的,即被显示的,那么需要在 viewDidApplear 以及 viewWillDisappear方法中加入以下代码:
[cpp 
- (BOOL)automaticallyForwardAppearanceAndRotationMethodsToChildViewControllers {  
    returnNO;  
}  
    
- (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要大。
[cpp] 
- (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中,需要如下代码:
[cpp]  
- (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 的一侧。
 
来一起看看下面的代码:
[cpp]   
// 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 方法。
补充:软件开发 , C++ ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,