iOS使用UIBezierPath实现ProgressView

实现效果如下:

界面采用UITableView和TabelViewCell的实现,红色的视图采用UIBezierPath绘制.注意红色的部分左上角,左下角是直角哟!!!!不多说<这里才是用UIBezierPath实现的真正愿意啦!!!??>,代码如下:

控制器代码:

//
//  ViewController.m
//  ProgressViewDemo
//
//  Created by 思 彭 on 2017/4/20.
//  Copyright ? 2017年 思 彭. All rights reserved.
//

#import "ViewController.h"
#import "ProgressTableViewCell.h"

@interface ViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) CAShapeLayer *layer;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"ProgressDemo";
    [self setUI];
}

#pragma mark - 设置界面

- (void)setUI {

    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor clearColor];
    // 注册cell
    [self.tableView registerClass:[ProgressTableViewCell class] forCellReuseIdentifier:@"cell"];
    self.tableView.tableFooterView = [[UIView alloc]init];
    [self.view addSubview:self.tableView];
}

#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return 5;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    ProgressTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    return cell;
}

#pragma mark - UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {

    return 0.001f;;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {

    return 0.0001f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    return 44;
}

@end

TabelViewCell代码:

//
//  ProgressTableViewCell.m
//  ProgressViewDemo
//
//  Created by 思 彭 on 2017/4/21.
//  Copyright ? 2017年 思 彭. All rights reserved.
//

#import "ProgressTableViewCell.h"
#import "Masonry.h"
#import "ProgressView.h"

@interface ProgressTableViewCell ()

@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) ProgressView *progressView;
@property (nonatomic, strong) UILabel *numberLabel;

@end

@implementation ProgressTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {

    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self createSubViews];
        [self layOut];
    }
    return self;
}

- (void)createSubViews {

    self.titleLabel = [[UILabel alloc]init];
    self.titleLabel.font = [UIFont systemFontOfSize:16];
    self.titleLabel.text = @"西单大悦城";
    self.titleLabel.textAlignment = NSTextAlignmentLeft;
    [self.contentView addSubview:self.titleLabel];
    self.progressView = [[ProgressView alloc]init];
    self.progressView.backgroundColor = [UIColor whiteColor];
    self.progressView.progress = arc4random_uniform(100) + 40;
    [self.contentView addSubview:self.progressView];
    self.numberLabel = [[UILabel alloc]init];
    self.numberLabel.font = [UIFont systemFontOfSize:16];
    self.numberLabel.text = @"¥2000";
    self.numberLabel.textAlignment = NSTextAlignmentRight;
    [self.contentView addSubview:self.numberLabel];
}

- (void)layOut {

    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.contentView).offset(10);
        make.centerY.mas_equalTo(self.contentView);
//        make.width.greaterThanOrEqualTo(@(70));
        make.width.mas_equalTo(self.contentView.frame.size.width * 0.3);
    }];
    [self.progressView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.titleLabel.mas_right).offset(10);
        make.height.mas_equalTo(20);
        make.centerY.mas_equalTo(self.titleLabel.mas_centerY);
        make.width.mas_equalTo(self.contentView.frame.size.width * 0.4);
    }];
    [self.numberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.progressView.mas_right).offset(10);
        make.centerY.mas_equalTo(self.contentView);
        make.right.mas_equalTo(self.contentView).offset(-10);
    }];
}

@end

ProgressView代码:

//
//  ProgressView.m
//  ProgressViewDemo
//
//  Created by 思 彭 on 2017/4/20.
//  Copyright ? 2017年 思 彭. All rights reserved.
//

#import "ProgressView.h"

@interface ProgressView ()

@end

@implementation ProgressView

-(void)drawRect:(CGRect)rect{

    // 创建贝瑟尔路径

    /*
    CGFloat width = self.progress / rect.size.width * rect.size.width;
    // 显示的宽度 = 服务器返回的数值 / 设置的总宽度 * 满值;

     显示的宽度= 满值 * 比例值
     比例值 = 服务器返回的宽度 / 满值
     */

    CGFloat width = rect.size.width * self.progress / rect.size.width;
     // 显示的宽度 = 服务器返回的数值 * 设置的总宽度 /  满值;
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, width, rect.size.height) byRoundingCorners:UIRectCornerTopRight|UIRectCornerBottomRight cornerRadii:CGSizeMake(rect.size.height, rect.size.height)];
    [[UIColor redColor] setFill];
    [path fill];
}

- (void)setProgress:(CGFloat)progress{

    _progress = progress;
    // 重绘,系统会先创建与view相关联的上下文,然后再调用drawRect
    [self setNeedsDisplay];
}

@end

是不是超级简单,可是我一开始还是没有想到,还是自己太菜..??...奋斗吧!!!

