iOS 进度条,可选择环形或者直线,有无动画

自己写的一个环形进度条,可以已圆或者直线的形式展示,有无动画,编写简洁,注释明确,不愿意读码的同学可以直接创建一个类,copy .h .m即可。


.h

#define SELECKED_COLOR [UIColor colorWithRed:2/255.0 green:126/255.0 blue:198/255.0 alpha:1]


#import <UIKit/UIKit.h>


@interface WYSegmentControl : UIControl


// UIControl 继承与UIView 

@property(nonatomic,assign) NSInteger selectedIndex;



-(id)initWithItems:(NSArray*)items WithNormalImage:(UIImage*)normalImage WithSelectImage:(UIImage*)selectImage WithFrame:(CGRect)rect hideScrollImg:(BOOL)isHide;


-(void)setSegmentHeight:(CGFloat)height;


-(void)SetSelectedSegmentIndex:(NSInteger)index;


/**

 *  设置按钮title

 *

 *  @param title title

 *  @param index 索引

 */

- (void)setSelectedSegmentTitle:(NSString *)title Index:(NSInteger)index;



@end


.m

#import "WYSegmentControl.h"


@interface WYSegmentControl()

{

    UIView *_v_scroll; // 滚动视图

    CGFloat _width_scroll; // 滚动视图宽度

    CGFloat _height_scroll;

    UIScrollView *_bgScrollView; // 背景滑动视图

    NSMutableArray *_arr_items; // 按钮

    

    UIImage *_normalImage;

    UIImage *_selectImage;

    CGFloat _btnWidth; // 按钮宽度

    BOOL _isHideScrollView; // 是否隐藏滑动条

    

}

@property(nonatomic,assign) CGFloat segheight;


@end


@implementation WYSegmentControl



-(id)initWithItems:(NSArray*)items WithNormalImage:(UIImage*)normalImage WithSelectImage:(UIImage*)selectImage WithFrame:(CGRect)rect hideScrollImg:(BOOL)isHide{

    

    if (self = [super initWithFrame:rect]) {

        

        

        _isHideScrollView = isHide;

        

        _normalImage = normalImage;

        _selectImage = selectImage;

        _btnWidth = self.frame.size.width / 4;

        

        if (items.count <= 3) {

            

            _btnWidth = rect.size.width / items.count;

        }

        

        // 按钮items

        [self setItems:items];

    }

    

    return self;

}

- (void)setSelectedSegmentTitle:(NSString *)title Index:(NSInteger)index

{

    for (UIButton* btn in _arr_items) {

        

        if (btn.tag == index){

            [btn setTitle:title forState:UIControlStateNormal];

        }

    }

}

- (void)setItems:(NSArray *)array

{

    for (UIView *subView in [self subviews]) {

        

        [subView removeFromSuperview];

    }

    

    NSInteger count = [array count];

    CGFloat width_singe = _btnWidth;

    CGFloat height = 40.0f;

    

    // 背景scrollView

    _bgScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width , 40)];

    _bgScrollView.showsHorizontalScrollIndicator = NO;

    _bgScrollView.contentSize = CGSizeMake(count * width_singe, 40);

    [self addSubview:_bgScrollView];

    

    _arr_items = [NSMutableArray array];

    [_arr_items removeAllObjects];

    for ( int i = 0; i< count; i++) {

        

        UIButton* btn = [[UIButton alloc] initWithFrame:CGRectMake(width_singe * i, 0, width_singe, height)];

        btn.tag = i;

        [btn setBackgroundImage:_normalImage forState:UIControlStateNormal];

        [btn setBackgroundImage:_selectImage forState:UIControlStateHighlighted];

        [btn setBackgroundImage:_selectImage forState:UIControlStateSelected];

        btn.titleLabel.font = [UIFont systemFontOfSize:14];

        [btn setTitle:[array objectAtIndex:i] forState:UIControlStateNormal];

        [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];

        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];

        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateSelected];

        [btn addTarget:self action:@selector(changeShow:) forControlEvents:UIControlEventTouchUpInside];

        [_bgScrollView addSubview:btn];

        [_arr_items addObject:btn];

    }

    _width_scroll = width_singe;

    


  

    if (!_isHideScrollView) {

        

        // 滑动条

        CGFloat v_height = 2;

        _height_scroll = v_height;

        _v_scroll = [[UIView alloc] initWithFrame:CGRectMake(0, 40 - v_height, width_singe, v_height)];

        [_bgScrollView addSubview:_v_scroll];

        UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width_singe, v_height)];

            

            line.backgroundColor = [UIColor redColor];

        [_v_scroll addSubview:line];

    } else {

        

        // 竖线

        NSInteger num = _arr_items.count;

        for ( int i = 0 ; i< num - 1; i++) {

            UILabel * lab_line = [[UILabel alloc] initWithFrame:CGRectMake(self.frame.size.width/num + (self.frame.size.width/num)*i,0,0.5,40)];

            lab_line.backgroundColor = [UIColor lightGrayColor];

            [self addSubview:lab_line];

        }

    

    }

    // 横线

    UILabel * lab_line = [[UILabel alloc] initWithFrame:CGRectMake(0,39.5,self.frame.size.width,0.5)];

    lab_line.backgroundColor = [UIColor lightGrayColor];

    [self addSubview:lab_line];

    [self bringSubviewToFront:lab_line];

    

}

// 重新设置每一个控件的高度

-(void)setSegmentHeight:(CGFloat)height{

    

    CGFloat width_singe = _btnWidth;

    for (UIButton* btn in _arr_items) {

        NSInteger tag = btn.tag;

        btn.frame = CGRectMake(width_singe*tag,0, width_singe, height);

    }

}


// 设置某一个被选中

-(void)SetSelectedSegmentIndex:(NSInteger)index{

    

    for (UIButton* btn in _arr_items) {

        

        if (btn.tag == index){

            btn.selected = YES;

            _selectedIndex = index;

            

        }else{

            btn.selected = NO;

        }

    }

}

// 被点击时 

-(void)changeShow:(id)sender{

    

    UIButton* btn = sender;

    _selectedIndex = btn.tag;

        

    if (btn) {

        [self startAnimation:btn animated:NO];

    }

    

    for (UIButton* btn in _arr_items) {

        btn.selected = NO;

    }

    

    btn.selected = YES;

    

    [self sendActionsForControlEvents:UIControlEventValueChanged];

}



- (void)startAnimation:(UIButton *)btn animated:(BOOL)animated

{

    if (animated) {

        

        __weak UIButton *weakBtn = btn;

        __weak UIView *weakSview = _v_scroll;

        [UIView animateWithDuration:0.2 animations:^{

            weakBtn.transform = CGAffineTransformMakeScale(0.5, 0.5);

            [weakSview setFrame:CGRectMake(_selectedIndex * _width_scroll, 40 - _height_scroll, _width_scroll, _height_scroll)];

            

        } completion:^(BOOL finished) {

            

            [UIView animateWithDuration:0.25 animations:^{

                

                weakBtn.transform = CGAffineTransformMakeScale(1.2, 1.2);

                

            } completion:^(BOOL finished) {

                

                weakBtn.transform = CGAffineTransformMakeScale(1, 1);

            }];

        }];

    } else {

        

        [_v_scroll setFrame:CGRectMake(_selectedIndex * _width_scroll, 40 - _height_scroll, _width_scroll, _height_scroll)];

        

    }

}

@end

编写仓促,有问题的地方可以留言,望共同进度



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值