- (void)setTitlesView { UIView *titleView = [[UIView alloc] init]; titleView.backgroundColor = SLQRGBColor(241, 241, 241); titleView.frame = CGRectMake(0, 64, self.view.width, 44); NSArray *title = @[@"全部",@"视频",@"声音",@"图片",@"段子"]; CGFloat x = 0; CGFloat width = titleView.width / title.count; for (NSInteger i = 0 ; i < 5; i ++) { UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; x = i * width; btn.frame = CGRectMake(x, 0, width, titleView.height); [btn setTitle:title[i] forState:UIControlStateNormal]; [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal]; [btn addTarget:self action:@selector(titleClick:) forControlEvents:UIControlEventTouchUpInside]; [titleView addSubview:btn]; }
UIView *indicatorView = [[UIView alloc] init]; indicatorView.backgroundColor = [UIColor redColor]; indicatorView.height = 2; indicatorView.y = titleView.height - indicatorView.height;
[titleView addSubview:indicatorView];
self.indicatorView = indicatorView; [self.view addSubview:titleView]; }
- (void)titleClick:(UIButton *)btn { [UIView animateWithDuration:0.1 animations:^{ self.indicatorView.width = btn.titleLabel.width; self.indicatorView.centerX = btn.centerX; }]; }
|