之前不同的項目寫了好幾次拌滋,這次想想封裝一下哥谷,代碼雖然不多,但是重復(fù)造輪子總不是好習(xí)慣瓶堕。
原理:ScrollView橫向滾動隘道,依次排列菜單按鈕,以顏色和下劃線來標(biāo)識選擇
圖中實現(xiàn)效果還有與collectionView的聯(lián)動郎笆,本文中就不提及了谭梗。
實現(xiàn)起來沒有復(fù)雜邏輯,直接上全套代碼:
typedef void(^MenuClickEvent)(NSInteger index);
@interface BDMenuScrollView : UIScrollView
{
UIView * markLine;
NSInteger lastIndex;
}
@property(nonatomic) NSInteger selectedIndex;
@property(nonatomic) BOOL repeatTouch;
@property(nonatomic, copy)MenuClickEvent clickBlock;
-(void)setMenuWithArray:(NSArray *)array;
-(void)setButtonSelectedAtIndex:(NSInteger)index;
@end
@implementation BDMenuScrollView
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.showsVerticalScrollIndicator = NO;
self.showsHorizontalScrollIndicator = NO;
}
return self;
}
-(void)setMenuWithArray:(NSArray *)array{
for (UIView * view in self.subviews) {
[view removeFromSuperview];
}
if(array.count){
CGRect frame = CGRectMake(0, 0, 0, self.frame.size.height);
for (int i = 0; i<array.count; i++) {
NSString * titleStr = array[i];
UIButton * button = [[UIButton alloc] initWithFrame:frame];
[self addSubview:button];
[button addTarget:self action:@selector(buttonEvent:) forControlEvents:UIControlEventTouchUpInside];
[button setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor colorFromHexString_au:@"04ab62"] forState:UIControlStateSelected];
[button setTitle:titleStr forState:UIControlStateNormal];
button.titleLabel.font = [UIFont systemFontOfSize:16];
button.tag = 100+i;
[button sizeToFit];
frame = button.frame;
frame.size.height = self.frame.size.height;
frame.size.width = frame.size.width + 30;
button.frame = frame;
frame.origin.x = CGRectGetMaxX(frame);
if(i == 0){
button.selected = YES;
self.selectedIndex = 0;
// add markline
markLine = [[UIView alloc]initWithFrame:CGRectMake(0, self.frame.size.height-2, 40, 2)];
[self addSubview:markLine];
markLine.backgroundColor = [UIColor colorFromHexString_au:@"04ab62"];
markLine.center = CGPointMake(button.center.x, markLine.centerY);
}
}
self.contentSize = CGSizeMake(frame.origin.x, 0);
}else{
return;
}
}
-(void)setButtonSelectedAtIndex:(NSInteger)index{
[self scrollToIndex:index];
}
-(void)scrollToIndex:(NSInteger)index{
if(self.selectedIndex == index){
return;
}
NSInteger a = lastIndex > self.selectedIndex ? lastIndex : self.selectedIndex;
NSInteger b = lastIndex <= self.selectedIndex ? lastIndex : self.selectedIndex;
// 忽略中間值
if(index <= a && index >= b){
return;
}
lastIndex = self.selectedIndex;
UIButton * lastButton = [self viewWithTag:lastIndex + 100];
lastButton.selected = NO;
self.selectedIndex = index;
lastButton = [self viewWithTag:self.selectedIndex + 100];
lastButton.selected = YES;
__weak typeof(self) weakself = self;
[UIView animateWithDuration:0.2 animations:^{
markLine.center = CGPointMake(lastButton.center.x, markLine.center.y);
} completion:^(BOOL finished) {
// 防止外部高頻調(diào)用的時候出現(xiàn)中間值, 產(chǎn)生結(jié)果偏移
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
lastIndex = weakself.selectedIndex;
});
}];
if(self.contentOffset.x > lastButton.frame.origin.x){
[self setContentOffset:CGPointMake(lastButton.frame.origin.x, 0) animated:YES];
}else if(self.contentOffset.x + self.frame.size.width < CGRectGetMaxX(lastButton.frame)){
[self setContentOffset:CGPointMake(CGRectGetMaxX(lastButton.frame)-self.frame.size.width, 0) animated:YES];
}
}
-(void)buttonEvent:(UIButton *)button{
if(button.tag - 100 == self.selectedIndex && !self.repeatTouch){
return;
}
NSInteger clickIndex = button.tag - 100;
if(self.clickBlock){
self.clickBlock(clickIndex);
}
[self scrollToIndex:clickIndex];
}
@end