iOS 利用CoreGraphics绘制一个卸货的状态进度

效果图:

卸货流程图.gif

接触了一点CoreGraphics,项目中的卸货状态图,不想用view,就画一个 ,做个记录。

调用方法:

#import "ViewController.h"
#import "FTProgressView.h"

#define Device_Height [[UIScreen mainScreen] bounds].size.height
#define Device_Width  [[UIScreen mainScreen] bounds].size.width//获取屏幕宽高
@interface ViewController ()
@property(nonatomic,strong)FTProgressView *progressView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.progressView =[[FTProgressView alloc]initWithFrame:CGRectMake(0, 100, Device_Width, 62) titleArray:@[@"已接单",@"已装货",@"运输中",@"已卸货",@"已完成"]];
    self.progressView.backgroundColor =[UIColor whiteColor];
    [self.view addSubview:self.progressView];
    
    self.progressView.index = 2;
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    self.progressView.index = arc4random()%5;
    NSLog(@"%ld",self.progressView.index);
}

#####实现:
#import <UIKit/UIKit.h>

@interface FTProgressView : UIView
@property(nonatomic,assign)NSInteger index; //确定哪个状态
-(instancetype)initWithFrame:(CGRect)frame titleArray:(NSArray *)titleArray;

@end


#import "FTProgressView.h"
#import "UIColor+Expanded.h"
#define leftSpace 25
#define lineH 4
#define smallH 15
#define smallneiH 13
#define bigH 20
#define CGRect(f)  CGRectMake(leftSpace- f/2 + i * avaWidth, (height -f)/2, f, f)

@interface FTProgressView()
@property(nonatomic,assign)NSInteger statusNum;
@property(nonatomic,strong)NSArray *titleArray;
@property(nonatomic,strong)UIColor *color1;
@property(nonatomic,strong)UIColor *color2;

@end
@implementation FTProgressView

-(instancetype)initWithFrame:(CGRect)frame titleArray:(NSArray *)titleArray{
    self = [super initWithFrame:frame];
    if (self) {
        self.titleArray = titleArray;
    }
    return self;
}
-(void)setIndex:(NSInteger)index{
    _index = index;
    [self setNeedsDisplay];
}
- (void)drawRect:(CGRect)rect {
    CGFloat height = rect.size.height;
    CGFloat width = rect.size.width;

    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetFillColorWithColor(context, self.color1.CGColor);
    
    
    CGContextFillRect(context, CGRectMake(leftSpace, (height - lineH)/2, width-2 *leftSpace , lineH));
    [self drawCircle];
    
    [self drawLabel];
}
-(void)drawCircle
{
    CGFloat height = self.frame.size.height;
    CGFloat width = self.frame.size.width;
 
    CGFloat circleWidth = width - 2* leftSpace;
    CGFloat avaWidth = circleWidth /(self.titleArray.count -1);

    for (int i =0; i< self.titleArray.count; i++) {
        
        if (i == self.index) {

            [self FillPathWithRect:CGRect(bigH) fillColor:self.color1]; //绘制大圆
        }else{

            [self FillPathWithRect:CGRect(smallH) fillColor:self.color1];
            [self FillPathWithRect:CGRect(smallneiH) fillColor:self.color2];
        }
        
    }
    
}
-(void)FillPathWithRect:(CGRect) rect fillColor:(UIColor *)fillColor{
    
    CGContextRef context = UIGraphicsGetCurrentContext();

    
    CGContextSetFillColorWithColor(context, fillColor.CGColor);
    
    
    CGContextAddEllipseInRect(context, rect);
    
    CGContextFillPath(context);
    
    
}
-(void)drawLabel
{
    
    CGFloat height = self.frame.size.height;
    CGFloat width = self.frame.size.width;
    
     CGFloat circleWidth = width - 2* leftSpace;
    
    CGFloat avaWidth = circleWidth /(self.titleArray.count -1);
    
    for (int i = 0; i<self.titleArray.count; i++) {
        
        NSAttributedString *attStr =[[NSAttributedString alloc]initWithString:self.titleArray[i] attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12],NSForegroundColorAttributeName:[UIColor hexStringToColor:@"3c3c3c"]}];
        
        if (i < self.index) {
            CGPoint point = CGPointMake(leftSpace +avaWidth *i-attStr.size.width/2, height/2 -bigH/2-1 -attStr.size.height);
            [attStr drawAtPoint:point];
        }else{
            CGPoint point = CGPointMake(leftSpace +avaWidth *i-attStr.size.width/2, height/2 +bigH/2+1);
            [attStr drawAtPoint:point];

        }
        
    }
}

-(UIColor *)color1{
    if (!_color1) {
        _color1 =[UIColor hexStringToColor:@"393d63"];
    }
    return _color1;
}
-(UIColor *)color2{
    if (!_color2) {
        _color2 =[UIColor hexStringToColor:@"ffffff"];
    }
    return _color2;
}
@end

demo地址:卸货流程进度

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 176,800评论 25 709
  • 这周和往常一样,没什么特别的,除了我们去旅游,阅读基本达标,作业也完成的还好,但英语趣配音不知道为什么不能配了,换...
    牛涵葳阅读 1,513评论 0 1
  • “削肩细腰,长挑身材,鸭蛋脸面,俊眼修眉,顾盼神飞,文彩精华,见之忘俗”这是黛玉初进贾府时对探春的第一印象,...
    独有南山桂花发阅读 3,297评论 0 2
  • 王羲之作为书圣,在中国书坛熠熠生辉,从源头上影响着书法后学。 书圣之为圣,其余种种不提,单就其作品面貌而言可谓丰富...
    淮高刘溪专用阅读 3,680评论 0 1
  • 可靠性和可用性,看起来像,其实完全两回事。 一、概念 先看官方定义:可用性是产品在任意随机时刻需要和开始执行任务时...
    璇紫阅读 10,514评论 0 3

友情链接更多精彩内容