#import
@interface WPageTitleView : UIView
@property (nonatomic,assign) NSInteger selectedIndex;
//添加参数数组
@property (nonatomic,strong) NSArray *titles;
@property (nonatomic,copy) void (^buttonSelected)(NSInteger index);
@end
#import "WPageTitleView.h"
@interface WPageTitleView ()
@property (nonatomic,strong) UIView *sliderView;
@property (nonatomic,weak) UIButton *selectedButton;
@end
@implementation WPageTitleView
- (void)titleButtonClicked:(UIButton *)button {
_selectedIndex = button.tag;
if (self.selectedButton) {
self.selectedButton.selected =YES;
}
button.selected= NO;
self.selectedButton= button;
if (self.buttonSelected) {
self.buttonSelected(button.tag);
}
NSString* title = self.titles[button.tag];
CGFloat sliderWidth = button.titleLabel.font.pointSize * title.length;
[self.sliderViewmas_remakeConstraints:^(MASConstraintMaker *make) {
make.width.mas_equalTo(sliderWidth);
make.height.mas_equalTo(2);
make.centerX.equalTo(button);
make.bottom.equalTo(self).offset(-2);
}];
[UIView animateWithDuration:0.25animations:^{
[selflayoutIfNeeded];
}];
}
- (void)setSelectedIndex:(NSInteger)selectedIndex {
_selectedIndex = selectedIndex;
UIButton* button = self.subviews[selectedIndex];
[selftitleButtonClicked:button];
}
- (void)setTitles:(NSArray *)titles {
[self.subviewsmakeObjectsPerformSelector:@selector(removeFromSuperview)];
_titles = titles;
CGFloat width = [UIScreenmainScreen].bounds.size.width / titles.count;
for ( int i =0; i
UIButton* titleButton = [selftitleButton:titles[i]];
titleButton.tag = i;
[self addSubview:titleButton];
[titleButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.bottom.equalTo(self);
make.left.equalTo(self).offset(width * i);
make.width.mas_equalTo(width);
}];
if (i != 0) {
titleButton.selected= YES;
} else {
self.selectedButton = titleButton;
}
}
UIButton *button = self.subviews[0];
NSString *title = titles[0];
CGFloat sliderWidth = button.titleLabel.font.pointSize * title.length;
[selfaddSubview:self.sliderView];
[self.sliderViewmas_makeConstraints:^(MASConstraintMaker *make) {
make.width.mas_equalTo(sliderWidth);
make.height.mas_equalTo(4);
make.centerX.equalTo(button);
make.bottom.equalTo(self).offset(-3);
}];
[selflayoutIfNeeded];
}
- (UIButton *)titleButton:(NSString *)title {
UIButton* titleButton = [[UIButtonalloc] init];
[titleButton addTarget:selfaction:@selector(titleButtonClicked:)forControlEvents:UIControlEventTouchUpInside];
titleButton.titleLabel.font = [UIFontsystemFontOfSize:15];
[titleButton setTitleColor:[UIColorredColor] forState:UIControlStateNormal];
[titleButton setTitleColor:[UIColorgrayColor] forState:UIControlStateSelected];
[titleButton setTitle:titleforState:UIControlStateNormal];
return titleButton;
}
- (void)drawRect:(CGRect)rect {
[selfmakelineStartPoint:self.sliderView.bottomandEndPoint:self.sliderView.bottom];
[selfmakelineStartPoint:self.selectedButton.topandEndPoint:self.selectedButton.top];
}
- (void)makelineStartPoint:(CGFloat)statPoint andEndPoint:(CGFloat)endPoint {
CGContextRef context =UIGraphicsGetCurrentContext();
CGContextSetLineCap(context,kCGLineCapRound);
CGContextSetLineWidth(context,0.5); //线宽
CGContextSetAllowsAntialiasing(context,true);
CGContextSetStrokeColorWithColor(context, [UIColorgrayColor].CGColor);
CGContextBeginPath(context);
CGContextMoveToPoint(context, 0, statPoint); //起点坐标
CGContextAddLineToPoint(context,self.frame.size.width, endPoint); //终点坐标
CGContextStrokePath(context);
}
- (UIView *)sliderView {
if (!_sliderView) {
UIView* sliderView = [[UIViewalloc] init];
sliderView.backgroundColor = [UIColorredColor];
sliderView.layer.cornerRadius =2;
sliderView.clipsToBounds= YES;
_sliderView = sliderView;
}
return_sliderView;
}
@end
调用
WPageTitleView *titleView = [[WPageTitleViewalloc] init];
[headerView addSubview:titleView];
self.titleView = titleView;
titleView.backgroundColor= [UIColorwhiteColor];
[titleView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.mas_equalTo(headerView);
make.bottom.equalTo(headerView.mas_bottom);
make.height.mas_equalTo(NNTitleHeight);
}];
[self.scrollViewmas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.bottom.equalTo(self.view);
make.top.mas_equalTo(headerView.top);
}];
__weak typeof(self) weakSelf =self;
titleView.titles = @[@"动态", @"文章",@"更多"];
titleView.selectedIndex= 0;
titleView.buttonSelected = ^(NSInteger index){
[weakSelf.scrollView setContentOffset:CGPointMake(NNScreenWidth * index,0)animated:YES];
};