iOS使用UIBezierPath实现ProgressView

所属分类: 软件编程 / IOS 阅读数: 483
收藏 0 赞 0 分享

使用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

是不是超级简单。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

iOS开发笔记--详解UILabel的相关属性设置

这篇文章主要介绍了iOS开发笔记--详解UILabel的相关属性设置,对初学者具有一定的参考价值,有需要的可以了解一下。
收藏 0 赞 0 分享

iOS中获取系统相册中的图片实例

这篇文章主要介绍了iOS中获取系统相册中的图片实例,具有一定的参考价值没有需要的朋友可以了解一下。
收藏 0 赞 0 分享

iOS 检测网络状态的两种方法

一般有Reachability和AFNetworking监测两种方式,都是第三方的框架,下文逐一详细给大家讲解,感兴趣的朋友一起看看吧
收藏 0 赞 0 分享

IOS 性能优化中离屏渲染

本文主要介绍了IOS 性能优化中离屏渲染的资料,提供了几种方法讲解了优化,有需要的小伙伴可以参考下
收藏 0 赞 0 分享

iOS获取当前设备型号等信息(全)包含iPhone7和iPhone7P

这篇文章主要介绍了iOS获取当前设备型号设备信息的总结包含iPhone7和iPhone7P,包括ios7之前之后的获取方式,本文接的非常详细,具有参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

iOS实现爆炸的粒子效果示例代码

之前在网上看到了一个Android实现的爆炸效果,感觉非常不错,所以自己尝试用iOS来实现下效果,现在将实现的过程、原理以及遇到的问题分享给大家,有需要的朋友们可以参考借鉴,下面来一起看看吧。
收藏 0 赞 0 分享

iOS毕业设计之天气预报App

这篇文章主要为大家详细介绍了iOS毕业设计之天气预报App,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

iOS轻点、触摸和手势代码开发

这篇文章主要为大家详细介绍了iOS轻点、触摸和手势代码开发,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

iOS 实现多代理的方法及实例代码

这篇文章主要介绍了iOS 实现多代理的方法及实例代码的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

iOS文字渐变色效果的实现方法

在大家日常开发iOS的过程中,可能会遇到要实现文字渐变色的效果,这篇文章文章通过示例代码和详细的步骤介绍了如何利用iOS实现文字渐变色的效果,实现后的很不错,感兴趣的朋友们下面来一起看看吧。
收藏 0 赞 0 分享
查看更多