您的位置:首页 > 其它

(封装)容器视图控制器(适用于新闻框架)

2015-11-04 08:44 246 查看
// 使用方法

AppDelegate.h

#import "FirstViewController.h"
#import "SecondViewController.h"
#import "ZLQWheelsViewController.h"
#import "ThirdViewController.h"

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary
*)launchOptions {
    // Override point for customization after application launch.
    self.window = [[UIWindow
alloc] initWithFrame:[[UIScreen
mainScreen] bounds]];
    [_window
makeKeyAndVisible];
    FirstViewController *firstVC = [[FirstViewController
alloc] init];
    SecondViewController *secondVC = [[SecondViewController
alloc] init];
    ThirdViewController *thirdVC = [[ThirdViewController
alloc] init];
    NSArray *titleArray =
@[@"first",
@"second", @"third"];
    NSArray *viewController =
@[firstVC, secondVC, thirdVC];
    ZLQWheelsViewController *zlqWheelsVC = [[ZLQWheelsViewController
alloc] initWithTitleArray:titleArray
andViewControllerArray:viewController];
    UINavigationController *zlqWheelsNC = [[UINavigationController
alloc] initWithRootViewController:zlqWheelsVC];
    [_window
setRootViewController:zlqWheelsNC];
    
    return
YES;
}

ZLQWheelsViewController.h

#import <UIKit/UIKit.h>

@interface ZLQWheelsViewController :
UIViewController
// 容器视图控制器
- (nonnull
instancetype)initWithTitleArray:(nonnull
NSArray *)titleArray andViewControllerArray:(nonnull
NSArray *)viewControllerArray;

@end

ZLQWheelsViewController.m

#import "ZLQWheelsViewController.h"
#import "ZLQWheelsView.h"
#import "ZLQWheelsViewHeader.h"

@interface ZLQWheelsViewController () <WheelsViewDelegate>

@property (nonatomic,
strong) NSMutableArray *titleArray;
@property (nonatomic,
strong) NSMutableArray *viewControllerArray;
@property (nonatomic,
strong) ZLQWheelsView *wheelsView;

@end

@implementation ZLQWheelsViewController

- (nonnull
instancetype)initWithTitleArray:(nonnull
NSArray *)titleArray andViewControllerArray:(nonnull
NSArray *)viewControllerArray {
    self = [super
init];
    if (self) {
        [self
setTitleArray:(NSMutableArray *)titleArray];
        [self
setViewControllerArray:(NSMutableArray *)viewControllerArray];
    }
    return
self;
}

- (void)viewDidLoad {
    [super
viewDidLoad];
    // Do any additional setup after loading the view.
    [[[self
navigationController] navigationBar]
setTranslucent:NO];
    self.wheelsView = [[ZLQWheelsView
alloc] initWithFrame:[[UIScreen
mainScreen] bounds]];
    [self.view
addSubview:_wheelsView];
    [_wheelsView
setTitleContent:_titleArray];
    [_wheelsView
setBgScrollViewWithView:[self
addChildViewControllers:_viewControllerArray]];
    [_wheelsView
setDelegate:self];
}
// 添加子视图
- (nonnull
NSArray *)addChildViewControllers:(nonnull
NSArray *)array {
    NSMutableArray *viewArray = [NSMutableArray
array];
    for (UIViewController *viewController
in array) {
        [self
addChildViewController:viewController];
        [viewArray addObject:viewController.view];
    }
    return viewArray;
}

- (void)addHeaderData {

}

- (void)addFooterData {

}

#warning
// 当需要执行刷新方法时
需调用该方法 并在ZLQWheelsViewHeader.h文件中引入所有你所需要的ViewController头文件
并在各个ViewController文件中重写 addHeaderData
和 addFooterData 方法
- (void)endDraggingAction:(NSInteger)tag {
    
    UIViewController *viewController = [_viewControllerArray
objectAtIndex:tag];
    /*
     *下拉刷新的方法
    [viewController addHeaderData];
     *
    [viewController addFooterData];
     */
}

- (void)becomeChildViewController:(nonnull
UIViewController *)viewController {
    
}

