(封装)容器视图控制器(适用于新闻框架)
来源:互联网 发布:淘宝上搜高仿用什么词 编辑:程序博客网 时间:2024/05/07 23:03
// 使用方法
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 = [[UIWindowalloc] initWithFrame:[[UIScreenmainScreen] bounds]];
[_windowmakeKeyAndVisible];
FirstViewController *firstVC = [[FirstViewControlleralloc] init];
SecondViewController *secondVC = [[SecondViewControlleralloc] init];
ThirdViewController *thirdVC = [[ThirdViewControlleralloc] init];
NSArray *titleArray =@[@"first",@"second", @"third"];
NSArray *viewController =@[firstVC, secondVC, thirdVC];
ZLQWheelsViewController *zlqWheelsVC = [[ZLQWheelsViewControlleralloc] initWithTitleArray:titleArrayandViewControllerArray:viewController];
UINavigationController *zlqWheelsNC = [[UINavigationControlleralloc] initWithRootViewController:zlqWheelsVC];
[_windowsetRootViewController:zlqWheelsNC];
returnYES;
}
ZLQWheelsViewController.h
#import <UIKit/UIKit.h>
@interface ZLQWheelsViewController :UIViewController
// 容器视图控制器
- (nonnullinstancetype)initWithTitleArray:(nonnullNSArray *)titleArray andViewControllerArray:(nonnullNSArray *)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
- (nonnullinstancetype)initWithTitleArray:(nonnullNSArray *)titleArray andViewControllerArray:(nonnullNSArray *)viewControllerArray {
self = [superinit];
if (self) {
[selfsetTitleArray:(NSMutableArray *)titleArray];
[selfsetViewControllerArray:(NSMutableArray *)viewControllerArray];
}
returnself;
}
- (void)viewDidLoad {
[superviewDidLoad];
// Do any additional setup after loading the view.
[[[selfnavigationController] navigationBar]setTranslucent:NO];
self.wheelsView = [[ZLQWheelsViewalloc] initWithFrame:[[UIScreenmainScreen] bounds]];
[self.viewaddSubview:_wheelsView];
[_wheelsViewsetTitleContent:_titleArray];
[_wheelsViewsetBgScrollViewWithView:[selfaddChildViewControllers:_viewControllerArray]];
[_wheelsViewsetDelegate:self];
}
// 添加子视图
- (nonnullNSArray *)addChildViewControllers:(nonnullNSArray *)array {
NSMutableArray *viewArray = [NSMutableArrayarray];
for (UIViewController *viewControllerin array) {
[selfaddChildViewController:viewController];
[viewArray addObject:viewController.view];
}
return viewArray;
}
- (void)addHeaderData {
}
- (void)addFooterData {
}
#warning
// 当需要执行刷新方法时需调用该方法 并在ZLQWheelsViewHeader.h文件中引入所有你所需要的ViewController头文件并在各个ViewController文件中重写 addHeaderData和 addFooterData 方法
- (void)endDraggingAction:(NSInteger)tag {
UIViewController *viewController = [_viewControllerArrayobjectAtIndex:tag];
/*
*下拉刷新的方法
[viewController addHeaderData];
*
[viewController addFooterData];
*/
}
- (void)becomeChildViewController:(nonnullUIViewController *)viewController {
}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
// 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
#import "ZLQWheelsView.h"
#define CURRENTSCEEN [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 = [superinitWithFrame:frame];
if (self) {
[selfcreateSubViews];
}
returnself;
}
//初始化子类
- (void)createSubViews{
CGFloat w =CURRENTSCEEN.size.width /6;
self.textColor = [UIColorredColor];
self.fontSize =20;
self.wheelsVernierModel =VernierModelNormal;
self.currentIndex =0;
self.btnArray = [NSMutableArrayarray];
self.contentArray = [NSMutableArrayarray];
self.backGroundImageView = [[UIImageViewalloc] initWithFrame:CGRectMake(0,0, CURRENTSCEEN.size.width,40)];
[selfaddSubview:_backGroundImageView];
self.titleScrollView = [[UIScrollViewalloc] initWithFrame:CGRectMake(0,0, CURRENTSCEEN.size.width,40)];
_titleScrollView.showsHorizontalScrollIndicator =NO;
_titleScrollView.bounces =NO;
[selfaddSubview:_titleScrollView];
self.vernier = [[UIImageViewalloc] initWithFrame:CGRectMake(10,0, w - 20,30)];
_vernier.backgroundColor = [UIColorlightGrayColor];
_vernier.alpha =0.5;
_vernier.center =CGPointMake(w / 2,20);
_vernier.layer.masksToBounds =YES;
_vernier.layer.cornerRadius =_vernier.frame.size.width /3.5;
[_titleScrollViewaddSubview:_vernier];
self.bgScrollView = [[UIScrollViewalloc] initWithFrame:CGRectMake(0,40, CURRENTSCEEN.size.width,CURRENTSCEEN.size.height -40)];
_bgScrollView.backgroundColor = [UIColorlightGrayColor];
_bgScrollView.showsHorizontalScrollIndicator =NO;
_bgScrollView.bounces =NO;
_bgScrollView.delegate =self;
_bgScrollView.pagingEnabled =YES;
[selfaddSubview:_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 *titlein array) {
UIButton *btn = [UIButtonbuttonWithType:UIButtonTypeCustom];
btn.frame =CGRectMake(0,0, w - 20,30);
btn.center =CGPointMake(w * i + w / 2,20);
[btn setTitle:titleforState:UIControlStateNormal];
[[btn titleLabel]setFont:[UIFontsystemFontOfSize:15]];
btn.tag =100 + i;
[btn addTarget:selfaction:@selector(btnAction:)forControlEvents:UIControlEventTouchUpInside];
if (i ==0) {
_vernier.frame = btn.frame;
}
[_titleScrollViewaddSubview:btn];
[_btnArrayaddObject:btn];
i++;
}
_titleScrollView.contentSize =CGSizeMake(w * array.count -CURRENTSCEEN.size.width,0);
//设置游标初始化
UIButton *btn = [_btnArrayobjectAtIndex:0];
if (_wheelsVernierModel ==VernierModelZoom) {
[selfsetVernierZoom:btn.center];
}elseif (_wheelsVernierModel ==VernierModelHighLight){
[selfsetVernierHighLight:btn.center];
}elseif (_wheelsVernierModel ==VernierModelTextHighLightAndZoom){
[selfsetVernierHighLightAndZomm: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 = [arrayobjectAtIndex:i];
tableView.frame =CGRectMake(i * w, 0, w,CURRENTSCEEN.size.height -100);
[_bgScrollViewaddSubview:tableView];
[_contentArrayaddObject:tableView];
}
_bgScrollView.contentSize =CGSizeMake(w * array.count,0);
_currentView = [_contentArrayobjectAtIndex: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 = [arrayobjectAtIndex:i];
view.frame =CGRectMake(i * w, 0, w,CURRENTSCEEN.size.height -100);
[_bgScrollViewaddSubview:view];
[_contentArrayaddObject:view];
}
_bgScrollView.contentSize =CGSizeMake(w * array.count,0);
_currentView = [_contentArrayobjectAtIndex:0];
}
//头部按钮的点击时间
- (void)btnAction:(UIButton *)btn{
if (_bgScrollView.subviews.count >= 2) {
_bgScrollView.contentOffset =CGPointMake(CURRENTSCEEN.size.width * (btn.tag - 100), 0);
}
[UIViewanimateWithDuration:0.5animations:^{
_vernier.center = btn.center;
} completion:^(BOOL finished) {
}];
_currentView = [_contentArrayobjectAtIndex:btn.tag -100];
if ([_delegaterespondsToSelector:@selector(titleButtonAction:)]) {
//代理方法
[_delegatetitleButtonAction:btn];
}
if ([_delegaterespondsToSelector:@selector(titleButtonActionWithView:)]) {
//代理方法
[_delegatetitleButtonActionWithView:_currentView];
}
if (_wheelsVernierModel ==VernierModelZoom) {
[selfsetVernierZoom:_vernier.center];
}elseif (_wheelsVernierModel ==VernierModelHighLight){
[selfsetVernierHighLight:_vernier.center];
}elseif (_wheelsVernierModel ==VernierModelTextHighLightAndZoom){
[selfsetVernierHighLightAndZomm:_vernier.center];
}
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
if (_wheelsVernierModel ==VernierModelZoom) {
[selfsetVernierZoom:_vernier.center];
}elseif (_wheelsVernierModel ==VernierModelHighLight){
[selfsetVernierHighLight:_vernier.center];
}elseif (_wheelsVernierModel ==VernierModelTextHighLightAndZoom){
[selfsetVernierHighLightAndZomm:_vernier.center];
}
}
//滑动切换页面触发事件
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
[UIViewanimateWithDuration:0.2animations:^{
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) {
[UIViewanimateWithDuration:0.2animations:^{
NSInteger index = scrollView.contentOffset.x /CURRENTSCEEN.size.width;
CGPoint point =CGPointMake(0,0);
if (_currentIndex != index) {
_currentIndex = index;
_currentView = [_contentArrayobjectAtIndex:index];
//当页面进行切换了触发代理事件
if ([_delegaterespondsToSelector:@selector(endDraggingAction:)]) {
[_delegateendDraggingAction:_currentIndex];
}
if ([_delegaterespondsToSelector:@selector(endDraggingActionWithView:)]) {
[_delegateendDraggingActionWithView:_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;
}elseif (index < 3){
_titleScrollView.contentOffset = point;
}elseif (index < _btnArray.count -2){
point.x =_titleScrollView.contentSize.width;
_titleScrollView.contentOffset = point;
}
if (_titleScrollView.contentOffset.x <= 0) {
point.x =0;
_titleScrollView.contentOffset = point;
}elseif (_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 = [UIButtonbuttonWithType:UIButtonTypeCustom];
_rightButton.frame =CGRectMake(CURRENTSCEEN.size.width - w,0, w, 40);
[_rightButtonaddTarget:selfaction:@selector(rightBtnAction:)forControlEvents:UIControlEventTouchUpInside];
}
[_rightButtonsetTitle:title forState:UIControlStateNormal];
[_rightButtonsetTitleColor:textColor forState:UIControlStateNormal];
[_rightButtonsetBackgroundColor:color];
[selfaddSubview:_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 = [UIButtonbuttonWithType:UIButtonTypeCustom];
_rightButton.frame =CGRectMake(CURRENTSCEEN.size.width - w,0, w, 40);
[_rightButtonaddTarget:selfaction:@selector(rightBtnAction:)forControlEvents:UIControlEventTouchUpInside];
}
[_rightButtonsetBackgroundImage:image forState:UIControlStateNormal];
[selfaddSubview:_rightButton];
_titleScrollView.contentSize =CGSizeMake(w * (_btnArray.count +1), 0);
}
//右边按钮的点击事件
- (void)rightBtnAction:(UIButton *)button{
[_delegaterightBtnClick: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 = [_btnArrayobjectAtIndex:0];
if (_wheelsVernierModel ==VernierModelZoom) {
[selfsetVernierZoom:btn.center];
}elseif (_wheelsVernierModel ==VernierModelHighLight){
[selfsetVernierHighLight:btn.center];
}elseif (_wheelsVernierModel ==VernierModelTextHighLightAndZoom){
[selfsetVernierHighLightAndZomm:btn.center];
}
}
- (void)setVernierZoom:(CGPoint)point{
[UIViewanimateWithDuration:0.3animations:^{
for (UIButton *buttonin _btnArray) {
// NSLog(@"%f", _fontSize);
if (button.center.x == point.x) {
button.titleLabel.font = [UIFontsystemFontOfSize:_fontSize];
}else{
button.titleLabel.font = [UIFontsystemFontOfSize:17];
}
}
}];
}
- (void)setVernierHighLight:(CGPoint)point{
[UIViewanimateWithDuration:0.3animations:^{
for (UIButton *buttonin _btnArray) {
if (button.center.x == point.x) {
[button setTitleColor:_textColorforState:UIControlStateNormal];
}else{
[button setTitleColor:[UIColorblackColor] forState:UIControlStateNormal];
}
}
}];
}
- (void)setVernierHighLightAndZomm:(CGPoint)point{
[selfsetVernierHighLight:point];
[selfsetVernierZoom:point];
}
@end
- (封装)容器视图控制器(适用于新闻框架)
- 实现一个容器视图控制器(上)
- 模型-视图-控制器(MVC)封装逻辑
- yii2框架(二):控制器和视图
- 封装框架(六)加载控制器
- Custom Container View Controller(自定义容器视图控制器)
- 自定义视图控制器容器
- 容器视图控制器
- UI_容器视图控制器
- iOS 容器视图控制器
- 容器视图控制器
- 封装框架(八)视图类
- iOS--项目最常见框架的搭建(标签栏控制器+导航栏控制器+视图控制器)
- ViewController (视图控制器)
- UI_ViewController(视图控制器)
- MVC框架的封装(五)控制器中连接数据库
- 学习mvc框架封装(四)加载控制器
- MVC框架的封装(五)控制器中连接数据库
- iTunes同步文件到手机应用中的方法
- ”数独“android小游戏
- 常规功能和模块自定义系统 (cfcmms)—014模块自定义概述(1)
- 机器视觉LED光源照明技术说明
- Recover the home data frome Linux
- (封装)容器视图控制器(适用于新闻框架)
- android异常
- java发送电子邮件email
- MDK5+CH340驱动安装
- 如何在代码和xml中实现中划线的方法!
- C#通过并口连接斑马打印机
- trie树
- Object C
- 访问图像像素的一些方法