按鈕下劃線動畫移動

很多時候我們需要實現(xiàn)在點擊按鈕的時候,按鈕下方的下劃線跟著動畫移動的效果,可能剛入門的同學寫起來有點費勁,在此總結了一點想法,簡單易懂,希望可以幫助到你們,如果有更好的建議或者意見,請及時告訴我,相互學習.

- (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:@"返回"];

//創(chuàng)建四個按鈕及按鈕下劃線
[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];
}
最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,455評論 25 708
  • 發(fā)現(xiàn) 關注 消息 iOS 第三方庫、插件、知名博客總結 作者大灰狼的小綿羊哥哥關注 2017.06.26 09:4...
    肇東周閱讀 12,250評論 4 61
  • 雜貨鋪里的那個老頭還是喜歡坐在門口,聽一部破收音機里的呀呀聲,雖然,他聽不到。村里人叫他王大炮,叫了一輩子。 ...
    伯爵男主閱讀 539評論 0 1
  • 有時候你覺得沒有用的知識會在不經(jīng)意間變成你的muse。 學校時候常常問:學著有什么用啊老師。呵呵,真到用時方恨少。...
    小霽桃源閱讀 167評論 0 1
  • 目前很多項目中都會用到“刮刮樂”這個功能點,處于此整理出了一套比較easy的實現(xiàn)方法。 在這里我主要用到了Imag...
    voQuan閱讀 1,780評論 1 5