- (void)didReceiveMemoryWarning {
    [super
didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

ZLQWheelsView.h

#import <UIKit/UIKit.h>

typedef enum : NSUInteger {
    VernierModelNormal,
    VernierModelZoom,
    VernierModelHighLight,
    VernierModelTextHighLightAndZoom,
    
} VernierModel;

@protocol WheelsViewDelegate <NSObject>

@optional
- (void)titleButtonAction:(UIButton *)button;//协议方法:头部按钮点击触发事件,参数:被点击的button(button.tag
= 100 + n, n为button在数组中的位置)
- (void)titleButtonActionWithView:(UIView *)view;//协议方法:头部按钮点击触发事件,参数:点击切换成当前的view
- (void)endDraggingAction:(NSInteger)tag;//协议方法:当滑动改变页面的时候触发,传一个NSInteger标记tag,tag代表当前显示在页面上的View的位置
- (void)endDraggingActionWithView:(UIView *)view;//协议方法:同上.传一个参数:当前的view
- (void)rightBtnClick:(UIButton *)btn;//右边按钮点击事件

@end

@interface ZLQWheelsView :
UIView

@property (nonatomic,
retain) id<WheelsViewDelegate> delegate;//代理

@property (nonatomic,
retain) NSMutableArray *btnArray;//按钮数组
@property (nonatomic,
assign) NSInteger currentIndex;//当前页面
@property (nonatomic,
retain) NSMutableArray *contentArray;//内容数组
@property (nonatomic,
assign) VernierModel wheelsVernierModel;//枚举设置游标类型
@property (nonatomic,
assign) CGFloat fontSize;//设置被选中时的大小(Zoom类型和HighLightZoom可用)
@property (nonatomic,
retain) UIColor *textColor;//字体颜色

/* 设置内容 */

- (void)setTitleContent:(NSArray *)array;//设置背景scrollView内容(tableView类型)
- (void)setBgScrollViewContents:(NSArray *)array;//设置背景scrollView内容(任意继承UIView类型)
- (void)setBgScrollViewWithView:(NSArray *)array;//设置右边按钮
- (void)setRightButtonWithImage:(UIImage *)image;//图片按钮
- (void)setRightButtonWithTitle:(NSString *)title Color:(UIColor *)color TextColor:(UIColor
*)textColor;//普通按钮只有按钮的title,背景色和text颜色
- (void)setVernierImage:(UIImage *)image;//设置游标背景

@end

ZLQWheelsView.m

#import "ZLQWheelsView.h"

#define CURRENTSCEEN
1b043
[UIScreen mainScreen].bounds

@interface ZLQWheelsView () <UIScrollViewDelegate>

@property (nonatomic,
retain) UIScrollView *titleScrollView;//头部scrollView
@property (nonatomic,
retain) UIScrollView *bgScrollView;//背景scrollView
@property (nonatomic,
retain) UIImageView *vernier;//游标
@property (nonatomic,
retain) UIButton *rightButton;//右边按钮默认为空
@property (nonatomic,
retain) UIView *currentView;//当前显示的View
@property (nonatomic,
retain) UIImageView *backGroundImageView;//设置背景

@end

@implementation ZLQWheelsView

- (instancetype)initWithFrame:(CGRect)frame{
    
    self = [super
initWithFrame:frame];
    
    if (self) {
        [self
createSubViews];
    }
    
    return
self;
}

//初始化子类
- (void)createSubViews{
    
    CGFloat w =
CURRENTSCEEN.size.width /
6;
    
    self.textColor = [UIColor
redColor];
    self.fontSize =
20;
    self.wheelsVernierModel =
VernierModelNormal;
    self.currentIndex =
0;
    self.btnArray = [NSMutableArray
array];
    self.contentArray = [NSMutableArray
array];
    
    self.backGroundImageView = [[UIImageView
alloc] initWithFrame:CGRectMake(0,
0, CURRENTSCEEN.size.width,
40)];
    [self
addSubview:_backGroundImageView];
    self.titleScrollView = [[UIScrollView
alloc] initWithFrame:CGRectMake(0,
0, CURRENTSCEEN.size.width,
40)];
    _titleScrollView.showsHorizontalScrollIndicator =
NO;
    _titleScrollView.bounces =
NO;
    
    [self
addSubview:_titleScrollView];
    
    self.vernier = [[UIImageView
alloc] initWithFrame:CGRectMake(10,
0, w - 20,
30)];
    _vernier.backgroundColor = [UIColor
lightGrayColor];
    _vernier.alpha =
0.5;
    _vernier.center =
CGPointMake(w / 2,
20);
    _vernier.layer.masksToBounds =
YES;
    _vernier.layer.cornerRadius =
_vernier.frame.size.width /
3.5;
    
    [_titleScrollView
addSubview:_vernier];
    
    self.bgScrollView = [[UIScrollView
alloc] initWithFrame:CGRectMake(0,
40, CURRENTSCEEN.size.width,
CURRENTSCEEN.size.height -
40)];
    _bgScrollView.backgroundColor = [UIColor
lightGrayColor];
    _bgScrollView.showsHorizontalScrollIndicator =
NO;
    _bgScrollView.bounces =
NO;
    _bgScrollView.delegate =
self;
    _bgScrollView.pagingEnabled =
YES;
    
    [self
addSubview:_bgScrollView];
}

//初始化头部
- (void)setTitleContent:(NSArray *)array{
    
    CGFloat w =
0;
    if (array.count <
6) {
        w = CURRENTSCEEN.size.width / array.count;
    }else{
        w = CURRENTSCEEN.size.width /
6;
    }
    
    NSInteger i =
0;
    
    for (NSString *title
in array) {
        
        UIButton *btn = [UIButton
buttonWithType:UIButtonTypeCustom];
        btn.frame =
CGRectMake(0,
0, w - 20,
30);
        btn.center =
CGPointMake(w * i + w / 2,
20);
        [btn setTitle:title
forState:UIControlStateNormal];
        [[btn titleLabel]
setFont:[UIFont
systemFontOfSize:15]];
        btn.tag =
100 + i;
        [btn addTarget:self
action:@selector(btnAction:)
forControlEvents:UIControlEventTouchUpInside];
        if (i ==
0) {
            _vernier.frame = btn.frame;
        }
        [_titleScrollView
addSubview:btn];
        [_btnArray
addObject:btn];
        i++;
    }
    
    _titleScrollView.contentSize =
CGSizeMake(w * array.count -
CURRENTSCEEN.size.width,
0);
    
    //设置游标初始化
    UIButton *btn = [_btnArray
objectAtIndex:0];
    
    if (_wheelsVernierModel ==
VernierModelZoom) {
        [self
setVernierZoom:btn.center];
    }else
if (_wheelsVernierModel ==
VernierModelHighLight){
        [self
setVernierHighLight:btn.center];
    }else
if (_wheelsVernierModel ==
VernierModelTextHighLightAndZoom){
        [self
setVernierHighLightAndZomm:btn.center];
    }
    
    if (_btnArray.count <
6) {
        _fontSize =
22;
    }
}

//初始化内容ScrollView
- (void)setBgScrollViewContents:(NSArray *)array{
    
    if (array.count !=
_titleScrollView.subviews.count -
1) {
        NSLog(@"titleScrollView上的button个数与bgScrollview所对应的个数不等!!!");
    }
    
    if (_contentArray.count >
0) {
        NSLog(@"已初始化了");
    }
    
    CGFloat w =
CURRENTSCEEN.size.width;
    
    for (int i =
0; i < array.count; i++) {
        
        UITableView *tableView = [array
objectAtIndex:i];
        tableView.frame =
CGRectMake(i * w, 0, w,
CURRENTSCEEN.size.height -
100);
        
        [_bgScrollView
addSubview:tableView];
        [_contentArray
addObject:tableView];
    }
    
    _bgScrollView.contentSize =
CGSizeMake(w * array.count,
0);
    _currentView = [_contentArray
objectAtIndex:0];
    
}

//初始化内容ScrollView
- (void)setBgScrollViewWithView:(NSArray *)array{
    
    if (array.count !=
_titleScrollView.subviews.count -
1) {
        NSLog(@"titleScrollView上的button个数与bgScrollview所对应的个数不等!!!");
    }
    
    if (_contentArray.count >
0) {
        NSLog(@"已初始化了");
    }
    
    CGFloat w =
CURRENTSCEEN.size.width;
    
    for (int i =
0; i < array.count; i++) {
        
        UIView *view = [array
objectAtIndex:i];
        view.frame =
CGRectMake(i * w, 0, w,
CURRENTSCEEN.size.height -
100);
        
        [_bgScrollView
addSubview:view];
        [_contentArray
addObject:view];
    }
    
    _bgScrollView.contentSize =
CGSizeMake(w * array.count,
0);
    
    _currentView = [_contentArray
objectAtIndex:0];
}

//头部按钮的点击时间
- (void)btnAction:(UIButton *)btn{
    
    if (_bgScrollView.subviews.count
>= 2) {
        _bgScrollView.contentOffset =
CGPointMake(CURRENTSCEEN.size.width * (btn.tag
- 100), 0);
    }
    
    [UIView
animateWithDuration:0.5
animations:^{
        _vernier.center = btn.center;
    } completion:^(BOOL finished) {
        
    }];
    
    _currentView = [_contentArray
objectAtIndex:btn.tag -
100];
    
    if ([_delegate
respondsToSelector:@selector(titleButtonAction:)]) {
        //代理方法
        [_delegate
titleButtonAction:btn];
    }
    
    if ([_delegate
respondsToSelector:@selector(titleButtonActionWithView:)]) {
        //代理方法
        [_delegate
titleButtonActionWithView:_currentView];
    }
    
    if (_wheelsVernierModel ==
VernierModelZoom) {
        [self
setVernierZoom:_vernier.center];
    }else
if (_wheelsVernierModel ==
VernierModelHighLight){
        [self
setVernierHighLight:_vernier.center];
    }else
if (_wheelsVernierModel ==
VernierModelTextHighLightAndZoom){
        [self
setVernierHighLightAndZomm:_vernier.center];
    }
    
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    if (_wheelsVernierModel ==
VernierModelZoom) {
        [self
setVernierZoom:_vernier.center];
    }else
if (_wheelsVernierModel ==
VernierModelHighLight){
        [self
setVernierHighLight:_vernier.center];
    }else
if (_wheelsVernierModel ==
VernierModelTextHighLightAndZoom){
        [self
setVernierHighLightAndZomm:_vernier.center];
    }
}

//滑动切换页面触发事件
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    [UIView
animateWithDuration:0.2
animations:^{
        
        CGFloat w =
0;
        
        if (_btnArray.count <
6) {
            w = CURRENTSCEEN.size.width /
_btnArray.count;
        }else{
            w = CURRENTSCEEN.size.width /
6;
        }
        
        CGFloat index = scrollView.contentOffset.x /
CURRENTSCEEN.size.width;
        CGFloat x = index * w  +
10;
        CGRect temp =
_vernier.frame;
        temp.origin.x = x;
        _vernier.frame = temp;
        
    } completion:^(BOOL finished) {
        
        [UIView
animateWithDuration:0.2
animations:^{
            
            NSInteger index = scrollView.contentOffset.x /
CURRENTSCEEN.size.width;
            CGPoint point =
CGPointMake(0,
0);
            
            if (_currentIndex != index) {
                _currentIndex = index;
                _currentView = [_contentArray
objectAtIndex:index];
                
                //当页面进行切换了触发代理事件
                if ([_delegate
respondsToSelector:@selector(endDraggingAction:)]) {
                    [_delegate
endDraggingAction:_currentIndex];
                }
                if ([_delegate
respondsToSelector:@selector(endDraggingActionWithView:)]) {
                    [_delegate
endDraggingActionWithView:_currentView];
                }
            }
            
            NSInteger max =
_btnArray.count -
2;
            NSInteger mid =
3;
            NSInteger location =
2;
            if (_btnArray.count <
6) {
                mid = _btnArray.count /
2 - 1;
                location = 1;
            }
            
            if (_rightButton ==
nil) {
                max = max - 1;
            }
            
            if (index >= mid && index < max) {
                
                point.x =
CURRENTSCEEN.size.width /
6 * (index - location);
                _titleScrollView.contentOffset = point;
                
            }else
if (index < 3){
                _titleScrollView.contentOffset = point;
                
            }else
if (index < _btnArray.count -
2){
                point.x =
_titleScrollView.contentSize.width;
                
                _titleScrollView.contentOffset = point;
            }
            
            if (_titleScrollView.contentOffset.x
<= 0) {
                point.x =
0;
                _titleScrollView.contentOffset = point;
            }else
if (_titleScrollView.contentOffset.x >=
_titleScrollView.contentSize.width){
                point.x =
_titleScrollView.contentSize.width;
                _titleScrollView.contentOffset = point;
            }
            
        }];
        
    }];
}

