效果如上,点击出现的图片是用UIImageview添加上的,比较简陋

我用了两种方法,第一种是直接在viewcontroller里面写代码

第二种是用了一个类来封装这个scrollviewbar 对外开放接口方法

下面就把我比较满意的第二种方法放上来

----------------------------------------假装分割线----------------

我首先新建了一个类 类名如下

对外可以使用该类的类方法,直接得到一个ScrollViewBar类型的对象

在使用一个对象方法来设置scrollviewbar上要表示的标题

 1 #import <UIKit/UIKit.h>
2
3 @interface ScrollViewBar : UIScrollView
4
5 @property (nonatomic , strong)NSArray *nameArr;
6
7 //-(void)setNameArr:(NSArray *)nameArr;
8
9
10 +(ScrollViewBar *)showScrollviewWithFrame :(CGRect)frame andContainerView :(UIView *)father andNameCount:(NSInteger)count;
11
12 -(void)addBtnsNameArr;
13 @end

以下是类方法的实现

 1 +(ScrollViewBar *)showScrollviewWithFrame :(CGRect)frame andContainerView :(UIView *)father andNameCount:(NSInteger)count{
2 ScrollViewBar *scrViewBar = [[ScrollViewBar alloc]initWithFrame:frame];
3 [scrViewBar setBackgroundColor:[UIColor whiteColor]];
4 scrViewBar.contentSize = CGSizeMake(20+90*count, frame.size.height);
5 scrViewBar.showsHorizontalScrollIndicator = false;
6 scrViewBar.bounces = NO;
7 [father addSubview:scrViewBar];
8
9
10 return scrViewBar;
11 }

对象方法的实现

 1 -(void)addBtnsNameArr{
2
3 self.btnArr = [NSMutableArray array];
4 for (int i = 0; i < self.nameArr.count; i++) {
5 UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
6 [self addSubview:btn];
7 //设置frame
8 btn.frame = CGRectMake(10+90*i, 5, 80, 50);
9 //加tag值
10 btn.tag = i + 1;
11 //设置标题 字体大小 颜色 事件
12 [btn setTitle:[self.nameArr objectAtIndex:i] forState:UIControlStateNormal];
13 [self setBtnEnabled:btn andValue:YES];
14 [btn addTarget:self action:@selector(btnDidClicked:) forControlEvents:UIControlEventTouchUpInside];
15
16 //把btn放进数组里面去
17 [_btnArr addObject:btn];
18 }
19 UIButton * firBtn = [_btnArr objectAtIndex:0];
20 [self setBtnEnabled:firBtn andValue:NO];
21 //添加视图
22 [self initUIImageView];
23 }

给scrollviewbar上的uibutton 加点击方法

 1 -(void)btnDidClicked:(UIButton *)btn{
2 if (btn.isEnabled == NO) {//说明已经被点击了,那么就不能再点击,
3
4 }else{//还没被点 点击就会变换字的大小
5 //先把已经点击过的字体变回来
6
7 for (UIButton *clickedBtn in _btnArr) {
8 if (clickedBtn.isEnabled == NO) {
9 [self setBtnEnabled:clickedBtn andValue:YES];
10 }
11 }
12 [self setBtnEnabled:btn andValue:NO];
13
14 //改变位置
15 if (btn.tag > 2 && btn.tag < self.nameArr.count - 1) {//从第三个开始滑动 到倒数第二个
16 [self setContentOffset:CGPointMake(btn.frame.origin.x+40-self.frame.size.width/2, 0) animated:YES];
17 }else{
18 if (btn.tag < 3) {
19 [self setContentOffset:CGPointMake(0, 0) animated:YES];
20 }else{
21 [self setContentOffset:CGPointMake(640-self.frame.size.width, 0) animated:YES];
22
23 }
24 }
25 //imgview的hidden值为yes
26
27 //把其他视图的hidden值设为yes
28
29 for (UIImageView *imgv in _imgViewArr) {
30 imgv.hidden = YES;
31 }
32 UIImageView *imgview = [_imgViewArr objectAtIndex:btn.tag-1];
33 imgview.hidden = NO;
34
35
36 }
37 }

下面这个方法是用来加载点击button显示的图片imageview ,只是做一个大概的样子。

(这里可以改进,我使用了imageview的hidden属性,按道理是应该用懒加载,不过我还不怎么会,等我会了以后再写一下用懒加载的方法)

 1 //添加视图
2 -(void)initUIImageView{
3 self.imgViewArr = [NSMutableArray array];
4 self.imgArr = [NSArray arrayWithObjects:@"777",@"111", @"222",@"333",@"444",@"555",@"666",nil];
5 for (int i = 0; i < self.nameArr.count; i++) {
6 UIImageView *imgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:[_imgArr objectAtIndex:i]]];
7 imgView.frame = CGRectMake(0, self.frame.origin.y +self.frame.size.height+5, self.frame.size.width, self.superview.frame.size.height - self.frame.origin.y - self.frame.size.height);
8 imgView.hidden = YES;
9 [self.superview addSubview:imgView];
10 [_imgViewArr addObject:imgView];
11 }
12 UIImageView *firImgview = [_imgViewArr objectAtIndex:0];
13 firImgview.hidden = NO;
14
15 }

最后是重写enabled的set方法

 1 //重写enabled的set方法
2 -(void)setBtnEnabled :(UIButton *)btn andValue :(BOOL)enabledV{
3 if (enabledV == YES) {//要把btn的enabled的值设置为yes 就是让btn的格式变成蓝色的
4 btn.titleLabel.font = [UIFont systemFontOfSize:18];
5 [btn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal] ;
6 btn.enabled = YES;
7 }else{
8 btn.titleLabel.font = [UIFont systemFontOfSize:24];
9 [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal] ;
10 btn.enabled = NO;
11 }
12 }

(在这个程序中,我是通过设置uibutton的enabled属性来判断button是否被点击过了,也可以用selected属性 那样就是重写selected的set方法)

全部实现代码如下:

#import "ScrollViewBar.h"
@interface ScrollViewBar ()
@property (nonatomic , strong)NSMutableArray *btnArr;
@property (nonatomic , strong)NSArray *imgArr;
@property (nonatomic , strong)NSMutableArray *imgViewArr;
@end
@implementation ScrollViewBar
+(ScrollViewBar *)showScrollviewWithFrame :(CGRect)frame andContainerView :(UIView *)father andNameCount:(NSInteger)count{
ScrollViewBar *scrViewBar = [[ScrollViewBar alloc]initWithFrame:frame];
[scrViewBar setBackgroundColor:[UIColor whiteColor]];
scrViewBar.contentSize = CGSizeMake(20+90*count, frame.size.height);
scrViewBar.showsHorizontalScrollIndicator = false;
scrViewBar.bounces = NO;
[father addSubview:scrViewBar]; return scrViewBar;
} -(void)addBtnsNameArr{ self.btnArr = [NSMutableArray array];
for (int i = 0; i < self.nameArr.count; i++) {
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[self addSubview:btn];
//设置frame
btn.frame = CGRectMake(10+90*i, 5, 80, 50);
//加tag值
btn.tag = i + 1;
//设置标题 字体大小 颜色 事件
[btn setTitle:[self.nameArr objectAtIndex:i] forState:UIControlStateNormal];
[self setBtnEnabled:btn andValue:YES];
[btn addTarget:self action:@selector(btnDidClicked:) forControlEvents:UIControlEventTouchUpInside]; //把btn放进数组里面去
[_btnArr addObject:btn];
}
UIButton * firBtn = [_btnArr objectAtIndex:0];
[self setBtnEnabled:firBtn andValue:NO];
//添加视图
[self initUIImageView];
} //添加视图
-(void)initUIImageView{
self.imgViewArr = [NSMutableArray array];
self.imgArr = [NSArray arrayWithObjects:@"777",@"111", @"222",@"333",@"444",@"555",@"666",nil];
for (int i = 0; i < self.nameArr.count; i++) {
UIImageView *imgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:[_imgArr objectAtIndex:i]]];
imgView.frame = CGRectMake(0, self.frame.origin.y +self.frame.size.height+5, self.frame.size.width, self.superview.frame.size.height - self.frame.origin.y - self.frame.size.height);
imgView.hidden = YES;
[self.superview addSubview:imgView];
[_imgViewArr addObject:imgView];
}
UIImageView *firImgview = [_imgViewArr objectAtIndex:0];
firImgview.hidden = NO; } -(void)btnDidClicked:(UIButton *)btn{
if (btn.isEnabled == NO) {//说明已经被点击了,那么就不能再点击, }else{//还没被点 点击就会变换字的大小
//先把已经点击过的字体变回来 for (UIButton *clickedBtn in _btnArr) {
if (clickedBtn.isEnabled == NO) {
[self setBtnEnabled:clickedBtn andValue:YES];
}
}
[self setBtnEnabled:btn andValue:NO]; //改变位置
if (btn.tag > 2 && btn.tag < self.nameArr.count - 1) {//从第三个开始滑动 到倒数第二个
[self setContentOffset:CGPointMake(btn.frame.origin.x+40-self.frame.size.width/2, 0) animated:YES];
}else{
if (btn.tag < 3) {
[self setContentOffset:CGPointMake(0, 0) animated:YES];
}else{
[self setContentOffset:CGPointMake(640-self.frame.size.width, 0) animated:YES]; }
}
//imgview的hidden值为yes //把其他视图的hidden值设为yes for (UIImageView *imgv in _imgViewArr) {
imgv.hidden = YES;
}
UIImageView *imgview = [_imgViewArr objectAtIndex:btn.tag-1];
imgview.hidden = NO; }
} //重写enabled的set方法
-(void)setBtnEnabled :(UIButton *)btn andValue :(BOOL)enabledV{
if (enabledV == YES) {//要把btn的enabled的值设置为yes 就是让btn的格式变成蓝色的
btn.titleLabel.font = [UIFont systemFontOfSize:18];
[btn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal] ;
btn.enabled = YES;
}else{
btn.titleLabel.font = [UIFont systemFontOfSize:24];
[btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal] ;
btn.enabled = NO;
}
} @end

在viewcontroller里面的配置

 1 #import "ViewController.h"
2 #import "ScrollViewBar.h"
3 @interface ViewController ()
4
5 @property(nonatomic ,strong)ScrollViewBar *scrViewBar;
6
7 @end
8
9 @implementation ViewController
10
11 - (void)viewDidLoad {
12 [super viewDidLoad];
13
14
15
16 self.scrViewBar = [ScrollViewBar showScrollviewWithFrame:CGRectMake(0, 100, self.view.frame.size.width, 60) andContainerView:self.view andNameCount:7];
17 [self.view setBackgroundColor:[UIColor grayColor]];
18
19 NSArray * nameArray = [[NSArray alloc]initWithObjects:@"全部",@"视频",@"声音",@"图片",@"段子",@"军事",@"科技", nil];
20 _scrViewBar.nameArr = nameArray;
21 [_scrViewBar addBtnsNameArr];
22
23 }
24
25 @end

在写这个程序过程中产生的疑惑:

1.一开始是想只写一个类方法,不过后来发现要在这一个类方法里面使用对象方法,就是不行的,所以又添加了一个对外的对象方法。

2.数组一定要初始化

用UIScrollview做一个网易scrollviewbar的更多相关文章

  1. 泥瓦匠想做一个与众不同的技术"匠"

    点击蓝字,关注泥瓦匠 本文阅读大约 3 分钟.感谢阅读 喝了最后一口百事可乐,想到它的 slogan:新一代的选择.新一代的选择,每个人选择不同,人生道路历程也不同.就像我刚毕业的时候,毕业选择不一样 ...

  2. 强哥的分享--如何使用Spring Boot做一个邮件系统

    http://springboot.fun/ actuator是单机.集群环境下要使用Spring Boot Admin将各个单机的actuator集成越来 mvn clean package -Dm ...

  3. 怎样做一个iOS App的启动分层引导动画?

    一. 为什么要写这篇文章? 这是一个很古老的话题,从两年前新浪微博开始使用多层动画制作iOS App的启动引导页让人眼前一亮(当然,微博是不是历史第一个这个问题值得商榷)之后,各种类型的引导页层出不穷 ...

  4. 【技巧】使用weeman来做一个钓鱼网页

    本文来自网友836834283 对玄魂工作室的投稿. 工具项目地址:https://github.com/Hypsurus/weeman/ 克隆地址:https://github.com/Hypsur ...

  5. 用struts2标签如何从数据库获取数据并在查询页面显示。最近做一个小项目,需要用到struts2标签从数据库查询数据,并且用迭代器iterator标签在查询页面显示,可是一开始,怎么也获取不到数据,想了许久,最后发现,是自己少定义了一个变量,也就是var变量。

    最近做一个小项目,需要用到struts2标签从数据库查询数据,并且用迭代器iterator标签在查询页面显示,可是一开始,怎么也获取不到数据,想了许久,最后发现,是自己少定义了一个变量,也就是var变 ...

  6. 基于trie树做一个ac自动机

    基于trie树做一个ac自动机 #!/usr/bin/python # -*- coding: utf-8 -*- class Node: def __init__(self): self.value ...

  7. 做一个 App 前需要考虑的几件事

    做一个 App 前需要考虑的几件事  来源:limboy的博客   随着工具链的完善,语言的升级以及各种优质教程的涌现,做一个 App 的成本也越来越低了.尽管如此,有些事情最好前期就做起来,避免当 ...

  8. 有了lisk,为什么我们还要做一个Asch?

    0 前言 首先要声明一点,我们和我们的一些朋友都是lisk的投资人和支持者,我们也相信lisk会成功. 事实上,lisk已经成功了一半,目前在区块链领域融资金额排行第二,仅次于以太坊. 那为什么我们还 ...

  9. 做一个阅读管理APP

    背景 由于最近在看的书有点多,所以一直想找一个能够管理阅读进度的书(鄙人记性不是很好,两天不看就忘了)可惜Android平台上一直找不到合适的APP: 有没有读书进度管理的网站或软件啊? 有没有记录读 ...

随机推荐

  1. redhat 7.x 的防火墙软件firewall 介绍

    zone 的概念.firewall 一般有9个zone ,配置文件都在 /usr/lib/firewalld/zones/ 里面. 系统的配置文件目录就在 /usr/lib/firewalld 这个目 ...

  2. javac 不是内部或外部命令 和 错误 找不到或无法加载主类 的解决方法

    使用package语句与import语句. 实验要求:按实验要求使用package语句,并用import语句使用Java平台提供的包中的类以及自定义包中的类.掌握一些重要的操作步骤. 代码: 模板1: ...

  3. 大数据SQL中的Join谓词下推,真的那么难懂?

    听到谓词下推这个词,是不是觉得很高大上,找点资料看了半天才能搞懂概念和思想,借这个机会好好学习一下吧. 引用范欣欣大佬的博客中写道,以前经常满大街听到谓词下推,然而对谓词下推却总感觉懵懵懂懂,并不明白 ...

  4. VLAN实验

    VLAN实验 如图所示:图中共有四个广播域,左边逻辑的分为两个广播域,右边也是逻辑的分为两个广播域, 配置顺序先配置交换机,在配置路由器 SW1 配置: 1.首先创建vlan [sw1]vlan ba ...

  5. MySQL基础语句(修改)

    ①INSERT INSERT INTO students (class_id, name, gender, score) VALUES (2, '大牛', 'M', 80); 向students表插入 ...

  6. Python多版本共存的方法

    目录 Python2.Python3共存的方法 python2下载及环境变量配置 第一步.打开Python官网,下载Python2 第二步.python2环境变量配置 测试结果 Python2.Pyt ...

  7. [atAGC054F]Decrement

    令$a_{i}$和$b_{i}$分别为$A_{i}$和$B_{i}$减少的值,考虑判定$\{a_{i}\},\{b_{i}\}$能否被得到 结论:$\{a_{i}\},\{b_{i}\}$能否被得到当 ...

  8. [luogu6838]网络站点

    先分析答案,即$x$和$y$的关系有以下两种: 1.$y$在$x$子树中,那么答案即为包含$y$的$x$的儿子 2.$y$不在$x$子树中,那么答案即为$x$的父亲 那么第一个问题就是判断$y$是否在 ...

  9. [bzoj1635]最高的牛

    初始如果没有限制,很显然每一头牛高度都是h当只有一个限制,让h[a]到h[b]的高度都减1即可容易发现两个限制不会相交(否则必然矛盾),只会包含或相离,因此没有影响,直接差分/线段树即可(注意:1.不 ...

  10. 记一次 android 线上 oom 问题

    背景 公司的主打产品是一款跨平台的 App,我的部门负责为它提供底层的 sdk 用于数据传输,我负责的是 Adnroid 端的 sdk 开发. sdk 并不直接加载在 App 主进程,而是隔离在一个单 ...