.h文件----------------------------------------------------------------------
//
//LYTitleScrollView.h
//LYHandWork
//
//Created by李瑜on 16/9/8.
//Copyright ? 2016年李瑜. All rights reserved.
//
#import <UIKIT/UIKIT.h>
/*
*協(xié)議
*/
@protocolLYTitleScrollViewDelegate
//處理點擊事件的代理方法
- (void)scrollView:(UIScrollView*)scrollView DidSelectedButton:(UIButton*)sender;
@end
@interfaceLYTitleScrollView :UIScrollView
/*
*property
*/
//代理
@property(nonatomic,weak)id buttonEventdelegate;
//該屬性決定button的個數(shù)以及上面的文字
@property(nonatomic,strong)NSArray * titles;
/*
*Methods
*/
//處理button被選中時的狀態(tài)
- (void)handleTitleButtonStateWithIndex:(NSInteger)index;
//設(shè)置title文字的顏色
- (void)setTitleColor:(UIColor*)color forState:(UIControlState)state;
@end
.m文件--------------------------------------------------------------------------------
//
//LYTitleScrollView.m
//LYHandWork
//
//Created by李瑜on 16/9/8.
//Copyright ? 2016年李瑜. All rights reserved.
//
#define SCREEN_W [UIScreen mainScreen].bounds.size.width
#define SCREEN_H [UIScreen mainScreen].bounds.size.height
#import"LYTitleScrollView.h"
@interfaceLYTitleScrollView()
//選中的按鈕
@property(nonatomic,strong)UIButton* selectedButton;
//標(biāo)題滾動條上的所有按鈕
@property(nonatomic,strong)NSMutableArray* allButtons;
@end
@implementationLYTitleScrollView
- (NSMutableArray*)allButtons{
if(_allButtons==nil) {
_allButtons= [NSMutableArrayarray];
}
return_allButtons;
}
- (instancetype)initWithFrame:(CGRect)frame{
if(self= [superinitWithFrame:frame]) {
[selfconfigure];
}
returnself;
}
//懶加載,添加按鈕到標(biāo)題滾動條上
- (void)setTitles:(NSArray *)titles{
NSInteger count = titles.count;
CGFloat width =60;
CGFloat height =self.frame.size.height;
for(inti =0; i < count; i++) {
UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(i * width,0, width, height);
[button setTitle:titles[i] forState:UIControlStateNormal];
[button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
button.titleLabel.font = [UIFont systemFontOfSize:15];
[selfaddSubview:button];
[button addTarget:selfaction:@selector(titleClick:) forControlEvents:UIControlEventTouchDown];
[self.allButtons addObject:button];
}
[selftitleClick:self.allButtons[0]];
self.contentSize = CGSizeMake(width * count,0);
}
//設(shè)置標(biāo)題文字的顏色
- (void)setTitleColor:(UIColor *)color forState:(UIControlState)state{
for(UIButton * buttoninself.allButtons) {
[button setTitleColor:color forState:state];
}
}
//配置scrollView的屬性
- (void)configure{
self.backgroundColor = [[UIColor greenColor] colorWithAlphaComponent:0.5];
self.showsHorizontalScrollIndicator =NO;
self.showsVerticalScrollIndicator =NO;
self.bounces =NO;
self.contentInset = UIEdgeInsetsMake(0,10,0,10);
}
#pragma mark -處理按鈕點擊事件
- (void)titleClick:(UIButton *)sender{
//處理被點擊的button的顏色,形變等狀態(tài)
[selfhandleTitleButtonStateWithIndex:[self.allButtons indexOfObject:sender]];
//處理點擊事件
if([self.buttonEventdelegate respondsToSelector:@selector(scrollView: DidSelectedButton:)]) {
[self.buttonEventdelegate scrollView:selfDidSelectedButton:sender];
}
}
/*
*改變titleButton的狀態(tài)
*按鈕被點擊時調(diào)用
*/
- (void)handleTitleButtonStateWithIndex:(NSInteger)index{
if(index >self.allButtons.count -1|| index <0) {
return;
}
UIButton * sender =self.allButtons[index];
if(sender ==self.selectedButton) {
NSLog(@"重復(fù)點擊");
}
//取消上一個按鈕的選中狀態(tài)
[self.selectedButton setSelected:NO];
//使當(dāng)前按鈕進入選中狀態(tài)
[sender setSelected:YES];
//點擊后放大
[UIView animateWithDuration:0.3animations:^{
self.selectedButton.transform = CGAffineTransformIdentity;
sender.transform = CGAffineTransformMakeScale(1.2,1.2);
}];
/*
*如果按鈕部分在屏幕之外,點擊按鈕滾動標(biāo)題條使選中的按鈕完全顯示
*/
CGFloat offset =self.contentOffset.x;
//超出屏幕左邊之外
if(sender.frame.origin.x - offset <0) {
[UIView animateWithDuration:0.3animations:^{
self.contentOffset = CGPointMake(sender.frame.origin.x,0);
}];
}
//超出屏幕右邊之外
if(CGRectGetMaxX(sender.frame) - offset > SCREEN_W) {
[UIView animateWithDuration:0.3animations:^{
self.contentOffset = CGPointMake(CGRectGetMaxX(sender.frame) - SCREEN_W,0);
}];
}
//記錄已經(jīng)被選中的按鈕
self.selectedButton = sender;
}
@end
注意:控制器使用此類時官套,一定要加上代碼(如果有導(dǎo)航控制器的情況下)self.navigationController.navigationBar.translucent=NO;否則button將無法顯示