/*  设置右边按钮 */

//普通按钮只有按钮的title与背景色
- (void)setRightButtonWithTitle:(NSString *)title Color:(UIColor *)color TextColor:(UIColor
*)textColor{
    
    CGFloat w =
0;
    
    if (_btnArray.count <
6) {
        w = CURRENTSCEEN.size.width /
_btnArray.count;
    }else{
        w = CURRENTSCEEN.size.width /
6;
    }
    
    if (_rightButton ==
nil) {
        _rightButton = [UIButton
buttonWithType:UIButtonTypeCustom];
        _rightButton.frame =
CGRectMake(CURRENTSCEEN.size.width - w,
0, w, 40);
        
        [_rightButton
addTarget:self
action:@selector(rightBtnAction:)
forControlEvents:UIControlEventTouchUpInside];
    }
    
    [_rightButton
setTitle:title forState:UIControlStateNormal];
    [_rightButton
setTitleColor:textColor forState:UIControlStateNormal];
    [_rightButton
setBackgroundColor:color];
    [self
addSubview:_rightButton];
    
    _titleScrollView.contentSize =
CGSizeMake(w * (_btnArray.count +
1), 0);
}

//图片按钮
- (void)setRightButtonWithImage:(UIImage *)image{
    
    CGFloat w =
0;
    
    if (_btnArray.count <
6) {
        w = CURRENTSCEEN.size.width /
_btnArray.count;
    }else{
        w = CURRENTSCEEN.size.width /
6;
    }
    
    if (_rightButton ==
nil) {
        _rightButton = [UIButton
buttonWithType:UIButtonTypeCustom];
        _rightButton.frame =
CGRectMake(CURRENTSCEEN.size.width - w,
0, w, 40);
        
        [_rightButton
addTarget:self
action:@selector(rightBtnAction:)
forControlEvents:UIControlEventTouchUpInside];
    }
    
    [_rightButton
setBackgroundImage:image forState:UIControlStateNormal];
    [self
addSubview:_rightButton];
    
    _titleScrollView.contentSize =
CGSizeMake(w * (_btnArray.count +
1), 0);
}

