ios-滚动导航条页面
// ViewController.m #import "ViewController.h"
#import "ScrollSliderView.h"
@interface ViewController ()
{
NSArray *titleArray;
}
@end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSArray *arr = @[@"家电",@"生鲜",@"小饰品",@"Apple全场",@"水果",@"疯狂折扣中",@"Hot美味零食",@"分期优惠",@"特卖专柜衣服",@"日常用品大集合",@"母婴海外"];
self.view.backgroundColor=[UIColor orangeColor];
self.automaticallyAdjustsScrollViewInsets=NO;
ScrollSliderView *scrollSliderView = [[ScrollSliderView alloc]initWithController:self withTitleArray:arr];
[self.view addSubview:scrollSliderView];
}
//
// ScrollSliderView.h #import <UIKit/UIKit.h> @interface ScrollSliderView : UIView<UIScrollViewDelegate>
#define VIEWWIDTH [UIScreen mainScreen].bounds.size.width
#define VIEWHEIGHT [UIScreen mainScreen].bounds.size.height
#define RAN_COLOR(CUS_RGB) [UIColor colorWithRed:((float)((CUS_RGB & 0xFF0000) >> 16))/255.0 green:((float)((CUS_RGB & 0xFF00) >> 8))/255.0 blue:((float)(CUS_RGB & 0xFF))/255.0 alpha:1.0f]
/*
*
*初始化数据
*/
-(id)initWithController:(UIViewController *)controller withTitleArray:(NSArray *)titleArray;
/*
*底部ScrollView
*/
@property (nonatomic,strong)UIScrollView *mainScrollView;
/*
*滑块ScrollView
*/
@property (nonatomic,strong)UIScrollView *sliderScrollView;
/*
*存放title的数组
*/
@property (nonatomic,copy)NSArray *dataArray;
/*
*点击或滑动后选中的位置
*/
@property (nonatomic, assign) NSInteger selectIndex;
/*
*所在的控制器
*/
@property (nonatomic,weak)UIViewController *fatherController;
@end
//
// ScrollSliderView.m
#import "ScrollSliderView.h"
#import "SubViewController.h"
#import "Cache.h" @implementation ScrollSliderView
{
Cache *cache;
}
/*
*初始化底部ScrollView
*/
-(UIScrollView *)mainScrollView{ if (!_mainScrollView) {
_mainScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(, , VIEWWIDTH, VIEWHEIGHT-)];
_mainScrollView.bounces = NO;
_mainScrollView.showsVerticalScrollIndicator = NO;
_mainScrollView.showsHorizontalScrollIndicator = NO;
_mainScrollView.pagingEnabled = YES;
_mainScrollView.backgroundColor = [UIColor lightGrayColor];
_mainScrollView.delegate = self;
} return _mainScrollView;
}
/*
*初始化self所有的控件
*/
-(id)initWithController:(UIViewController *)controller withTitleArray:(NSArray *)titleArray
{
if (self = [super initWithFrame:[UIScreen mainScreen].bounds]) {
cache = [[Cache alloc]init];
_dataArray = [NSArray arrayWithArray:titleArray];
_fatherController = controller; [self mainScrollView];
[self addSubview:_mainScrollView]; [self creatSliderScrollView:titleArray];
_mainScrollView.contentSize = CGSizeMake(titleArray.count*VIEWWIDTH, VIEWHEIGHT - - );
SubViewController *subVC = [SubViewController new];
subVC.index = ;
subVC.tilte = _dataArray[];
subVC.view.frame = CGRectMake(,, VIEWWIDTH,_mainScrollView.frame.size.height);
subVC.view.backgroundColor = [UIColor colorWithHue:(arc4random() % / 256.0) saturation:(arc4random() % / 256.0) + 0.5 brightness:(arc4random() % ) + 0.5 alpha:];
[_mainScrollView addSubview:subVC.view];
[controller addChildViewController:subVC];
[cache addCacheSelectIndex:];
}
return self; }
/*
*创建滑块
*/
- (void)creatSliderScrollView:(NSArray *)array{
_sliderScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(,, VIEWWIDTH, )];
_sliderScrollView.backgroundColor = [UIColor whiteColor];
_sliderScrollView.bounces = NO;
_sliderScrollView.showsHorizontalScrollIndicator = NO;
_sliderScrollView.showsVerticalScrollIndicator = NO;
[self addSubview:_sliderScrollView];
CGFloat spaceWidth = ;
CGFloat widthContentSize = ;
for (NSInteger i = ; i < array.count; i++) {
NSString *titleName = self.dataArray[i];
NSInteger strWidth = [self sizeforWidthWithString:titleName];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
UIView *sliderSign = [[UIView alloc]initWithFrame:CGRectMake(spaceWidth + widthContentSize,,strWidth + ,)];
sliderSign.backgroundColor = RAN_COLOR(0xE43494);
sliderSign.tag = + i;
btn.frame = CGRectMake(spaceWidth+widthContentSize,,strWidth + ,);
[btn setTitle:titleName forState:UIControlStateNormal];
[btn setTitleColor:RAN_COLOR(0xE43494) forState:UIControlStateSelected];
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
btn.titleLabel.font = [UIFont systemFontOfSize:];
[_sliderScrollView addSubview:btn];
[_sliderScrollView addSubview:sliderSign];
btn.tag = + i;
if (i == ) {
btn.selected = YES;
sliderSign.hidden = NO;
}else{ sliderSign.hidden = YES; }
widthContentSize = strWidth++spaceWidth+widthContentSize;
[btn addTarget:self action:@selector(selectIndexTableViewAndCollectionView:) forControlEvents:UIControlEventTouchUpInside];
}
self.sliderScrollView.contentSize = CGSizeMake(widthContentSize + , );
}
/*
*计算title的长度
*/
-(NSInteger)sizeforWidthWithString:(NSString *)str
{
NSDictionary *attribute = @{NSFontAttributeName: [UIFont systemFontOfSize:]};
CGSize size = [str boundingRectWithSize:CGSizeMake(VIEWWIDTH - , ) options: NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attribute context:nil].size;
return size.width;
}
/*
*点击滑块按钮响应事件
*/
- (void)selectIndexTableViewAndCollectionView:(UIButton *)sender{
//按钮点击 先把所有的按钮都不选中 滑动条View隐藏
for (NSInteger i = ; i < _dataArray.count; i++) {
UIButton *btn = (UIButton *)[self viewWithTag: + i];
UIView *sliderSign = (UIView *)[self viewWithTag: + i];
sliderSign.hidden = YES;
btn.selected = NO;
}
NSInteger index = sender.tag - ;
self.selectIndex = index;
//选中 为选中状态
sender.selected = YES;
UIView *View = (UIView *)[self viewWithTag:index + ];
View.hidden = NO; UIButton *btnRight = [_sliderScrollView viewWithTag:sender.tag + ];
UIButton *btnLeft = [_sliderScrollView viewWithTag:sender.tag - ];
//分页 显示哪个控制器
_mainScrollView.contentOffset = CGPointMake(index*VIEWWIDTH,);
if ((sender.frame.origin.x + sender.frame.size.width) > VIEWWIDTH - ){
if (sender.tag != ( + _dataArray.count - )) {
[UIView animateWithDuration:0.3f animations:^{
self.sliderScrollView.contentOffset = CGPointMake(btnRight.frame.origin.x + btnRight.frame.size.width - VIEWWIDTH, );
}];
}
}
else
{
[UIView animateWithDuration:0.3f animations:^{
self.sliderScrollView.contentOffset = CGPointMake(btnLeft.frame.origin.x, );
}];
} if (![cache hasCacheIndex:_selectIndex]) {
[self addSubController];
}
}
/*
*创建未创建的滑块对应控制器
*/
-(void)addSubController
{
SubViewController *subVC = [SubViewController new];
subVC.index = _selectIndex;
subVC.tilte = _dataArray[_selectIndex];
[_mainScrollView addSubview:subVC.view];
subVC.view.frame = CGRectMake(_selectIndex*VIEWWIDTH,, VIEWWIDTH,_mainScrollView.frame.size.height);
subVC.view.backgroundColor = [UIColor colorWithHue:(arc4random() % / 256.0) saturation:(arc4random() % / 256.0) + 0.5 brightness:(arc4random() % ) + 0.5 alpha:];
[_mainScrollView addSubview:subVC.view]; [_fatherController addChildViewController:subVC];
}
/*
*UIScrollViewDelegate,控制滑动后变化
*/
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
NSInteger index = scrollView.contentOffset.x/VIEWWIDTH;
_selectIndex = index;
for (NSInteger i = ; i < _dataArray.count; i++) {
UIButton *btn = (UIButton *)[self viewWithTag: + i];
UIView *downSliderView = (UIView *)[self viewWithTag: + i];
if (index == i) {
downSliderView.hidden = NO;
btn.selected = YES; }else{ downSliderView.hidden = YES;
btn.selected = NO;
} }
UIButton *btnRight = [_sliderScrollView viewWithTag:index + + ];
UIButton *btnCenter = [_sliderScrollView viewWithTag:index + ];
UIButton *btnLeft = [_sliderScrollView viewWithTag:index - + ];
//分页 显示哪个控制器
_mainScrollView.contentOffset = CGPointMake(index * VIEWWIDTH,);
if ((btnCenter.frame.origin.x + btnCenter.frame.size.width) > VIEWWIDTH - ){
if (btnCenter.tag != ( + _dataArray.count - )) {
[UIView animateWithDuration:0.3f animations:^{
_sliderScrollView.contentOffset = CGPointMake(btnRight.frame.origin.x + btnRight.frame.size.width - VIEWWIDTH, );
}];
}
}
else
{
[UIView animateWithDuration:0.3f animations:^{
_sliderScrollView.contentOffset = CGPointMake(btnLeft.frame.origin.x, );
}];
} if (![cache hasCacheIndex:_selectIndex]) {
[self addSubController]; }
} @end
// Cache.h #import <Foundation/Foundation.h> @interface Cache : NSObject
@property(nonatomic ,strong)NSMutableArray *cacheArray;
- (instancetype)init;
-(void)addCacheSelectIndex:(NSInteger)index;
-(BOOL)hasCacheIndex:(NSInteger)index; @end
//
// Cache.m #import "Cache.h" @implementation Cache - (instancetype)init
{
self = [super init];
if (self) {
_cacheArray = [[NSMutableArray alloc]init];
}
return self;
}
-(void)addCacheSelectIndex:(NSInteger)index
{
[_cacheArray addObject:[NSString stringWithFormat:@"%ld",index]];
} -(BOOL)hasCacheIndex:(NSInteger)index
{
BOOL hasIndex = [_cacheArray containsObject:[NSString stringWithFormat:@"%ld",index]];
if (!hasIndex) {
[self addCacheSelectIndex:index];
} return hasIndex;
} @end
//
// SubViewController.h #import <UIKit/UIKit.h> @interface SubViewController : UIViewController
@property(nonatomic,assign)NSInteger index;
@property(nonatomic,strong)NSString *tilte; @end
//
// SubViewController.m
#import "SubViewController.h"
#define VIEWWIDTH [UIScreen mainScreen].bounds.size.width
#define VIEWHEIGHT [UIScreen mainScreen].bounds.size.height
#define RAN_COLOR(CUS_RGB) [UIColor colorWithRed:((float)((CUS_RGB & 0xFF0000) >> 16))/255.0 green:((float)((CUS_RGB & 0xFF00) >> 8))/255.0 blue:((float)(CUS_RGB & 0xFF))/255.0 alpha:1.0f] @interface SubViewController () @end @implementation SubViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view. UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(, , , )];
label.layer.borderColor = [UIColor whiteColor].CGColor;
label.layer.borderWidth = ;
label.text = [NSString stringWithFormat:@"%ld:%@",_index,_tilte];
label.center = self.view.center;
label.textColor = [UIColor orangeColor];
label.font = [UIFont systemFontOfSize:];
label.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:label]; } - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end
ios-滚动导航条页面的更多相关文章
- ios 修改导航条返回按钮
ios 修改导航条返回按钮 方式一:使用系统的:可以更改系统的文字:以及通过设置导航条的颜色来达到预期的效果 UIBarButtonItem *backBtns = [[UIBarButtonItem ...
- iOS:导航条滚动透明度随着tableView的滚动而变化
来源:HelloYeah 链接:http://www.jianshu.com/p/b8b70afeda81 下面这个界面有没有觉得很眼熟.打开你手里的App仔细观察,你会发现很多都有实现这个功能.比如 ...
- Vue.js+cube-ui(Scroll组件)实现类似头条效果的横向滚动导航条
本博主在一次个人移动端项目中,遇到这么一个需求:希望自己的项目中,头部导航条的效果可以像今日头条那样,横向滚动! 对于这样的效果,在各大移动端项目中几乎是随处可见,为什么呢? 我们都知道,对于移动端也 ...
- iOS 隐藏导航条分割线
// 导航条分割线 @property (nonatomic, strong) UIView *navSeparateView; // 获取导航条分割线 UIView *backgroundView ...
- 【转】iOS隐藏导航条1px的底部横线
默认情况下会有这条线 第一种方法: 1 2 3 4 5 6 UINavigationBar *navigationBar = self.navigationController.navigationB ...
- Android Material Design:基于CoordinatorLayout实现向上滚动导航条ToolBar滚出、向下滚动导航条滚出
activity_main.xml: <android.support.design.widget.CoordinatorLayout xmlns:android="http://sc ...
- iOS隐藏导航条1px的底部横线
第二种方法:1)声明UIImageView变量,存储底部横线 @implementation MyViewController { UIImageView *navBarHairlineImageVi ...
- 【iOS开发-22】navigationBar导航条和navigationItem设置:基本搞定导航条上的文字和按钮以及各种跳转
http://blog.csdn.net/weisubao/article/details/39646739?utm_source=tuicool&utm_medium=referral (1 ...
- iOS导航条渐变透明
来源:HelloYeah 链接:http://www.jianshu.com/p/b8b70afeda81 下面这个界面有没有觉得很眼熟.打开你手里的App仔细观察,你会发现很多都有实现这个功能.比如 ...
随机推荐
- Android实现边缘凹凸的View
转载 最近做项目的时候遇到一个卡劵的效果,由于自己觉得用图片来做的话可以会出现适配效果不好,再加上自己自定义view方面的知识比较薄弱,所以想试试用自定义View来实现.但是由于自己知识点薄弱,一开始 ...
- MVC学习笔记---MVC生命周期
Asp.net应用程序管道处理用户请求时特别强调"时机",对Asp.net生命周期的了解多少直接影响我们写页面和控件的效率.因此在2007年和2008年我在这个话题上各写了一篇文章 ...
- oracle的关闭过程(各个模式关闭)
关闭数据库与实例 与数据库启动一下,关闭数据库与实例也分为3步:关闭数据库-->实例卸载数据库--->终止实例. 1.Nomal(正常关闭方式) 命令:shutdown nomal 讲解: ...
- PHP描述冒泡排序和快速排序算法
使用PHP描述冒泡排序和快速排序算法,对象可以是一个数组.使用PHP描述顺序查找和二分查找(也叫做折半查找)算法,顺序查找必须考虑效率,对象可以是一个有序数组.写一个二维数组排序算法函数,能够具有通用 ...
- 谈谈“色彩空间表示方法”——RGB、YUY2、YUYV、YVYU、UYVY、AYUV
转自:http://bbs.chinavideo.org/viewthread.php?tid=4143 还可参考http://www.fourcc.org/yuv.php 小知识:RGB与YUV-- ...
- loadrunner解决“服务器正在运行中”方法
问题现象: 这个问题在上家公司遇见过,今天无意中找到了解决办法: 解决方法: 打开任务管理器: 找到这个进程:ThumbProcess.exe,关掉这个进程即可解决. 今天运行lr的vugen报错 解 ...
- Spring的bean标签
Spring框架中主要有四种标签bean.alias.import.beans,其中bean标签是其他标签的基础. 一.bean标签的属性 scope:用来配置spring bean的作用域 sing ...
- JVM的GC实现详解
新生代中的98%对象都是“朝生夕死”的,所以并不需要按照1:1的比例来划分内存空间,而是将内存分为一块比较大的Eden空间和两块较小的Survivor空间,每次使用Eden和其中一块Survivor. ...
- LightOJ1422 Halloween Costumes(区间DP)
题目大概是依次有n场派对,每场派对都有需要穿某套衣服去参加,可以同时穿多套衣服,就是一套套着一套,如果脱了的话就不能再穿上那套了,问最少需要几套衣服去参加完所有派对. 区间DP: dp[i][j]第i ...
- LightOJ1021 Painful Bases(状压DP)
容易想到状态dp[n][S][m](S是数字出现的集合),表示前n位用了数字集S且模k余数是m的方案数. 利用 (xy)base % k = ( x*base+y ) % k = (( x%k ) * ...