Initial commit
This commit is contained in:
+84
@@ -0,0 +1,84 @@
|
||||
//
|
||||
// UMComHorizonCollectionView.h
|
||||
// UMCommunity
|
||||
//
|
||||
// Created by umeng on 15/11/26.
|
||||
// Copyright © 2015年 Umeng. All rights reserved.
|
||||
//
|
||||
|
||||
#import <UIKit/UIKit.h>
|
||||
|
||||
@class UMComHorizonCollectionCell,UMComHorizonCollectionView,UMComDropdownColumnCell;
|
||||
|
||||
|
||||
@protocol UMComHorizonCollectionViewDelegate <NSObject>
|
||||
|
||||
@optional;
|
||||
|
||||
- (NSInteger)numberOfRowInHorizonCollectionView:(UMComHorizonCollectionView *)collectionView;
|
||||
|
||||
- (void)horizonCollectionView:(UMComHorizonCollectionView *)collectionView reloadCell:(UMComHorizonCollectionCell *)cell atIndexPath:(NSIndexPath *)indexPath;
|
||||
|
||||
- (void)horizonCollectionView:(UMComHorizonCollectionView *)collectionView didSelectedColumn:(NSInteger)column;
|
||||
|
||||
- (UIImage *)horizonCollectionView:(UMComHorizonCollectionView *)collectionView imageForIndexPath:(NSIndexPath *)indexPath;
|
||||
|
||||
- (UIImage *)horizonCollectionView:(UMComHorizonCollectionView *)collectionView hilightImageForIndexPath:(NSIndexPath *)indexPath;
|
||||
|
||||
- (NSString *)horizonCollectionView:(UMComHorizonCollectionView *)collectionView titleForIndexPath:(NSIndexPath *)indexPath;
|
||||
|
||||
@end
|
||||
|
||||
@interface UMComHorizonCollectionView : UICollectionView
|
||||
|
||||
@property (nonatomic, assign) BOOL isHighLightWhenDidSelected;
|
||||
|
||||
@property (nonatomic, strong) UIView *bottomLine;
|
||||
|
||||
@property (nonatomic, assign) CGFloat itemSpace;
|
||||
|
||||
@property (nonatomic, assign) CGSize itemSize;
|
||||
|
||||
@property (nonatomic, readonly) NSInteger currentIndex;
|
||||
|
||||
@property (nonatomic, readonly) NSInteger lastIndex;
|
||||
|
||||
@property (nonatomic, copy) UIColor *titleHightColor;
|
||||
|
||||
|
||||
@property (nonatomic, assign) CGFloat bottomLineHeight;
|
||||
|
||||
@property (nonatomic, assign) CGFloat indicatorLineHeight;
|
||||
|
||||
@property (nonatomic, assign) CGFloat indicatorLineWidth;
|
||||
|
||||
@property (nonatomic, assign) CGFloat indicatorLineLeftEdge;
|
||||
|
||||
@property (nonatomic, strong) UIImageView *scrollIndicatorView;
|
||||
|
||||
@property (nonatomic, strong) UIView *topLine;
|
||||
|
||||
|
||||
@property (nonatomic, weak) id<UMComHorizonCollectionViewDelegate> cellDelegate;
|
||||
|
||||
|
||||
- (instancetype)initWithFrame:(CGRect)frame itemCount:(NSInteger)count;
|
||||
|
||||
- (instancetype)initWithFrame:(CGRect)frame itemSize:(CGSize)itemSize itemCount:(NSInteger)count;
|
||||
|
||||
- (void)startIndex:(NSInteger)index;
|
||||
|
||||
@end
|
||||
|
||||
|
||||
@interface UMComHorizonCollectionCell : UICollectionViewCell
|
||||
|
||||
@property (nonatomic, strong) UIImageView *imageView;
|
||||
|
||||
@property (nonatomic, strong) UILabel *label;
|
||||
|
||||
@property (nonatomic, assign) NSInteger index;
|
||||
|
||||
|
||||
@end
|
||||
|
||||
+244
@@ -0,0 +1,244 @@
|
||||
//
|
||||
// UMComHorizonCollectionView.m
|
||||
// UMCommunity
|
||||
//
|
||||
// Created by umeng on 15/11/26.
|
||||
// Copyright © 2015年 Umeng. All rights reserved.
|
||||
//
|
||||
|
||||
#import "UMComHorizonCollectionView.h"
|
||||
#import "UMComResouceDefines.h"
|
||||
|
||||
@interface UMComHorizonCollectionView ()<UICollectionViewDataSource, UICollectionViewDelegate>
|
||||
|
||||
@property (nonatomic, assign) NSInteger itemCount;
|
||||
|
||||
@property (nonatomic, strong) UICollectionViewFlowLayout *currentLayout;
|
||||
|
||||
@property (nonatomic, strong) NSIndexPath *currentIndexPath;
|
||||
|
||||
@property (nonatomic, strong) NSMutableDictionary *indexPathsDict;
|
||||
|
||||
@property (nonatomic, assign) BOOL isTheFirstTime;
|
||||
|
||||
@end
|
||||
|
||||
@implementation UMComHorizonCollectionView
|
||||
|
||||
- (instancetype)initWithFrame:(CGRect)frame itemCount:(NSInteger)count
|
||||
{
|
||||
CGFloat itemWidth = frame.size.width/count;
|
||||
CGSize itemSize = CGSizeMake(itemWidth, frame.size.height);
|
||||
self = [self initWithFrame:frame itemSize:itemSize itemCount:count];
|
||||
return self;
|
||||
}
|
||||
|
||||
- (instancetype)initWithFrame:(CGRect)frame itemSize:(CGSize)itemSize itemCount:(NSInteger)count
|
||||
{
|
||||
UICollectionViewFlowLayout *currentLayout = [[UICollectionViewFlowLayout alloc]init];
|
||||
currentLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
|
||||
currentLayout.itemSize = itemSize;
|
||||
currentLayout.sectionInset = UIEdgeInsetsMake(0.5, 0.5, 0.5, 0.5);
|
||||
self.currentLayout = currentLayout;
|
||||
self = [super initWithFrame:frame collectionViewLayout:currentLayout];
|
||||
if (self) {
|
||||
_itemSize = itemSize;
|
||||
self.itemCount = count;
|
||||
self.itemSpace = 0.5;
|
||||
_indicatorLineWidth = itemSize.width;
|
||||
_indicatorLineHeight = 0.5;
|
||||
_isTheFirstTime = YES;
|
||||
_indexPathsDict = [NSMutableDictionary dictionary];
|
||||
self.dataSource = self;
|
||||
self.delegate = self;
|
||||
[self registerClass:[UMComHorizonCollectionCell class] forCellWithReuseIdentifier:@"UMComHorizonCollectionCell"];
|
||||
self.backgroundColor = [UIColor whiteColor];
|
||||
self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
|
||||
self.scrollIndicatorView = [[UIImageView alloc]initWithFrame:CGRectMake(_itemSpace, self.frame.size.height-0.5, self.itemSize.width, 0.5)];
|
||||
self.scrollIndicatorView.backgroundColor = [UIColor clearColor];
|
||||
[self addSubview:self.scrollIndicatorView];
|
||||
_indicatorLineLeftEdge = 0;
|
||||
UIView *topLine = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, 1)];
|
||||
[self addSubview:topLine];
|
||||
self.topLine = topLine;
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)setItemCount:(NSInteger)itemCount
|
||||
{
|
||||
_itemCount = itemCount;
|
||||
[self reloadData];
|
||||
}
|
||||
|
||||
- (void)setItemSpace:(CGFloat)itemSpace
|
||||
{
|
||||
_itemSpace = itemSpace;
|
||||
self.currentLayout.minimumLineSpacing = itemSpace;
|
||||
CGFloat itemWidth = (self.frame.size.width-itemSpace*(self.itemCount+1))/self.itemCount;
|
||||
self.currentLayout.itemSize = CGSizeMake(itemWidth, self.frame.size.height-1);
|
||||
self.itemSize = self.currentLayout.itemSize;
|
||||
|
||||
}
|
||||
|
||||
- (void)setBottomLineHeight:(CGFloat)bottomLineHeight
|
||||
{
|
||||
_bottomLineHeight = bottomLineHeight;
|
||||
if (!self.bottomLine) {
|
||||
self.bottomLine = [[UIView alloc]initWithFrame:CGRectMake(0, self.frame.size.height-bottomLineHeight, self.frame.size.width, bottomLineHeight)];
|
||||
self.bottomLine.backgroundColor = self.backgroundColor;
|
||||
[self addSubview:self.bottomLine];
|
||||
}else{
|
||||
self.bottomLine.frame = CGRectMake(0, self.frame.size.height-bottomLineHeight, self.frame.size.width, bottomLineHeight);
|
||||
}
|
||||
}
|
||||
|
||||
- (void)setIndicatorLineHeight:(CGFloat)indicatorLineHeight
|
||||
{
|
||||
_indicatorLineHeight = indicatorLineHeight;
|
||||
CGRect frame = _scrollIndicatorView.frame;
|
||||
frame.size.height = indicatorLineHeight;
|
||||
frame.origin.y = self.frame.size.height - indicatorLineHeight;
|
||||
_scrollIndicatorView.frame = frame;
|
||||
}
|
||||
|
||||
- (void)setIndicatorLineWidth:(CGFloat)indicatorLineWidth
|
||||
{
|
||||
_indicatorLineWidth = indicatorLineWidth;
|
||||
CGRect frame = _scrollIndicatorView.frame;
|
||||
frame.size.width = indicatorLineWidth;
|
||||
_scrollIndicatorView.frame = frame;
|
||||
}
|
||||
|
||||
- (void)setIndicatorLineLeftEdge:(CGFloat)indicatorLineLeftEdge
|
||||
{
|
||||
_indicatorLineLeftEdge = indicatorLineLeftEdge;
|
||||
CGRect frame = _scrollIndicatorView.frame;
|
||||
frame.origin.x = indicatorLineLeftEdge;
|
||||
_scrollIndicatorView.frame = frame;
|
||||
}
|
||||
|
||||
- (void)startIndex:(NSInteger)index
|
||||
{
|
||||
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0];
|
||||
[self collectionView:self didSelectItemAtIndexPath:indexPath];
|
||||
__weak typeof(self) weakSelf = self;
|
||||
[UIView animateWithDuration:0.25 animations:^{
|
||||
weakSelf.scrollIndicatorView.center = CGPointMake(self.itemSize.width*index+weakSelf.indicatorLineWidth/2+UMComWidthScaleBetweenCurentScreenAndiPhone6Screen(2.5) + _indicatorLineLeftEdge, weakSelf.scrollIndicatorView.center.y);
|
||||
}];
|
||||
}
|
||||
#pragma mark -
|
||||
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
|
||||
{
|
||||
if (self.cellDelegate && [self.cellDelegate respondsToSelector:@selector(numberOfRowInHorizonCollectionView:)]) {
|
||||
return [self.cellDelegate numberOfRowInHorizonCollectionView:self];
|
||||
}
|
||||
return _itemCount;
|
||||
}
|
||||
|
||||
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
|
||||
{
|
||||
UMComHorizonCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"UMComHorizonCollectionCell" forIndexPath:indexPath];
|
||||
cell.index = indexPath.row;
|
||||
if (_isTheFirstTime == YES) {
|
||||
self.currentIndexPath = indexPath;
|
||||
_isTheFirstTime = NO;
|
||||
}
|
||||
if (self.cellDelegate && [self.cellDelegate respondsToSelector:@selector(horizonCollectionView:reloadCell:atIndexPath:)]) {
|
||||
[self.cellDelegate horizonCollectionView:self reloadCell:cell atIndexPath:indexPath];
|
||||
}
|
||||
if (self.currentIndexPath.row == indexPath.row) {
|
||||
if (self.cellDelegate && [self.cellDelegate respondsToSelector:@selector(horizonCollectionView:hilightImageForIndexPath:)]) {
|
||||
cell.imageView.image = [self.cellDelegate horizonCollectionView:self hilightImageForIndexPath:indexPath];
|
||||
}else{
|
||||
if (self.cellDelegate && [self.cellDelegate respondsToSelector:@selector(horizonCollectionView:imageForIndexPath:)]) {
|
||||
cell.imageView.image = [self.cellDelegate horizonCollectionView:self imageForIndexPath:indexPath];
|
||||
}
|
||||
}
|
||||
if (self.titleHightColor) {
|
||||
cell.label.textColor = self.titleHightColor;
|
||||
}
|
||||
}else{
|
||||
if (self.cellDelegate && [self.cellDelegate respondsToSelector:@selector(horizonCollectionView:imageForIndexPath:)]) {
|
||||
cell.imageView.image = [self.cellDelegate horizonCollectionView:self imageForIndexPath:indexPath];
|
||||
}
|
||||
}
|
||||
if (self.cellDelegate && [self.cellDelegate respondsToSelector:@selector(horizonCollectionView:titleForIndexPath:)]) {
|
||||
cell.label.text = [self.cellDelegate horizonCollectionView:self titleForIndexPath:indexPath];
|
||||
}
|
||||
return cell;
|
||||
}
|
||||
|
||||
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
|
||||
{
|
||||
UMComHorizonCollectionCell *cell = (UMComHorizonCollectionCell *)[collectionView cellForItemAtIndexPath:indexPath];
|
||||
__weak typeof(self) weakSelf = self;
|
||||
self.currentIndexPath = indexPath;
|
||||
_lastIndex = self.currentIndex;
|
||||
_currentIndex = indexPath.row;
|
||||
if (self.cellDelegate && [self.cellDelegate respondsToSelector:@selector(horizonCollectionView:didSelectedColumn:)]) {
|
||||
|
||||
[UIView animateWithDuration:0.25 animations:^{
|
||||
weakSelf.scrollIndicatorView.center = CGPointMake(cell.frame.origin.x+weakSelf.indicatorLineWidth/2 + _indicatorLineLeftEdge, weakSelf.scrollIndicatorView.center.y);
|
||||
}];
|
||||
[self.cellDelegate horizonCollectionView:self didSelectedColumn:indexPath.row];
|
||||
}
|
||||
[self reloadData];
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
|
||||
{
|
||||
return _itemSize;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
// Only override drawRect: if you perform custom drawing.
|
||||
// An empty implementation adversely affects performance during animation.
|
||||
- (void)drawRect:(CGRect)rect {
|
||||
// Drawing code
|
||||
}
|
||||
*/
|
||||
|
||||
//- (void)drawRect:(CGRect)rect
|
||||
//{
|
||||
// UIColor *color = [UIColor redColor];
|
||||
// CGContextRef context = UIGraphicsGetCurrentContext();
|
||||
// CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
|
||||
// CGContextFillRect(context, rect);
|
||||
// CGContextSetStrokeColorWithColor(context, color.CGColor);
|
||||
// CGFloat itemSpaceTopEdge = 6;
|
||||
// for (int index = 1; index < self.itemCount; index++) {
|
||||
// CGContextStrokeRect(context, CGRectMake(index * self.itemSize.width, itemSpaceTopEdge, self.itemSpace, rect.size.height - itemSpaceTopEdge*2));
|
||||
// }
|
||||
//}
|
||||
|
||||
@end
|
||||
|
||||
|
||||
@implementation UMComHorizonCollectionCell
|
||||
|
||||
- (instancetype)initWithFrame:(CGRect)frame
|
||||
{
|
||||
self = [super initWithFrame:frame];
|
||||
if (self) {
|
||||
self.contentView.backgroundColor = [UIColor whiteColor];
|
||||
self.imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height-1)];
|
||||
self.imageView.backgroundColor = [UIColor clearColor];
|
||||
[self.contentView addSubview:self.imageView];
|
||||
|
||||
self.label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height-1)];
|
||||
self.label.backgroundColor = [UIColor clearColor];
|
||||
self.label.font = UMComFontNotoSansLightWithSafeSize(15);
|
||||
self.label.textAlignment = NSTextAlignmentCenter;
|
||||
self.label.textColor = [UIColor blackColor];
|
||||
self.label.numberOfLines = 0;
|
||||
[self.contentView addSubview:self.label];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
@end
|
||||
+160
@@ -0,0 +1,160 @@
|
||||
//
|
||||
// UMComHorizonMenuView.h
|
||||
// UMCommunity
|
||||
//
|
||||
// Created by umeng on 15/11/10.
|
||||
// Copyright © 2015年 Umeng. All rights reserved.
|
||||
//
|
||||
|
||||
#import <UIKit/UIKit.h>
|
||||
|
||||
typedef enum {
|
||||
menuDefaultType = 0,
|
||||
menuImageFullNoTitleType = 1,
|
||||
menuTitleFullNoImageType = 2,
|
||||
menuTitleFrontImageType = 3,
|
||||
menuTitleBottomAndImageTop = 4,
|
||||
menuTitleTopAndImageBottom = 5,
|
||||
menuTitleBottomAndNoImage = 6,
|
||||
menuTitleLeftAndImageRight = 7,
|
||||
menuTitleRightAndImageLeft = 8,
|
||||
menuButtonType = 9,//定义button点击效果
|
||||
}UMComMenuItemViewType;
|
||||
|
||||
@class UMComMenuItem, UMComMenuItemView;
|
||||
|
||||
@interface UMComHorizonMenuView : UIView
|
||||
|
||||
@property (nonatomic, assign) CGSize itemSize;
|
||||
|
||||
@property (nonatomic, assign) CGFloat leftMargin;
|
||||
|
||||
@property (nonatomic, assign) CGFloat rightMargin;
|
||||
|
||||
@property (nonatomic, assign) CGFloat topMargin;
|
||||
|
||||
@property (nonatomic, readonly) NSInteger selectedIndex;
|
||||
|
||||
@property (nonatomic, readonly) NSInteger lastIndex;
|
||||
|
||||
@property (nonatomic, strong) NSArray *menuItems;
|
||||
|
||||
@property (nonatomic, assign) CGFloat itemSpace;
|
||||
|
||||
@property (nonatomic, strong) UIImage *bgImage;
|
||||
|
||||
@property (nonatomic, assign) BOOL isHighLightWhenDidSelected;
|
||||
|
||||
@property (nonatomic, assign) UIFont *itemTextFont;
|
||||
|
||||
@property (nonatomic, strong) UIView *bottomLine;
|
||||
|
||||
@property (nonatomic, assign) CGFloat bottomLineWidth;
|
||||
|
||||
@property (nonatomic, strong) UIImageView *scrollIndicatorView;
|
||||
|
||||
@property (nonatomic, assign) CGFloat scrollIndicatorWidth;
|
||||
|
||||
|
||||
@property (nonatomic, copy ) void (^selectedAtIndex)(UMComHorizonMenuView *menuView, NSInteger index);
|
||||
|
||||
- (void)reloadWithMenuItems:(NSArray *)menuItems
|
||||
itemSize:(CGSize)itemSize;
|
||||
|
||||
- (void)reloadWithMenuItems:(NSArray *)menuItems
|
||||
leftMargin:(CGFloat)leftMargin
|
||||
rightMargin:(CGFloat)rightMargin
|
||||
itemSize:(CGSize)itemSize;
|
||||
|
||||
- (void)reloadItems;
|
||||
|
||||
/**
|
||||
* 2.4版本的新方法的布局模型,|leftmargin-item-space....item-rightmargin|
|
||||
*
|
||||
*/
|
||||
- (void)reloadWithNewMenuItems:(NSArray *)menuItems
|
||||
leftMargin:(CGFloat)leftMargin
|
||||
rightMargin:(CGFloat)rightMargin
|
||||
itemSize:(CGSize)itemSize;
|
||||
|
||||
/**
|
||||
* 返回对应索引的控件
|
||||
* 如果menuButtonType,返回UIButton
|
||||
*/
|
||||
-(UIView*) viewWithIndex:(NSInteger)index;
|
||||
|
||||
@end
|
||||
|
||||
typedef enum {
|
||||
|
||||
HighLightTextColor = 0,
|
||||
HighLightImage = 1,
|
||||
HighLightBgColor = 2,
|
||||
HighLightText = 3,
|
||||
|
||||
}HighLightType;
|
||||
|
||||
|
||||
|
||||
@interface UMComMenuItem : NSObject
|
||||
|
||||
@property (nonatomic, copy) NSString *title;
|
||||
|
||||
@property (nonatomic, copy) NSString *highLightTitle;
|
||||
|
||||
@property (nonatomic, copy) UIColor *highTextColor;
|
||||
|
||||
@property (nonatomic, copy) UIColor *textCorlor;
|
||||
|
||||
@property (nonatomic, copy) NSString *imageName;
|
||||
|
||||
@property (nonatomic, copy) NSString *highLightImageName;
|
||||
|
||||
@property (nonatomic, strong) UIColor *bgColor;
|
||||
|
||||
@property (nonatomic, strong) UIColor *highLightBgColor;
|
||||
|
||||
@property (nonatomic, assign) HighLightType highLightType;
|
||||
|
||||
@property (nonatomic, assign) UMComMenuItemViewType itemViewType;
|
||||
|
||||
@property (nonatomic, assign) BOOL isHighLighted;
|
||||
|
||||
+ (UMComMenuItem *)itemWithTitle:(NSString *)title imageName:(NSString *)name;
|
||||
|
||||
+ (UMComMenuItem *)itemWithTitle:(NSString *)title imageName:(NSString *)name highLightType:(HighLightType)highLightType itemViewType:(UMComMenuItemViewType)itemViewType;
|
||||
|
||||
+ (UMComMenuItem *)itemWithTitle:(NSString *)title imageName:(NSString *)name highLightImageName:(NSString *)highLightImageName;
|
||||
|
||||
+ (UMComMenuItem *)itemWithTitle:(NSString *)title imageName:(NSString *)name highLightTitle:(NSString *)highLightTitle highLightImageName:(NSString *)highLightImageName;
|
||||
|
||||
|
||||
@end
|
||||
|
||||
|
||||
@interface UMComMenuItemView : UIView
|
||||
|
||||
@property (nonatomic, strong) UILabel *titleLabel;
|
||||
|
||||
@property (nonatomic, strong) UIImageView *imageView;
|
||||
|
||||
@property (nonatomic, assign) NSInteger index;
|
||||
|
||||
@property (nonatomic, strong) UMComMenuItem *menuItem;
|
||||
|
||||
@property (nonatomic, strong) UIButton *buton;//为高亮准备的
|
||||
|
||||
|
||||
@property (nonatomic, copy) void (^clickBlock)(UMComMenuItemView *menuItemView,NSInteger index);
|
||||
|
||||
- (void)reloadWithTitle:(NSString *)title
|
||||
image:(UIImage *)image
|
||||
index:(NSInteger)index;
|
||||
- (void)reloadWithTitle:(NSString *)title
|
||||
image:(UIImage *)image;
|
||||
|
||||
- (void)highLight;
|
||||
|
||||
- (void)nomal;
|
||||
|
||||
@end
|
||||
+464
@@ -0,0 +1,464 @@
|
||||
//
|
||||
// UMComHorizonMenuView.m
|
||||
// UMCommunity
|
||||
//
|
||||
// Created by umeng on 15/11/10.
|
||||
// Copyright © 2015年 Umeng. All rights reserved.
|
||||
//
|
||||
|
||||
#import "UMComHorizonMenuView.h"
|
||||
#import "UMComResouceDefines.h"
|
||||
#import <UMComFoundation/UMComKit+Color.h>
|
||||
|
||||
#define textFont UMComFontNotoSansLightWithSafeSize(15)
|
||||
#define ButtonTextFont UMComFontNotoSansLightWithSafeSize(18)
|
||||
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
@interface UMComHorizonMenuView ()
|
||||
|
||||
@property (nonatomic, strong) NSMutableArray *menuItemViews;
|
||||
|
||||
@property (nonatomic) NSInteger selectedIndex;
|
||||
|
||||
@property (nonatomic) NSInteger lastIndex;
|
||||
|
||||
@property (nonatomic, strong) UIImageView *bgImageView;
|
||||
|
||||
@end
|
||||
|
||||
@implementation UMComHorizonMenuView
|
||||
|
||||
|
||||
- (instancetype)initWithFrame:(CGRect)frame
|
||||
{
|
||||
self = [super initWithFrame:frame];
|
||||
if (self) {
|
||||
self.backgroundColor = UMComColorWithHexString(@"#ececec");
|
||||
self.leftMargin = 0;
|
||||
self.rightMargin = 0;
|
||||
self.bottomLineWidth = 1;
|
||||
self.itemSize = CGSizeMake(30, 30);
|
||||
self.isHighLightWhenDidSelected = NO;
|
||||
self.menuItemViews = [NSMutableArray array];
|
||||
self.itemTextFont = textFont;
|
||||
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
|
||||
[self addSubview:imageView];
|
||||
self.bgImageView = imageView;
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)setBottomLineWidth:(CGFloat)bottomLineWidth
|
||||
{
|
||||
_bottomLineWidth = bottomLineWidth;
|
||||
if (!self.bottomLine) {
|
||||
self.bottomLine = [[UIView alloc]initWithFrame:CGRectMake(0, self.frame.size.height-bottomLineWidth, self.frame.size.width, bottomLineWidth)];
|
||||
[self addSubview:self.bottomLine];
|
||||
}else{
|
||||
self.bottomLine.frame = CGRectMake(0, self.frame.size.height-bottomLineWidth, self.frame.size.width, bottomLineWidth);
|
||||
}
|
||||
}
|
||||
|
||||
- (void)setScrollIndicatorWidth:(CGFloat)scrollIndicatorWidth
|
||||
{
|
||||
_scrollIndicatorWidth = scrollIndicatorWidth;
|
||||
CGFloat width = self.itemSize.width;
|
||||
if (width == 0) {
|
||||
width = 1;
|
||||
}
|
||||
if (!self.scrollIndicatorView) {
|
||||
self.scrollIndicatorView = [[UIImageView alloc]initWithFrame:CGRectMake(0, self.frame.size.height-scrollIndicatorWidth, width, scrollIndicatorWidth)];
|
||||
[self addSubview:self.scrollIndicatorView];
|
||||
}else{
|
||||
self.scrollIndicatorView.frame = CGRectMake(0, self.frame.size.height-scrollIndicatorWidth, width, scrollIndicatorWidth);
|
||||
}
|
||||
}
|
||||
- (void)setItemSize:(CGSize)itemSize
|
||||
{
|
||||
_itemSize = itemSize;
|
||||
self.scrollIndicatorView.frame = CGRectMake(self.scrollIndicatorView.frame.origin.x, self.scrollIndicatorView.frame.origin.y, itemSize.width, self.scrollIndicatorView.frame.size.height);
|
||||
}
|
||||
|
||||
- (void)setBgImage:(UIImage *)bgImage
|
||||
{
|
||||
_bgImage = bgImage;
|
||||
self.bgImageView.image = bgImage;
|
||||
}
|
||||
|
||||
- (void)setMenuItems:(NSArray *)menuItems
|
||||
{
|
||||
_menuItems = menuItems;
|
||||
if (menuItems.count == self.menuItemViews.count) {
|
||||
for (int index = 0; index < menuItems.count; index ++ ) {
|
||||
UMComMenuItemView *menuItemView = self.menuItemViews[index];
|
||||
UMComMenuItem *item = self.menuItems[index];
|
||||
menuItemView.menuItem = item;
|
||||
menuItemView.index = index;
|
||||
}
|
||||
}else{
|
||||
for (UMComMenuItemView *menuItemView in self.menuItemViews) {
|
||||
[menuItemView removeFromSuperview];
|
||||
}
|
||||
[self.menuItemViews removeAllObjects];
|
||||
for (int index = 0; index < menuItems.count; index ++ ) {
|
||||
UMComMenuItemView *menuItemView = [[UMComMenuItemView alloc] initWithFrame:CGRectMake(0, 0, self.itemSize.width, self.itemSize.height)];
|
||||
menuItemView.titleLabel.font = self.itemTextFont;
|
||||
UMComMenuItem *item = self.menuItems[index];
|
||||
menuItemView.menuItem = item;
|
||||
menuItemView.index = index;
|
||||
[self.menuItemViews addObject:menuItemView];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
- (void)reloadWithMenuItems:(NSArray *)menuItems
|
||||
itemSize:(CGSize)itemSize
|
||||
{
|
||||
self.itemSize = itemSize;
|
||||
self.menuItems = menuItems;
|
||||
[self reloadItems];
|
||||
}
|
||||
|
||||
- (void)reloadWithMenuItems:(NSArray *)menuItems
|
||||
leftMargin:(CGFloat)leftMargin
|
||||
rightMargin:(CGFloat)rightMargin
|
||||
itemSize:(CGSize)itemSize
|
||||
{
|
||||
self.leftMargin = leftMargin;
|
||||
self.rightMargin = rightMargin;
|
||||
if (menuItems.count == 0) {
|
||||
return;
|
||||
}
|
||||
[self reloadWithMenuItems:menuItems itemSize:itemSize];
|
||||
}
|
||||
|
||||
- (void)reloadWithNewMenuItems:(NSArray *)menuItems
|
||||
leftMargin:(CGFloat)leftMargin
|
||||
rightMargin:(CGFloat)rightMargin
|
||||
itemSize:(CGSize)itemSize
|
||||
{
|
||||
if (menuItems.count == 0) {
|
||||
return;
|
||||
}
|
||||
self.leftMargin = leftMargin;
|
||||
self.rightMargin = rightMargin;
|
||||
self.itemSize = itemSize;
|
||||
self.menuItems = menuItems;
|
||||
|
||||
{
|
||||
CGFloat itemSpace = 0;
|
||||
NSInteger spaceCount = self.menuItemViews.count - 1;
|
||||
itemSpace = (self.frame.size.width - self.leftMargin - self.rightMargin - self.itemSize.width * self.menuItemViews.count)/spaceCount;
|
||||
self.itemSpace = itemSpace;
|
||||
for (int index = 0; index < self.menuItemViews.count; index ++) {
|
||||
UMComMenuItemView *menuItemView = self.menuItemViews[index];
|
||||
CGRect tempFrame = CGRectMake(self.leftMargin + index*(self.itemSpace + self.itemSize.width), (self.frame.size.height - self.itemSize.height)/2, self.itemSize.width, self.frame.size.height-self.bottomLineWidth);
|
||||
menuItemView.frame = tempFrame;
|
||||
[self menuItemsFrameWithMenuItem:menuItemView];
|
||||
[self menuItemView:menuItemView index:index];
|
||||
[self setMenuItemViewBlock:menuItemView];
|
||||
[self addSubview:menuItemView];
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
-(UIView*) viewWithIndex:(NSInteger)index
|
||||
{
|
||||
if (index >= 0 && index < self.menuItemViews.count) {
|
||||
|
||||
UMComMenuItemView* view = self.menuItemViews[index];
|
||||
if (view) {
|
||||
return view.buton;
|
||||
}
|
||||
return nil;
|
||||
}
|
||||
return nil;
|
||||
}
|
||||
|
||||
- (void)reloadItems
|
||||
{
|
||||
if (self.menuItems.count == 0 || self.menuItemViews.count == 0) {
|
||||
return;
|
||||
}
|
||||
CGFloat itemSpace = 0;
|
||||
NSInteger spaceCount = self.menuItemViews.count + 1;
|
||||
itemSpace = (self.frame.size.width - self.leftMargin - self.rightMargin - self.itemSize.width * self.menuItemViews.count)/spaceCount;
|
||||
self.itemSpace = itemSpace;
|
||||
self.bottomLine.frame = CGRectMake(self.leftMargin+itemSpace, self.bottomLine.frame.origin.y, self.frame.size.width-self.leftMargin-self.rightMargin-itemSpace*2, self.bottomLine.frame.size.height);
|
||||
self.scrollIndicatorView.center = CGPointMake(self.leftMargin+itemSpace+self.itemSize.width/2, self.scrollIndicatorView.center.y);
|
||||
[self reloadAnyTypeOfMenuItems];
|
||||
}
|
||||
|
||||
|
||||
- (void)reloadAnyTypeOfMenuItems
|
||||
{
|
||||
if (self.menuItems.count == 0 || self.menuItemViews.count == 0) {
|
||||
return;
|
||||
}
|
||||
CGFloat itemSpace = 0;
|
||||
NSInteger spaceCount = self.menuItemViews.count + 1;
|
||||
itemSpace = (self.frame.size.width - self.leftMargin - self.rightMargin - self.itemSize.width * self.menuItemViews.count)/spaceCount;
|
||||
self.itemSpace = itemSpace;
|
||||
for (int index = 0; index < self.menuItemViews.count; index ++) {
|
||||
UMComMenuItemView *menuItemView = self.menuItemViews[index];
|
||||
menuItemView.frame = [self itemFrameWithIndex:index];
|
||||
[self menuItemsFrameWithMenuItem:menuItemView];
|
||||
[self menuItemView:menuItemView index:index];
|
||||
[self setMenuItemViewBlock:menuItemView];
|
||||
[self addSubview:menuItemView];
|
||||
}
|
||||
}
|
||||
|
||||
- (CGRect)itemFrameWithIndex:(NSInteger)index
|
||||
{
|
||||
CGRect frame = CGRectMake(self.leftMargin + index*(self.itemSpace + self.itemSize.width) + self.itemSpace, (self.frame.size.height - self.itemSize.height)/2, self.itemSize.width, self.frame.size.height-self.bottomLineWidth);
|
||||
return frame;
|
||||
}
|
||||
|
||||
- (void)menuItemsFrameWithMenuItem:(UMComMenuItemView *)itemView
|
||||
{
|
||||
UMComMenuItemViewType itemType = itemView.menuItem.itemViewType;
|
||||
CGFloat topMargin = self.topMargin+self.bottomLineWidth;
|
||||
if (itemType == menuImageFullNoTitleType || itemType == menuDefaultType) {
|
||||
itemView.imageView.frame = CGRectMake(0, topMargin/2, self.itemSize.width, self.itemSize.height-topMargin);
|
||||
}else if (itemType == menuTitleFrontImageType){
|
||||
itemView.imageView.frame = CGRectMake(0, topMargin/2, self.itemSize.width, self.itemSize.height-topMargin);
|
||||
}else if (itemType == menuTitleFullNoImageType){
|
||||
itemView.imageView.hidden = YES;
|
||||
itemView.titleLabel.frame = CGRectMake(0, topMargin/2, self.itemSize.width, self.itemSize.height-topMargin);
|
||||
}else if(itemType == menuTitleBottomAndNoImage){
|
||||
itemView.titleLabel.frame = CGRectMake(0, topMargin/2, self.itemSize.width, self.itemSize.height-topMargin);
|
||||
}else if (itemType == menuTitleBottomAndImageTop){
|
||||
CGFloat titleHeight = self.itemSize.height/3;
|
||||
itemView.titleLabel.frame = CGRectMake(0, topMargin/2, self.itemSize.width, titleHeight);
|
||||
itemView.imageView.frame = CGRectMake(0, topMargin/2+itemView.titleLabel.frame.size.height, self.itemSize.width, self.itemSize.height - titleHeight-topMargin);
|
||||
}else if (itemType == menuTitleTopAndImageBottom){
|
||||
CGFloat titleHeight = self.itemSize.height*2/3;
|
||||
itemView.imageView.frame = CGRectMake(0, topMargin/2, self.itemSize.width, titleHeight);
|
||||
itemView.titleLabel.frame = CGRectMake(0, topMargin/2+itemView.titleLabel.frame.size.height, self.itemSize.width, self.itemSize.height - titleHeight-topMargin);
|
||||
}else if (itemType == menuTitleRightAndImageLeft){
|
||||
CGFloat imageWidth = self.itemSize.height;
|
||||
itemView.imageView.frame = CGRectMake(0, topMargin/2, imageWidth, imageWidth);
|
||||
itemView.titleLabel.frame = CGRectMake(imageWidth, topMargin/2, self.itemSize.width-imageWidth, self.itemSize.height-topMargin);
|
||||
}else if (itemType == menuTitleLeftAndImageRight){
|
||||
CGFloat imageWidth = self.itemSize.height;
|
||||
itemView.imageView.frame = CGRectMake(self.itemSize.width-imageWidth, topMargin/2, imageWidth, imageWidth);
|
||||
itemView.titleLabel.frame = CGRectMake(0, topMargin/2, self.itemSize.width-imageWidth, self.itemSize.height-topMargin);
|
||||
}
|
||||
else if (itemType == menuButtonType)
|
||||
{
|
||||
itemView.imageView.hidden = YES;
|
||||
NSString* imageName = itemView.menuItem.imageName;
|
||||
NSString* highLightImageName = itemView.menuItem.highLightImageName;
|
||||
if (imageName) {
|
||||
[itemView.buton setBackgroundImage:UMComImageWithImageName(imageName) forState:UIControlStateNormal];
|
||||
}
|
||||
if (highLightImageName) {
|
||||
[itemView.buton setBackgroundImage:UMComImageWithImageName(highLightImageName) forState:UIControlStateHighlighted];
|
||||
itemView.buton.imageView.contentMode = UIViewContentModeScaleAspectFit;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
- (void)menuItemView:(UMComMenuItemView *)menuItemView index:(NSInteger)index
|
||||
{
|
||||
if (self.isHighLightWhenDidSelected == YES) {
|
||||
if (self.selectedIndex == index) {
|
||||
[menuItemView highLight];
|
||||
}else{
|
||||
[menuItemView nomal];
|
||||
}
|
||||
}else{
|
||||
[menuItemView nomal];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)setMenuItemViewBlock:(UMComMenuItemView *)menuItemView
|
||||
{
|
||||
__weak typeof(self) weakSelf = self;
|
||||
menuItemView.clickBlock = ^(UMComMenuItemView *menuItemView,NSInteger index){
|
||||
__weak typeof(weakSelf) strongSelf = self;
|
||||
weakSelf.lastIndex = weakSelf.selectedIndex;
|
||||
weakSelf.selectedIndex = index;
|
||||
if (weakSelf.isHighLightWhenDidSelected == YES) {
|
||||
[weakSelf reloadAnyTypeOfMenuItems];
|
||||
}
|
||||
[weakSelf scrollViewScrollToPageMenuItemView:menuItemView];
|
||||
if (weakSelf.selectedAtIndex) {
|
||||
weakSelf.selectedAtIndex(strongSelf, index);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
- (void)scrollViewScrollToPageMenuItemView:(UMComMenuItemView *)menuItemView
|
||||
{
|
||||
__weak typeof(self) weakSelf = self;
|
||||
[UIView animateWithDuration:0.25 animations:^{
|
||||
weakSelf.scrollIndicatorView.center = CGPointMake(menuItemView.center.x, weakSelf.scrollIndicatorView.center.y);
|
||||
}];
|
||||
}
|
||||
|
||||
/*
|
||||
// Only override drawRect: if you perform custom drawing.
|
||||
// An empty implementation adversely affects performance during animation.
|
||||
- (void)drawRect:(CGRect)rect {
|
||||
// Drawing code
|
||||
}
|
||||
*/
|
||||
|
||||
@end
|
||||
|
||||
|
||||
@implementation UMComMenuItem
|
||||
|
||||
- (instancetype)init
|
||||
{
|
||||
self = [super init];
|
||||
if (self) {
|
||||
self.textCorlor = [UIColor blackColor];
|
||||
self.highTextColor = [UIColor redColor];
|
||||
self.bgColor = [UIColor clearColor];
|
||||
self.highLightBgColor = [UIColor blueColor];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
+ (UMComMenuItem *)itemWithTitle:(NSString *)title
|
||||
imageName:(NSString *)imageName
|
||||
{
|
||||
UMComMenuItem *item = [[UMComMenuItem alloc]init];
|
||||
item.title = title;
|
||||
item.imageName = imageName;
|
||||
return item;
|
||||
}
|
||||
|
||||
+ (UMComMenuItem *)itemWithTitle:(NSString *)title
|
||||
imageName:(NSString *)name
|
||||
highLightType:(HighLightType)highLightType
|
||||
itemViewType:(UMComMenuItemViewType)itemViewType
|
||||
{
|
||||
UMComMenuItem *menuItem = [UMComMenuItem itemWithTitle:title imageName:name];
|
||||
menuItem.highLightType = highLightType;
|
||||
menuItem.itemViewType = itemViewType;
|
||||
return menuItem;
|
||||
}
|
||||
|
||||
+ (UMComMenuItem *)itemWithTitle:(NSString *)title
|
||||
imageName:(NSString *)name
|
||||
highLightImageName:(NSString *)highLightImageName
|
||||
{
|
||||
UMComMenuItem *item = [UMComMenuItem itemWithTitle:title imageName:name];
|
||||
item.highLightImageName = highLightImageName;
|
||||
return item;
|
||||
}
|
||||
|
||||
+ (UMComMenuItem *)itemWithTitle:(NSString *)title
|
||||
imageName:(NSString *)name
|
||||
highLightTitle:(NSString *)highLightTitle
|
||||
highLightImageName:(NSString *)highLightImageName
|
||||
{
|
||||
UMComMenuItem *item = [UMComMenuItem itemWithTitle:title imageName:name];
|
||||
item.highLightTitle = highLightTitle;
|
||||
item.highLightImageName = highLightImageName;
|
||||
return item;
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
|
||||
|
||||
@implementation UMComMenuItemView
|
||||
|
||||
- (instancetype)initWithFrame:(CGRect)frame
|
||||
{
|
||||
self = [super initWithFrame:frame];
|
||||
if (self) {
|
||||
|
||||
_imageView = [[UIImageView alloc] initWithFrame:frame];
|
||||
_imageView.contentMode = UIViewContentModeScaleAspectFit;
|
||||
_imageView.backgroundColor = [UIColor clearColor];
|
||||
[self addSubview:_imageView];
|
||||
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(didClickOnItem:)];
|
||||
[self addGestureRecognizer:tap];
|
||||
_titleLabel = [[UILabel alloc]initWithFrame:frame];
|
||||
_titleLabel.font = UMComFontNotoSansLightWithSafeSize(14);
|
||||
_titleLabel.backgroundColor = [UIColor clearColor];
|
||||
_titleLabel.textAlignment = NSTextAlignmentCenter;
|
||||
[self addSubview:_titleLabel];
|
||||
|
||||
_buton = [UIButton buttonWithType:UIButtonTypeCustom];
|
||||
_buton.imageView.contentMode = UIViewContentModeScaleAspectFit;
|
||||
_buton.frame = frame;
|
||||
_buton.backgroundColor = [UIColor clearColor];
|
||||
[_buton addTarget:self action:@selector(didClickOnItem:) forControlEvents:UIControlEventTouchUpInside];
|
||||
[self addSubview:_buton];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
+ (UMComMenuItemView *)menuItemViewWithMenuItem:(UMComMenuItem *)menuItem
|
||||
{
|
||||
UMComMenuItemView *menuItemView = [[UMComMenuItemView alloc]initWithFrame:CGRectMake(0, 0, 1 , 1)];
|
||||
|
||||
return menuItemView;
|
||||
}
|
||||
|
||||
- (void)reloadWithTitle:(NSString *)title
|
||||
image:(UIImage *)image
|
||||
index:(NSInteger)index
|
||||
{
|
||||
self.index = index;
|
||||
self.titleLabel.text = title;
|
||||
self.imageView.image = image;
|
||||
}
|
||||
|
||||
- (void)reloadWithTitle:(NSString *)title
|
||||
image:(UIImage *)image
|
||||
{
|
||||
if (title) {
|
||||
self.titleLabel.text = title;
|
||||
}
|
||||
if (image) {
|
||||
self.imageView.image = image;
|
||||
}
|
||||
}
|
||||
|
||||
- (void)highLight
|
||||
{
|
||||
UMComMenuItem *item = self.menuItem;
|
||||
if (item.highLightType == HighLightText) {
|
||||
self.titleLabel.text = item.highLightTitle;
|
||||
}else if (item.highLightType == HighLightTextColor){
|
||||
self.titleLabel.textColor = item.highTextColor;
|
||||
self.titleLabel.text = item.title;
|
||||
self.backgroundColor = item.bgColor;
|
||||
}else if (item.highLightType == HighLightImage){
|
||||
self.titleLabel.text = item.title;
|
||||
self.imageView.image = UMComImageWithImageName(item.highLightImageName);
|
||||
}else if (item.highLightType == HighLightBgColor){
|
||||
self.titleLabel.text = item.title;
|
||||
self.backgroundColor = item.highLightBgColor;
|
||||
}
|
||||
}
|
||||
|
||||
- (void)nomal
|
||||
{
|
||||
UMComMenuItem *item = self.menuItem;
|
||||
self.titleLabel.text = item.title;
|
||||
self.titleLabel.textColor = item.textCorlor;
|
||||
self.imageView.image = UMComImageWithImageName(item.imageName);
|
||||
self.backgroundColor = item.bgColor;
|
||||
}
|
||||
|
||||
- (void)didClickOnItem:(id)item
|
||||
{
|
||||
if (self.clickBlock) {
|
||||
self.clickBlock(self,self.index);
|
||||
}
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
Reference in New Issue
Block a user