按钮下划线动画移动

很多时候我们需要实现在点击按钮的时候,按钮下方的下划线跟着动画移动的效果,可能刚入门的同学写起来有点费劲,在此总结了一点想法,简单易懂,希望可以帮助到你们,如果有更好的建议或者意见,请及时告诉我,相互学习.

- (void)viewDidLoad {
 [super viewDidLoad];

self.view.backgroundColor = [UIColor whiteColor];
self.title = @"动画";
self.navigationController.navigationBar.barTintColor = [UIColor purpleColor];
self.navigationController.navigationBar.translucent = NO;
[self.navigationController.navigationBar setTitleTextAttributes:
 @{NSFontAttributeName:[UIFont systemFontOfSize:19],
   NSForegroundColorAttributeName:[UIColor redColor]}];
[self setBackBarButtonItemWithTitle:@"返回"];

//创建四个按钮及按钮下划线
[self setupBtns];

}

-(void)setupBtns {
UIButton *btn;
for (int i = 0; i < 4; i++) {
    btn = [[UIButton alloc] initWithFrame:CGRectMake(i * (SCREEN_WIDTH/4), 0, SCREEN_WIDTH/4, 40)];
    btn.tag = i;
    btn.backgroundColor = [UIColor lightGrayColor];
    [btn setTitle:[NSString stringWithFormat:@"%d",i] forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
  }

UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(btn.frame), SCREEN_WIDTH/4, 2)];
line.backgroundColor = [UIColor greenColor];
[self.view addSubview:line];
self.line = line;

}

-(void)clickBtn:(UIButton *)sender {
//获取到点击的是哪一个按钮
NSInteger index = [sender.superview.subviews indexOfObject:sender];
[UIView animateWithDuration:0.25 animations:^{//重写下划线的frame
    CGRect frame = self.line.frame;
    frame.origin.x = SCREEN_WIDTH/4 * index;
    self.line.frame = frame;
}];

if (sender.tag == 0) {
    NSLog(@"第一个");
}else if (sender.tag == 1) {
    NSLog(@"第二个");
}else if (sender.tag == 2) {
    NSLog(@"第三个");
}else if (sender.tag == 3) {
    NSLog(@"第四个");
}
}

/**
 *  设置返回按钮标题
 */
- (void)setBackBarButtonItemWithTitle:(NSString *)title
{
UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 60, 30)];
[btn setTitle:title forState:UIControlStateNormal];
[btn setBackgroundColor:[UIColor clearColor]];
[btn setTitleColor:COLOR_MY_WHITE forState:UIControlStateNormal];
[btn setImage:[UIImage imageNamed:@"返回"] forState:UIControlStateNormal];
[btn setContentMode:UIViewContentModeScaleAspectFill];
[btn setImageEdgeInsets:UIEdgeInsetsMake(0, -7, 0, 50)];
[btn setTitleEdgeInsets:UIEdgeInsetsMake(0, -5, 0, 10)];
[btn addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
self.navigationItem.leftBarButtonItem = barButtonItem;
}

/**
 *  返回按钮点击事件
 */
- (void)backAction:(id)sender
{
[self.navigationController popViewControllerAnimated:YES];
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,287评论 25 708
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,241评论 4 61
  • 杂货铺里的那个老头还是喜欢坐在门口,听一部破收音机里的呀呀声,虽然,他听不到。村里人叫他王大炮,叫了一辈子。 ...
    伯爵男主阅读 538评论 0 1
  • 有时候你觉得没有用的知识会在不经意间变成你的muse。 学校时候常常问:学着有什么用啊老师。呵呵,真到用时方恨少。...
    小霁桃源阅读 167评论 0 1
  • 目前很多项目中都会用到“刮刮乐”这个功能点,处于此整理出了一套比较easy的实现方法。 在这里我主要用到了Imag...
    voQuan阅读 1,775评论 1 5