模仿网易标签视图切换代码

 

#import "ViewController.h"
#import "SubViewController.h"

@interface ViewController ()<UIPageViewControllerDataSource,UIPageViewControllerDelegate,UIScrollViewDelegate>
{
    UIPageViewController *_pageViewController;
    NSMutableArray *_vcArray;
    NSInteger _currentPage;
    UIView *_coverView;
    UIScrollView *_scroll;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.translucent = NO;
    
    [self configScrollView];
    [self configPageViewController];
    
}

/**
 *  配置scrollView
 */
-(void)configScrollView
{
    _vcArray = [[NSMutableArray alloc]init];
    NSArray *titleArray = @[@"要闻",@"视频"
                            ,@"上海",@"财经"
                            ,@"科技",@"社会"
                            ,@"娱乐",@"体育"];
    
    _scroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen]bounds].size.width, 30)];
    [self.view addSubview:_scroll];
    
    for (int i = 0; i<titleArray.count; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
        btn.frame = CGRectMake(5 + 65 * i, 2, 60, 26);
        btn.backgroundColor = [UIColor orangeColor];
        [_scroll addSubview:btn];
        
        [btn setTitle:titleArray[i] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
        btn.tag = 100 + i;
    }
    
    _coverView = [[UIView alloc]initWithFrame:CGRectMake(5, 2, 60, 26)];
    [_scroll addSubview:_coverView];
    _coverView.backgroundColor = [UIColor colorWithWhite:0.1 alpha:0.7];
    
    _scroll.contentSize = CGSizeMake(65 * titleArray.count + 5, 0);
    
    for (NSString *title in titleArray) {
        SubViewController *sub = [[SubViewController alloc]init];
        sub.title = title;
        [_vcArray addObject:sub];
    }
}

//按钮点击事件
-(void)click:(UIButton *)btn
{
    NSInteger index = btn.tag - 100;
    //从视图控制器中取得与按钮相对应的对象
    SubViewController *sub = _vcArray[index];

    [_pageViewController setViewControllers:@[sub] direction:index < _currentPage animated:YES completion:nil];
    
    [self coverViewAnimate:index];
        
    _currentPage = index;
}

/**
 *   遮罩动画效果
 *
 *  @param index 当前的scrollView是第几个
 */
-(void)coverViewAnimate:(NSInteger)index
{
    //动画:移动覆盖的view
    [UIView animateWithDuration:0.25 animations:^{
        _coverView.frame = CGRectMake(5 + 65 * index, 2, 60, 26);
    } completion:^(BOOL finished) {
        //前一个动画完成以后,再加一个scrollView的动画 
        [UIView animateWithDuration:0.25 animations:^{
            if (_coverView.frame.origin.x < _scroll.contentOffset.x) {
                _scroll.contentOffset = CGPointMake(_coverView.frame.origin.x - 5, 0);
            }
            if (_coverView.frame.origin.x + 60 > _scroll.contentOffset.x + [[UIScreen mainScreen]bounds].size.width) {
                _scroll.contentOffset = CGPointMake(_coverView.frame.origin.x + 60 - [[UIScreen mainScreen]bounds].size.width + 5, 0);
            }
        }];
    }];

}

/**
 *  设置PageViewController
 */
-(void)configPageViewController
{
    CGFloat screenHeight = [[UIScreen mainScreen]bounds].size.height;
    CGFloat screenWidth = [[UIScreen mainScreen]bounds].size.width;
    _pageViewController = [[UIPageViewController alloc]initWithTransitionStyle:1 navigationOrientation:0 options:nil];
    _pageViewController.dataSource = self;
    _pageViewController.delegate = self;
    
    [_pageViewController setViewControllers:@[_vcArray[0]] direction:0 animated:NO completion:nil];
    _pageViewController.view.frame = CGRectMake(0, 30, screenWidth, screenHeight - 30);
    
    [self.view addSubview:_pageViewController.view];
    
    //PageVeiwController里面如果样式是UIPageViewControllerTransitionStyleScroll,在PageVeiwController的内部,会有一个scrollView
    for (UIView *view in _pageViewController.view.subviews)
    {
        if ([view isKindOfClass:[UIScrollView class]]) {
            UIScrollView *scrollView = (UIScrollView *)view;
            scrollView.delegate = self;
        }
    }
}

#pragma mark uiscrollView

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
    NSInteger index = [_vcArray indexOfObject:viewController];
    if (index == 0) {
        return nil;
    }
    return _vcArray[index - 1];
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
    NSInteger index = [_vcArray indexOfObject:viewController];
    if (index == _vcArray.count - 1) {
        return nil;
    }
    return _vcArray[index + 1];
}

- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed
{
    //如果需要取视图控制器里面的属性,需要用子类来强转
    //父类的声明,指向子类的对象
    UIViewController *vc = pageViewController.viewControllers[0];
    NSInteger index = [_vcArray indexOfObject:vc];
    _currentPage = index;
    [self coverViewAnimate:index];
}

@end

 

 

其中的subViewController是自定义的一个UITableViewController视图

posted @ 2015-08-21 12:11  aprogrammer  阅读(188)  评论(0编辑  收藏  举报