//右边按钮的点击事件
- (void)rightBtnAction:(UIButton *)button{
    
    [_delegate
rightBtnClick:button];
}

//设置游标背景图
- (void)setVernierImage:(UIImage *)image{
    
    _vernier.layer.masksToBounds =
NO;
    _vernier.alpha =
1;
    _vernier.image = image;
    
}

- (void)setWheelsVernierModel:(VernierModel)wheelsVernierModel{
    
    if (wheelsVernierModel !=
VernierModelNormal) {
        //        _vernier.alpha = 0;
    }
    
    _wheelsVernierModel = wheelsVernierModel;
    
    if (_btnArray.count ==
0) {
        return;
    }
    
    UIButton *btn = [_btnArray
objectAtIndex:0];
    
    if (_wheelsVernierModel ==
VernierModelZoom) {
        [self
setVernierZoom:btn.center];
    }else
if (_wheelsVernierModel ==
VernierModelHighLight){
        [self
setVernierHighLight:btn.center];
    }else
if (_wheelsVernierModel ==
VernierModelTextHighLightAndZoom){
        [self
setVernierHighLightAndZomm:btn.center];
    }
    
}

- (void)setVernierZoom:(CGPoint)point{
    
    [UIView
animateWithDuration:0.3
animations:^{
        
        for (UIButton *button
in _btnArray) {
            //            NSLog(@"%f", _fontSize);
            if (button.center.x == point.x)
{
                button.titleLabel.font = [UIFont
systemFontOfSize:_fontSize];
            }else{
                button.titleLabel.font = [UIFont
systemFontOfSize:17];
            }
            
        }
    }];
}

- (void)setVernierHighLight:(CGPoint)point{
    
    [UIView
animateWithDuration:0.3
animations:^{
        
        for (UIButton *button
in _btnArray) {
            
            if (button.center.x == point.x)
{
                [button setTitleColor:_textColor
forState:UIControlStateNormal];
            }else{
                [button setTitleColor:[UIColor
blackColor] forState:UIControlStateNormal];
            }
        }
    }];
}

- (void)setVernierHighLightAndZomm:(CGPoint)point{
    [self
setVernierHighLight:point];
    [self
setVernierZoom:point];
}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: