效果如上,点击出现的图片是用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. GDB 调试技巧(不断更新中......)

    一.break到不同类的同名函数 方法: 在函数前面加类名以及作用域运算符 eg : break A::func //break 到类A的func函数 程序如下: //gdb_test.cpp #in ...

  2. LeetCode 22. 括号生成 C++(回溯法)

      还是用回溯法暴力解题,遍历所有可能,不过还是在此基础上进行了一些的优化,来阻止那些不必要的遍历.好,上代码. class Solution { public: vector<string&g ...

  3. hudi clustering 数据聚集(一)

    概要 数据湖的业务场景主要包括对数据库.日志.文件的分析,而管理数据湖有两点比较重要:写入的吞吐量和查询性能,这里主要说明以下问题: 1.为了获得更好的写入吞吐量,通常把数据直接写入文件中,这种情况下 ...

  4. SQL里ORDER BY 对查询的字段进行排序,字段为空不想排在最前

    在安字段排序时 空字段往往都是在最前,我只是想空字段在排序的后面,不为空的在前,这个如何修改呢 order by datatime desc   这样的句子也一样  不管是正排还是倒排  为空的都在最 ...

  5. feignclient各种使用技巧说明

    FeignClient常见用法 常规的FeignClient的创建与使用我相信只要使用过spring cloud全家桶的套件的基本上都是非常熟悉了,我们只需定义一个interface,然后定义相关的远 ...

  6. 分布式配置系统Apollo如何实时更新配置的?

    引言 记得我们那时候刚开始学习Java的时候都只是一个单体项目,项目里面的配置基本都是写在项目里面的properties文件中,比如数据库配置啥的,各种逻辑开关,一旦这些配置修改了,还需要重启项目这修 ...

  7. [atARC087F]Squirrel Migration

    对这棵树重心情况分类讨论: 1.若这棵树存在两个重心,分别记作$x$和$y$,如果将$(x,y)$断开,两棵子树大小都相同(都为$\frac{n}{2}$),此时$p_{i}$与$i$必然不同属于一个 ...

  8. 力扣 - 剑指 Offer 10- I. 斐波那契数列

    题目 剑指 Offer 10- I. 斐波那契数列 思路1(递归 / 自顶向下) 这题是很常见的一道入门递归题,可以采用自顶向下的递归方法,比如我们要求第n个位置的值,根据斐波那契数列的定义fib(n ...

  9. 【Microsoft Azure 的1024种玩法】七.Azure云端搭建部署属于自己的维基百科

    [简介] MediaWiki是全球最著名的开源wiki程序,运行于PHP+MySQL环境.MediaWiki从2002年2月25日被作为维基百科全书的系统软件,并有大量其他应用实例.MediaWiki ...

  10. 学军中学csp-noip2020模拟5

    Problem List(其实这几场全是附中出的) 这场比赛的题目相当有价值,特别是前两题,相当的巧妙. A.路径二进制 数据范围这么小,当然是搜索. \(30pts:\)大力搜索出奇迹,最后统计答案 ...