时间: 2024-08-12 06:35:45

iOS使用UIBezierPath实现ProgressView的相关文章

iOS - 用 UIBezierPath 实现果冻效果

最近在网上看到一个很酷的下拉刷新效果(http://iostuts.io/2015/10/17/elastic-bounce-using-uibezierpath-and-pan-gesture/).自己试着实现了一下其中的果冻回弹效果. 效果 DEMO 由于文笔不太好-.- ,建议先下载demo,再结合下面的分析,会好理解点.地址https://github.com/Resory/RYCuteView 逻辑 下图p1,蓝色部分图形是一个CAShapeLayer,他的形状由UIBezierPat

iOS 使用UIBezierPath类实现随手画画板

这篇文章介绍一下如何通过这个类实现一个简单的随手画画板的简单程序demo,功能包括:划线(可以调整线条粗细,颜色),撤销笔画,回撤笔画,清除画布,橡皮擦.当然也可以扩展其他的功能. 一.首先看看实现划线部分的关键代码吧! 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 - (void) drawRect: (CGRect) rect {

iOS绘图—— UIBezierPath 和 Core Graphics

前言 iOS系统本身提供了两套绘图的框架,即UIBezierPath 和 Core Graphics.而前者所属UIKit,其实是对Core Graphics框架关于path的进一步封装,所以使用起来比较简单.但是毕竟Core Graphics更接近底层,所以它更加强大. UIBezierPath 可以创建基于矢量的路径,例如椭圆或者矩形,或者有多个直线和曲线段组成的形状. 使用UIBezierPath,你只能在当前上下文中绘图,所以如果你当前处于UIGraphicsBeginImageCont

iOS 使用UIBezierPath和CAShapeLayer画各种图形

CAShapeLayer 是 CALayer 的子类,但是比 CALayer 更灵活,可以画出各种图形,当然,你也可以使用其他方式来画,随你. 杂谈 在 CAShapeLayer 中,也可以像 CALayer 一样指定它的 frame 来画,就像这样: let layer = CAShapeLayer() layer.frame = CGRectMake(110, 100, 150, 100) layer.backgroundColor = UIColor.blackColor().CGColo

CGContextRef&amp;CGMutablePathRef&amp;UIBezierPath简单学习

简单的四句介绍 Quartz是一个二维绘图引擎,使用的是CoreGraphics库,同时支持iOS和Mac系统 CGContextRef:获取图形上下文.或者叫作用域,即画布,他是专门用来保存绘画期间的各种数据的 UIBezierPath是对CGPathRef的封装.创建矢量图形时,拆解成一条或者多条线段,拼接起来,每条下端的终点都是下一条线段的起点 当我们绘制路径时,Path的信息就会被Graphics context重置. 如果我们想要保存path信息,并多次使用它,我们就可以用到CGPat

IOS CAShapeLayer CAGradientLayer UIBezierPath 使用实例

CGRect rect = CGRectMake(100, 100, 100, 100); UIView * bgView = [[UIView alloc]initWithFrame:rect]; bgView.backgroundColor = [UIColor grayColor]; [self.view addSubview:bgView]; CAShapeLayer * trackLayer = [CAShapeLayer layer]; trackLayer.frame = bgVi

iOS progressview的简单使用

head 代码.. Java代码   #import <UIKit/UIKit.h> @interface ViewController : UIViewController { UIProgressView *progressview; UIProgressView *progressviewbar; NSTimer *timer; } @property(retain,nonatomic) IBOutlet UIProgressView *progressview; @property(r

iOS 开发 Quartz 2D+ UIBezierPath绘图大全详解

Quartz 2D 使用大全结构图 UIKIt UIBezierPath Core Graphics OpenGL ES Quartz2D的区别和联系 UIKIt:UIKit中的控件都是基于Core Graphics实现的 UIBezierPath:UIBezierPath属于UIKit,它是苹果对复杂的Core Graphics进行的封装,方便我们用OC语言进行简单的绘图 Core Graphics:是一套基于C语言的API,支持向量图形,线.形状.图案.路径.剃度.位图图像和pdf 内容的绘

IOS Animation-CAShapeLayer、UIBezierPath与Animation的结合

在阅读本文之前,对CAShapeLayer.UIBezierPath不熟悉的话,可以先阅读文章 贝塞尔曲线与Layer 如果对动画不熟悉的话,先阅读文章 动画基础.深入 Layer是绘图的画板,Bezier是画图的画笔,Animation是画图的动作.现在我们可以通过下面例子更好的让它们更好地结合在一起. 1)画一个小屋子动画 我们首先通过定义CAShapeLayer画板,然后定义path来确定画图路径.最后使用动画.如下面代码 1 //让一个屋子的线画起来的动画效果 2 func addCAS