属性:
//菜单内容
@property (nonatomic, strong) NSArray *titles;
//默认颜色 default:BlackColor
@property (nonatomic, strong) UIColor *nomalColor;
//选中颜色 default:RedColor
@property (nonatomic, strong) UIColor *selectedColor;
//指示器颜色 default:BlackColor
@property (nonatomic, strong) UIColor *IndicatorColor;
//高度 default:40
@property (nonatomic, assign) CGFloat menuHeight;
//delegate
@property (nonatomic, weak) id<LBTopScrollMenuDelegate> delegate_topMenu;
代理方法:
//index:选中的item编号
- (void)topScrollMenu:(LBTopScrollMenu *)scrollMenu didSelectedItemInIndex:(NSUInteger)index;
公共方法:
//init titles:菜单内容
- (instancetype)initWithFrame:(CGRect)frame titles:(NSArray *)titles;
//更新指示器 index:当前选中的item的编号
- (void)updateIndicatorWithSelectedItemIndex:(NSUInteger)index;
使用方法:
//懒加载
- (LBTopScrollMenu *)topMenu
{
if (!_topMenu) {
//初始化有三种方法,init、initWithFrame、initWithFrame: titles:
_topMenu = [[LBTopScrollMenu alloc] init];
_topMenu.backgroundColor = [UIColor greenColor];
_topMenu.menuHeight = 40;
_topMenu.titles = self.titles;
_topMenu.delegate_topMenu = self;
_topMenu.nomalColor = [UIColor yellowColor];
_topMenu.selectedColor = [UIColor redColor];
_topMenu.lineColor = [UIColor blueColor];
}
return _topMenu;
}
//<LBTopScrollMenuDelegate>
- (void)topScrollMenu:(LBTopScrollMenu *)scrollMenu didSelectedItemInIndex:(NSUInteger)index
{
//偏移
[self.scrollView setContentOffset:CGPointMake(index * CGRectGetWidth(self.view.frame), 0) animated:YES];
}
//<UIScrollViewDelegate>
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
NSUInteger index = scrollView.contentOffset.x / CGRectGetWidth(self.view.frame);
//更新指示器
[self.topMenu updateIndicatorWithSelectedItemIndex:index];
}