123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287 |
- //
- // MenuSegmentView.m
- // DDTG
- //
- // Created by 翟玉磊 on 16/5/11.
- // Copyright © 2016年 翟玉磊. All rights reserved.
- //
- #define DEFAULT_DURATION 0.3f //动画时间
- #import "MenuSegmentView.h"
- @interface MenuSegmentView () {
-
- NSMutableArray *buttonArray;
-
- NSMutableArray *badegArray;
- }
- @property (nonatomic, assign) CGFloat viewWidth;
- @property (nonatomic, assign) CGFloat viewHeight;
- @property (nonatomic, assign) CGFloat labelWidth;
- @property (nonatomic, assign) NSInteger selectIndex;
- @property (nonatomic, strong) UIView *heightLightView;
- @property (nonatomic, strong) UIView *heightTopView;
- @property (nonatomic, strong) UIView *heightColorView;
- @property (nonatomic, readwrite, strong) UIView *bottomLine;
- @property (nonatomic, strong) ButtonOnClickBlock buttonBlock;
- @property (nonatomic, strong) SelectIndexBlock selectIndexBlock;
- @end
- @implementation MenuSegmentView
- - (instancetype)initWithFrame:(CGRect)frame titles:(NSArray *)array {
-
- if (self = [super initWithFrame:frame]) {
-
- _titles = array;
- _viewHeight = self.f_heigh;
- _viewWidth = self.f_width;
- _duration = DEFAULT_DURATION;
- [self initValue];
- }
- return self;
- }
- - (void)initValue {
-
- // 初始化数据
- self.selectIndex = 0;
- _isShowBottomLine = YES;
- _selectedLineHeight = 2;
- buttonArray = [NSMutableArray array];
-
- [self customeData];
-
- [self createBottomLabels];
- [self createTopLabels];
- [self createTopButtons];
- [self createBadgeViews];
- }
- - (void)setSelectedItemIndex:(int)page {
-
- [self selectedAnimation:page];
- }
- //更新titles
- - (void)updateTitlesWithData:(NSArray *)array {
-
- _titles = [NSArray arrayWithArray:[array copy]];
-
- // 删除子视图
- for (UIView *view in self.subviews) {
- [view removeFromSuperview];
- }
-
- // 创建
- [self initValue];
- }
- - (void)setIsShowBottomLine:(BOOL)isShowBottomLine {
- _isShowBottomLine = isShowBottomLine;
- if (_isShowBottomLine) {
- _bottomLine.hidden = NO;
- }else {
- _bottomLine.hidden = YES;
- }
- }
- - (void)setSelectedLineHeight:(CGFloat)selectedLineHeight {
- _selectedLineHeight = selectedLineHeight;
- if (_heightTopView) {
- _heightColorView.frame = CGRectMake(_heightColorView.f_x, self.f_heigh - selectedLineHeight, _heightColorView.f_width, selectedLineHeight);
- [_heightColorView addViewBorder:Color_Clear redian:_selectedLineHeight/2];
- }
- }
- /**
- 显示或者隐藏指定索引的红点
- @param index 索引
- @param status YES显示 NO隐藏
- */
- - (void)updateBadgeWithIndex:(NSInteger)index status:(BOOL)status {
- for (NSInteger i = 0; i < badegArray.count; i++) {
- if (i == index) {
- UIView *badge = [badegArray objectAtIndex:i];
- badge.hidden = !status;
- break;
- }
- }
- }
- - (void)setTitleFont:(UIFont *)titleFont {
-
- }
- //回调
- - (void)setButtonOnClickBlock:(ButtonOnClickBlock) block {
- if (block) {
- _buttonBlock = block;
- }
- }
- /**
- * 提供默认信息
- */
- - (void)customeData {
-
- _titleDefaultColor = Color_TextFont;
- _titleSelectColor = Color_Button_Select;
- _titleFont = Font_B(18);
- _labelWidth = _viewWidth / _titles.count;
- }
- /**
- * 创建底部层的label
- */
- - (void)createBottomLabels {
-
- for (int i = 0; i < _titles.count; i ++) {
- UILabel *tempLabel = [self createLabelWithTitlesIndex:i textColor:_titleDefaultColor];
- tempLabel.font = _titleFont;
- [self addSubview:tempLabel];
- }
-
- if (_bottomLine == nil) {
- _bottomLine = [UIView createLineRect:CGRectMake(0, _viewHeight - 0.5, _viewWidth, 0.5)];
- [self addSubview:_bottomLine];
- }
- }
- /**
- * 创建上层高亮使用的label
- */
- - (void)createTopLabels {
-
- CGRect heightLabelFrame = CGRectMake(0, 0, _labelWidth, _viewHeight);
- _heightLightView = [[UIView alloc] initWithFrame:heightLabelFrame];
- _heightLightView.clipsToBounds = YES;
-
- _heightColorView = [[UIView alloc] initWithFrame:CGRectMake(0, _viewHeight - 2, _labelWidth, _selectedLineHeight)];
- [_heightColorView addViewBorder:Color_Clear redian:_selectedLineHeight/2];
- _heightColorView.backgroundColor = _titleSelectColor;
- [_heightLightView addSubview:_heightColorView];
-
- _heightTopView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, _viewWidth, _viewHeight)];
- for (int i = 0; i < _titles.count; i ++) {
- UILabel *label = [self createLabelWithTitlesIndex:i textColor:_titleSelectColor];
- [_heightTopView addSubview:label];
- }
- [_heightLightView addSubview:_heightTopView];
- [self addSubview:_heightLightView];
- }
- - (void)createBadgeViews {
- badegArray = [NSMutableArray array];
- for (int i = 0; i < _titles.count; i ++) {
- CGFloat titleWidth = [HandleString lableWidth:_titles[i] withSize:CGSizeMake(MAXFLOAT, _viewHeight) withFont:_titleFont];
- CGFloat x = (_labelWidth - titleWidth) / 2 + titleWidth + 5.0f;
- UIView *badgeView = [[UIView alloc] initWithFrame:CGRectMake(x + _labelWidth * i, 10.0f, 5, 5)];
- badgeView.tag = i;
- [badgeView setBackgroundColor:[UIColor redColor]];
- badgeView.hidden = YES;
- [badgeView addViewBorder:Color_Clear redian:2.5];
- [self addSubview:badgeView];
- [badegArray addObject:badgeView];
- }
- }
- /**
- * 创建按钮
- */
- - (void)createTopButtons {
- buttonArray = [NSMutableArray new];
- for (int i = 0; i < _titles.count; i ++) {
- CGRect tempFrame = [self countCurrentRectWithIndex:i];
- UIButton *tempButton = [[UIButton alloc] initWithFrame:tempFrame];
- tempButton.tag = i;
- [tempButton setBackgroundColor:[UIColor clearColor]];
- [tempButton addTarget:self action:@selector(tapButton:) forControlEvents:UIControlEventTouchUpInside];
- [self addSubview:tempButton];
- [buttonArray addObject:tempButton];
- }
- }
- //点击事件
- - (void)tapButton:(UIButton *)sender {
- if (_buttonBlock && sender.tag < _titles.count) {
- _buttonBlock(sender.tag, _titles[sender.tag]);
- }
- // [self selectedAnimation:sender.tag];
- }
- - (void)selectedAnimation:(NSInteger)page {
-
- if (_selectIndex == page) {
- return;
- }
- _selectIndex = page;
- if (_selectIndexBlock) {
- _selectIndexBlock(_selectIndex);
- }
- CGRect frame = [self countCurrentRectWithIndex:page];
- CGRect changeFrame = [self countCurrentRectWithIndex:-page];
-
- __weak typeof(self) weak_self = self;
- [UIView animateWithDuration:_duration animations:^{
- self->_heightLightView.frame = frame;
- self->_heightTopView.frame = changeFrame;
- } completion:^(BOOL finished) {
- [weak_self shakeAnimationForView:self->_heightColorView];
- }];
- }
- - (CGRect)countCurrentRectWithIndex:(NSInteger)index {
-
- return CGRectMake(_labelWidth * index, 0, _labelWidth, _viewHeight);
- }
- /**
- * 感觉索引创建label
- *
- * @param index 创建的第几个index
- * @param textColor label字体颜色
- *
- * @return 返回创建好的label
- */
- - (UILabel *)createLabelWithTitlesIndex:(NSInteger)index textColor:(UIColor *)textColor {
-
- CGRect currentLabelFrame = [self countCurrentRectWithIndex:index];
- UILabel *label = [[UILabel alloc] initWithFrame:currentLabelFrame];
- label.text = _titles[index];
- label.textColor = textColor;
- label.font = _titleFont;
- label.tag = index + 10;
- label.minimumScaleFactor = 0.1f;
- label.textAlignment = NSTextAlignmentCenter;
- return label;
- }
- /**
- * 抖动效果
- *
- * @param view 要抖动的view
- */
- - (void)shakeAnimationForView:(UIView *) view {
- CALayer *viewLayer = view.layer;
- CGPoint position = viewLayer.position;
- CGPoint x = CGPointMake(position.x + 1, position.y);
- CGPoint y = CGPointMake(position.x - 1, position.y);
- CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
- [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault]];
- [animation setFromValue:[NSValue valueWithCGPoint:x]];
- [animation setToValue:[NSValue valueWithCGPoint:y]];
- [animation setAutoreverses:YES];
- [animation setDuration:.06];
- [animation setRepeatCount:3];
- [viewLayer addAnimation:animation forKey:nil];
- }
- @end
|