系统的segment控件太封闭,想换个颜色加个背景太难了,忍不住自己写一个,以备不时之需
这个控件给出了很多自定义属性的设置,用起来还是比较方便的,需要注意的 itemWidth如果不设置,则会按照控件的宽度平均分配每一项的宽度,如果设置了,那么总宽度超过控件宽度后会有滑动效果
直接上代码吧:
头文件:
#import<Foundation/Foundation.h>@PRotocolWCSegmentControlDelegate-(void)wcSegmentControlSelectionChanged:(id)sender;@end@interfaceWCSegmentControl : UIView
@property (nonatomic, strong)id<WCSegmentControlDelegate>delegate;
@property (nonatomic, strong) NSMutableArray*dataSourceOFTitle;
@property (nonatomic, assign, setter=setCurrentSelectedIndex:)intcurrentSelectedIndex;//title color@property (nonatomic, strong) UIColor *titleColor;
@property (nonatomic, strong) UIColor*selectedTitleColor;//font@property (nonatomic, strong) UIFont *titleFont;//item selectedBackground Color;@property (nonatomic, strong) UIColor *itemBackgroundColor;
@property (nonatomic, strong) UIColor*selectedItemBackgroundColor;//item selectedBackground image;@property (nonatomic, strong) UIImage *itemBackgroundImage;
@property (nonatomic, strong) UIImage*selectedItemBackgroundImage;//item border@property (nonatomic, strong) UIColor *itemBorderColor;
@property (nonatomic, assign) BOOL isShowItemBorderWhenHilight;
@property (nonatomic, assign)intitemBorderWidth;
@property (nonatomic, assign)intitemCornerRadius;//item, 不设置则均分控件宽度@property (nonatomic, assign)intitemWidth;//control border@property (nonatomic, strong) UIColor *borderColor;
@property (nonatomic, assign) BOOL isShowBorder;
@property (nonatomic, assign)intborderWidth;
@property (nonatomic, assign)intcornerRadius;//分割线@property (nonatomic, strong) UIColor *splitColor;
@property (nonatomic, assign)intsplitBorderWidth;
@property (nonatomic, assign) BOOL isShowSplitBorder;@end
实现文件:
#import"WCSegmentControl.h"#import"WCSegmentControlItemButton.h"@implementationWCSegmentControl {
UIScrollView*_scrollView;
}- (id)initWithFrame:(CGRect)frame {
self=[super initWithFrame:frame];if(self) {
self.clipsToBounds=YES;
_scrollView=[[UIScrollView alloc] initWithFrame:self.bounds];
_scrollView.backgroundColor=self.backgroundColor;
[self addSubview:_scrollView];
_dataSourceOFTitle=[NSMutableArray array];
_selectedTitleColor=[UIColor whiteColor];
_titleColor=[UIColor blackColor];
_itemBackgroundColor=[UIColor whiteColor];
_selectedItemBackgroundColor=kWCColor7;
_borderWidth=1;
_borderColor=kWCColor7;
_cornerRadius=5;
_itemBorderColor=kWCColor7;
_itemBorderWidth=1;
_itemCornerRadius=0;
_titleFont= [UIFont systemFontOfSize:12];
_splitColor=kWCColor7;
_splitBorderWidth=1;
_isShowBorder=YES;
_isShowItemBorderWhenHilight=NO;
_isShowSplitBorder=YES;
}returnself;
}- (void)setDataSourceOFTitle:(NSMutableArray *)dataSourceOFTitle {
_dataSourceOFTitle=dataSourceOFTitle;
[self reloadData];
}-(WCSegmentControlItemButton *)createItemControlWithTitle:(NSString *)title {
WCSegmentControlItemButton* btn =[[WCSegmentControlItemButton alloc] init];if(_itemBackgroundImage)
{
[btn setBackgroundImage:_itemBackgroundImage forState:UIControlStateNormal];
}if(_selectedItemBackgroundImage)
{
[btn setBackgroundImage:_selectedItemBackgroundImage forState:UIControlStateSelected];
}
[btn setBackgroundColor:_itemBackgroundColor];
[btn setTitleColor:_selectedTitleColor forState:UIControlStateSelected];
[btn setTitleColor:_titleColor forState:UIControlStateNormal];
btn.titleLabel.font=_titleFont;
[btn setTitle:title forState:UIControlStateNormal];
btn.layer.cornerRadius=_itemCornerRadius;returnbtn;
}- (void)refreshUI {if(_isShowBorder) {
self.layer.borderWidth=_borderWidth;
self.layer.borderColor=_borderColor.CGColor;
self.layer.cornerRadius=_cornerRadius;
}else{
self.layer.borderWidth=0;
}
}-(void) reloadData
{
[_scrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];if([_dataSourceOFTitle count] >0) {//UIEdgeInsetsCGRect fra =CGRectMake(0,0,
_itemWidth>0? _itemWidth : self.width /[_dataSourceOFTitle count],
self.height);
CGFloat leftMargin= MAX(0, (self.width -fra.size.width* [self.dataSourceOFTitle count])/2);
__block CGFloat contentWidth=leftMargin;
[self.dataSourceOFTitle enumerateObjectsUsingBlock:^(NSString *title, NSUInteger idx, BOOL *stop) {
WCSegmentControlItemButton*btn =[self createItemControlWithTitle:title];
btn.frame=fra;
btn.left=leftMargin + idx *btn.width;
[btn addTarget:self action:@selector(btnTapped:) forControlEvents:UIControlEventTouchUpInside];
btn.index=idx;
[_scrollView addSubview:btn];if(_isShowSplitBorder && idx !=0) {
UIView*line = [[UIView alloc] initWithFrame:CGRectMake(0,0, _splitBorderWidth, btn.height)];
line.backgroundColor=_splitColor;
line.left=btn.left;
[_scrollView addSubview:line];
}
contentWidth=btn.right;
}];
_scrollView.contentSize=CGSizeMake(contentWidth, _scrollView.height);
[self setCurrentSelectedIndex:0];
[self refreshUI];
}
}-(void) btnTapped:(id) sender
{
WCSegmentControlItemButton*btn =sender;if(self.currentSelectedIndex ==btn.index) {return;
}
[self setCurrentSelectedIndex:btn.index];//不可以在setCurrentSelectedIndex触发,否则会造成重复执行if([(NSObject *) (self.delegate) respondsToSelector:@selector(wcSegmentControlSelectionChanged:)]) {
[self.delegatewcSegmentControlSelectionChanged:self];
}
}-(void) setCurrentSelectedIndex:(int) currentSelectedIndex
{
_currentSelectedIndex=currentSelectedIndex;
[_scrollView.subviews enumerateObjectsUsingBlock:^(idobj, NSUInteger idx, BOOL *stop) {if([obj isKindOfClass:[WCSegmentControlItemButtonclass]]) {
WCSegmentControlItemButton*view =obj;if(view.index ==currentSelectedIndex) {
[view setSelected:YES];
[view setBackgroundColor:_selectedItemBackgroundColor];//如果在屏幕外则需要移动到屏幕中if(view.right - _scrollView.width >0) {
_scrollView.contentOffset= CGPointMake(view.right - _scrollView.width,0)
;
}elseif(view.left - _scrollView.contentOffset.x <0) {
_scrollView.contentOffset= CGPointMake(view.left,0);
}if(_isShowItemBorderWhenHilight) {
view.layer.borderWidth=_borderWidth;
view.layer.borderColor=_borderColor.CGColor;
view.layer.cornerRadius=_cornerRadius;
}
}else{
[view setSelected:NO];
[view setBackgroundColor:_itemBackgroundColor];
view.layer.borderWidth=0;
}
}
}];
}@end