.h文件----------------------------------------------------------------------
//
//LYTitleScrollView.h
//LYHandWork
//
//Created by李瑜on 16/9/8.
//Copyright © 2016年李瑜. All rights reserved.
//
#import <UIKIT/UIKIT.h>
/*
*协议
*/
@protocolLYTitleScrollViewDelegate
//处理点击事件的代理方法
- (void)scrollView:(UIScrollView*)scrollView DidSelectedButton:(UIButton*)sender;
@end
@interfaceLYTitleScrollView :UIScrollView
/*
*property
*/
//代理
@property(nonatomic,weak)id buttonEventdelegate;
//该属性决定button的个数以及上面的文字
@property(nonatomic,strong)NSArray * titles;
/*
*Methods
*/
//处理button被选中时的状态
- (void)handleTitleButtonStateWithIndex:(NSInteger)index;
//设置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;
//标题滚动条上的所有按钮
@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;
}
//懒加载,添加按钮到标题滚动条上
- (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);
}
//设置标题文字的颜色
- (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的颜色,形变等状态
[selfhandleTitleButtonStateWithIndex:[self.allButtons indexOfObject:sender]];
//处理点击事件
if([self.buttonEventdelegate respondsToSelector:@selector(scrollView: DidSelectedButton:)]) {
[self.buttonEventdelegate scrollView:selfDidSelectedButton:sender];
}
}
/*
*改变titleButton的状态
*按钮被点击时调用
*/
- (void)handleTitleButtonStateWithIndex:(NSInteger)index{
if(index >self.allButtons.count -1|| index <0) {
return;
}
UIButton * sender =self.allButtons[index];
if(sender ==self.selectedButton) {
NSLog(@"重复点击");
}
//取消上一个按钮的选中状态
[self.selectedButton setSelected:NO];
//使当前按钮进入选中状态
[sender setSelected:YES];
//点击后放大
[UIView animateWithDuration:0.3animations:^{
self.selectedButton.transform = CGAffineTransformIdentity;
sender.transform = CGAffineTransformMakeScale(1.2,1.2);
}];
/*
*如果按钮部分在屏幕之外,点击按钮滚动标题条使选中的按钮完全显示
*/
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);
}];
}
//记录已经被选中的按钮
self.selectedButton = sender;
}
@end
注意:控制器使用此类时,一定要加上代码(如果有导航控制器的情况下)self.navigationController.navigationBar.translucent=NO;否则button将无法显示