文章目录

  • 效果图
  • 自定义的View
  • button被点击后执行点击事件,利用selectedIndex的set方法改变下划线位置
  • 使用及button的自定义点击事件添加
  • 参考文献

效果图


自定义的View

在自定义View里写好button及下划线,使用时直接调用View即可

#define UNDERLINETAG 2000
#define UNDERLINEDISTANCE 10
#define UNDERLINEHEIGHT 2#import <UIKit/UIKit.h>NS_ASSUME_NONNULL_BEGIN@interface ButtonView : UIView@property (nonatomic, strong) NSArray *items;
@property (nonatomic, strong) NSMutableArray *buttonsArray;
@property (nonatomic) NSInteger selectedIndex;- (instancetype) initWithItems:(NSArray *) items;@endNS_ASSUME_NONNULL_END- (instancetype)initWithItems:(NSArray *)items {self = [super init];if (self) {self.items = items;self.buttonsArray = [[NSMutableArray alloc] init];//初始化buttonint i = 0;for (NSString *titleStr in _items) {UIButton *button = [[UIButton alloc] init];[_buttonsArray addObject:button];button.tag = 1000 + i;[button setTitle:titleStr forState:UIControlStateNormal];[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];[button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];[self addSubview:button];i++;}//初始化下划线UIView *underLine = [[UIView alloc] init];underLine.backgroundColor = [UIColor blackColor];underLine.tag = UNDERLINETAG;underLine.layer.cornerRadius = 5;[self addSubview:underLine];//设置最初显示在第一个buttonself.selectedIndex = 0;[self selectButtonWithIndex:0];}return self;
}- (void)layoutSubviews {[super layoutSubviews];CGFloat width = CGRectGetWidth(self.frame) / _items.count; //每个button平均宽度CGFloat height = CGRectGetHeight(self.frame); //给定高度for (int i = 0; i < _items.count; i++) {UIButton *button = (UIButton *)[self viewWithTag:1000 + i];if (button != nil) {button.frame = CGRectMake(i * width, 0, width, height);}}UIView *underLine = [self viewWithTag:UNDERLINETAG];CGFloat underLineW = width - 2 * UNDERLINEDISTANCE;if (underLine != nil) {underLine.frame = CGRectMake(self.selectedIndex * underLineW + UNDERLINEDISTANCE, height - UNDERLINEHEIGHT, underLineW, UNDERLINEHEIGHT);}
}

button被点击后执行点击事件,利用selectedIndex的set方法改变下划线位置

- (void)buttonAction:(UIButton *)button {NSInteger index = button.tag - 1000;if (index == self.selectedIndex)return;self.selectedIndex = index;
}//调用selectedIndex的setter方法
- (void)setSelectedIndex:(NSInteger)selectedIndex {if (_selectedIndex == selectedIndex)return;_selectedIndex = selectedIndex;[self selectButtonWithIndex:selectedIndex];
}- (void)selectButtonWithIndex:(NSInteger)index {CGFloat width = CGRectGetWidth(self.frame) / _items.count;CGFloat height = CGRectGetHeight(self.frame);CGFloat underLineW = width - 2 * UNDERLINEDISTANCE;[UIView animateWithDuration:.3 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{UIView *underLine = [self viewWithTag:UNDERLINETAG];if (underLine != nil) {underLine.frame = CGRectMake(index * width + UNDERLINEDISTANCE, height - UNDERLINEHEIGHT, underLineW, UNDERLINEHEIGHT);}} completion:^(BOOL finished) {}];
}

使用及button的自定义点击事件添加

这里我只想到了用数组将button存起来然后遍历button设置点击事件

    ButtonView *view = [[ButtonView alloc] initWithItems:@[@"嘿嘿嘿", @"哈哈哈", @"啦啦啦"]];view.frame = CGRectMake(0, 100, self.view.bounds.size.width, 50);for (UIButton *button in view.buttonsArray) {if (button.tag == 1001) {[button addTarget:self action:@selector(clickOne) forControlEvents:UIControlEventTouchUpInside];} else if (button.tag == 1002) {[button addTarget:self action:@selector(clickTwo) forControlEvents:UIControlEventTouchUpInside];} else {;}}[self.view addSubview:view];

参考文献

[iOS 控件]按钮下划线随点击事件移动

iOS 按钮加下划线随之移动相关推荐

  1. ios 按钮文字下划线_iOS 文字下划线

    设置UITextView,UILabel 中的文字加下划线 //添加下划线 -(NSAttributedString*) getAttributedString:(NSAttributedString ...

  2. ios 按钮文字下划线_iOS - UIButton设置文字标题下划线以及下划线颜色

    创建button设置可以折行显示 - (void)viewDidLoad { [super viewDidLoad]; UIButton * button = [[UIButton alloc] in ...

  3. ios 按钮文字下划线_iOS实现一段文字中部分有下划线,并且可以点击

    项目中有一个需求就是实现一段文字中有几个特殊的字符可以有下划线,并且可以进行点击. 首先可以实现下划线效果,首先想到的是UILabel和UITextView控件的 NSMutableAttribute ...

  4. ios 按钮下面加下划线_IOS UIlabel 、UIButton添加下划线

    1.给UILabel 添加下划线 UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 100, 300, 100)]; labe ...

  5. 我可以在Android版式中加下划线吗?

    如何在Android布局xml文件中定义带下划线的文本? #1楼 Strings.xml文件内容: <resource><string name="my_text" ...

  6. C/C++ 为什么#ifndef 头文件 要用大写加下划线?(这就是一种约定俗成的规范)

    如图,防止头文件重复包含,为什么要用大写加下划线?为什么不是直接写peripherals.h,我已经在问了 解释: 首先,test.h不是合法的C标识名:合法的C标识名只能由字母.数字和下划线组成,且 ...

  7. 把骆驼命名法的变量,变为大写字母变小写且之前加下划线

    /** * 把骆驼命名法的变量,变为大写字母变小写且之前加下划线 * * @param str * @return */ public static String toUnderline(String ...

  8. HTML怎么设置下划线?html文字加下划线方法

    HTML中的下划线曾经是将文本包含在<u></u>标签中的问题,但是这种方法已被放弃,而更倾向于使用更多功能的CSS.一般来说,下划线被认为是引起人们对文本注意的一种方式,那么 ...

  9. JavaScript中方法或者变量名称前加下划线的是什么意思?

    JavaScript中的方法或者变量名称前面有下划线,是做什么的? 加下划线不是js要求的,是程序员约定俗成的. 通常变量前加下划线表示"私有变量". 函数名前加下划线表示&quo ...

最新文章

  1. vivox50pro鸿蒙系统,vivo X50 Pro最适合用来拍风景,看看网友的作品就知道了
  2. Power Designer反向获取数据库物理模型时Unable to list the users.
  3. SSM框架的简单搭建
  4. lg手机历史机型_华为后,又一中国芯崛起!国际手机巨头LG都采用它的芯片了...
  5. 理解CSRF(跨站请求伪造)
  6. SQL 通配符 、分页 功能的学习
  7. [软件工程] 面向对象方法学引论
  8. [写轮眼]QQ服务调校
  9. 怎样查找计算机的ip mac地址,如何通过mac地址查ip,教您Mac怎么查看ip地址
  10. 安装 virtualbox additions 备忘 转
  11. MyBatisCodeHelper-Pro插件
  12. 无法访问eclipse官网?镜像源可以帮你
  13. suse账号解锁_Linux多次登录失败用户被锁定使用Pam_Tally2解锁
  14. 基于JSP的IQ智力测试系统
  15. 什么是Crunchyroll,它提供什么动漫?
  16. SpringBoot-二维码工具类
  17. ListView滚动的方法
  18. Python 的异步 IO:Aiohttp Client 代码分析
  19. iOS'Dev的2018年个人总结 | 掘金年度征文
  20. 基于STM32的AD7699、(AD7689、AD7949、AD7682)芯片驱动

热门文章

  1. 从硬件到纯软件,回顾一个老工程师的经历之路!
  2. GOTS认证咨询,GOTS验厂又能为企业带来什么样的意义
  3. catia 界面语言设置中文和英文切换
  4. IntelliJ IDEA 代码字体大小的快捷键设置放大缩小(很实用)(图文详解)
  5. 微信公众号、小程序使用注意事项
  6. R语言地理空间分析、可视化及模型预测
  7. 阿里云网盘内测_超大杯福利,阿里云网盘内测资格+20年资源积累神站
  8. 南京师范大学本科毕业论文答辩PPT模板
  9. 安装配置远程jupyter notebook(原ipython notebook)
  10. VMware安装CentOS7超详细版