iOS 自定义进度条
经常会遇到需要自定义进度条的需要,那么使用以下的小demo可以实现
.m文件
#import “GGProgressView.h”
@interface GGProgressView()
{
UIView *_progressView;
float _progress;
float _width;
float _heigth;
}
@end
@implementation GGProgressView
-(instancetype)initWithFrame:(CGRect)frame
{
return [self initWithFrame:frame progressViewStyle:GGProgressViewStyleDefault];
}
- (instancetype)initWithFrame:(CGRect)frame progressViewStyle:(GGProgressViewStyle)style
{
if (self=[super initWithFrame:frame]) {
_progressView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, frame.size.height)];
_progress=0;
self.progressViewStyle=style;
[self addSubview:_progressView];
}
return self;
}
-(void)setProgressViewStyle:(GGProgressViewStyle)progressViewStyle
{
_progressViewStyle=progressViewStyle;
if (progressViewStyleGGProgressViewStyleTrackFillet) {
self.layer.masksToBounds=YES;
self.layer.cornerRadius=self.bounds.size.height/2;
}
else if (progressViewStyleGGProgressViewStyleAllFillet)
{
self.layer.masksToBounds=YES;
self.layer.cornerRadius=self.bounds.size.height/2;
_progressView.layer.cornerRadius=self.bounds.size.height/2;
}
}
-(void)setTrackTintColor:(UIColor *)trackTintColor
{
_trackTintColor=trackTintColor;
if (self.trackImage) {
}
else
{self.backgroundColor=trackTintColor;
}
}
-(void)setProgress:(float)progress
{
NSLog(@"progressprogress %lf %lf %lf %lf",progress,_width,_heigth,self.bounds.size.width );_progress=MIN(progress, 1);
_progressView.frame=CGRectMake(0, 0, self.bounds.size.width*_progress, self.bounds.size.height);
}
-(float)progress
{
return _progress;
}
-(void)setProgressViewStyle:(GGProgressViewStyle)style Frame:(CGRect)frame{
_progressView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, frame.size.height)];
_progress=0;
self.progressViewStyle=style;
[self addSubview:_progressView];
}
-(void)setHeigth:(float)heigth
{
_heigth = heigth;
}
-(float)heigth
{
return _heigth;
}
-(void)setWidth:(float)width
{
_width= width;
}
-(float)width
{
return _width;
}
-(void)setProgressTintColor:(UIColor *)progressTintColor
{
_progressTintColor=progressTintColor;
_progressView.backgroundColor=progressTintColor;
}
-(void)setTrackImage:(UIImage *)trackImage
{
_trackImage=trackImage;
if(self.isTile)
{
self.backgroundColor=[UIColor colorWithPatternImage:trackImage];
}
else
{
self.backgroundColor=[UIColor colorWithPatternImage:[self stretchableWithImage:trackImage]];
}
}
-(void)setIsTile:(BOOL)isTile
{
_isTile = isTile;
if (self.progressImage) {
[self setProgressImage:self.progressImage];
}
if (self.trackImage) {
[self setTrackImage:self.trackImage];
}
}
-(void)setProgressImage:(UIImage *)progressImage
{
_progressImage = progressImage;
if(self.isTile)
{
_progressView.backgroundColor=[UIColor colorWithPatternImage:progressImage];
}
else
{
_progressView.backgroundColor=[UIColor colorWithPatternImage:[self stretchableWithImage:progressImage]];
}
}
- (UIImage )stretchableWithImage:(UIImage )image{
UIGraphicsBeginImageContextWithOptions(self.frame.size, NO, 0.f);
[image drawInRect:self.bounds];
UIImage lastImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return lastImage;
}
@end
//*********************************
.h文件
#import <UIKit/UIKit.h>
typedef NS_ENUM(NSInteger, GGProgressViewStyle) {
GGProgressViewStyleDefault, // 默认
GGProgressViewStyleTrackFillet , // 轨道圆角(默认半圆)
GGProgressViewStyleAllFillet, //进度与轨道都圆角
};
@interface GGProgressView : UIView
@property(nonatomic) float progress; // 0.0 … 1.0, 默认0 超出为1.
@property(nonatomic) GGProgressViewStyle progressViewStyle;
@property(nonatomic,assign) BOOL isTile; //背景图片是平铺填充 默认NO拉伸填充 设置为YES时图片复制平铺填充
@property(nonatomic, strong, nullable) UIColor* progressTintColor;
@property(nonatomic, strong, nullable) UIColor* trackTintColor;
@property(nonatomic, strong, nullable) UIImage* progressImage; //进度条背景图片,默认拉伸填充 优先级大于背景色
@property(nonatomic, strong, nullable) UIImage* trackImage; //轨道填充图片
@property(nonatomic) float heigth;
@property(nonatomic) float width;
- (instancetype)initWithFrame:(CGRect)frame;
- (instancetype)initWithFrame:(CGRect)frame progressViewStyle:(GGProgressViewStyle)style;
-(void)setProgressViewStyle:(GGProgressViewStyle)style Frame:(CGRect)frame;
@end
引用:
//这个方法是基于使用Xib创建的情况下
@property (weak, nonatomic) IBOutlet GGProgressView *pushProgress;
[_pushProgress setProgressViewStyle:GGProgressViewStyleTrackFillet Frame:CGRectMake(0, 0, 74, 24)];
_pushProgress.progressTintColor= UIColorMakeWithHex(@"#FF514E") ;
_pushProgress.trackTintColor= UIColorMakeWithHex(@"#FFE0DF") ;;
_pushProgress.heigth = 24;
_pushProgress.width = 74;
_pushProgress.progress=0.5;
_pushProgress.clipsToBounds = YES;
_pushProgress.layer.cornerRadius = 12;
如果用代码创建的话就用
- (instancetype)initWithFrame:(CGRect)frame;
- (instancetype)initWithFrame:(CGRect)frame progressViewStyle:(GGProgressViewStyle)style;
其中之一进行init
iOS 自定义进度条相关推荐
- IOS开发基础之绘制饼图、柱状图、自定义进度条
IOS开发基础之绘制饼图.柱状图.自定义进度条 源码在我的主页里 1.绘制饼图 效果 源码 // LJView.m // 34-绘图饼图 // Created by 鲁军 on 2021/2/23. ...
- Android 各种自定义进度条Progressbar
Android 自定义进度条 Progressbar 控件集合 关于我,欢迎关注 博客:ccapton(http://blog.csdn.net/ccapton) 微信:Ccapton Github ...
- android自定义进度条渐变色View,不使用任何图片资源
最近在公司,项目不是很忙了,偶尔看见一个兄台在CSDN求助,帮忙要一个自定义的渐变色进度条,我当时看了一下进度条,感觉挺漂亮的,就尝试的去自定义view实现了一个,废话不说,先上图吧! 这个自 ...
- android 自定义 进度条 旋转,Android_Android ProgressBar进度条使用详解,ProgressBar进度条,分为旋转进 - phpStudy...
Android ProgressBar进度条使用详解 ProgressBar进度条,分为旋转进度条和水平进度条,进度条的样式根据需要自定义,之前一直不明白进度条如何在实际项目中使用,网上演示进度条的案 ...
- android ProgressBar 自定义进度条颜色
android 自定义进度条颜色 先看图 基于产品经理各种自定义需求,经过查阅了解,下面是自己对android自定义进度条的学习过程! 这个没法了只能看源码了,还好下载了源码, sources\bas ...
- android自定义进度条_Android中的自定义进度栏
android自定义进度条 Custom progress bar in android application gives it a personal touch. In this tutorial ...
- elementui自定义进度条形状
elementui自定义进度条形状 前言 一.我们看下进度条的结构 二.怎么改path里数据 1.了解下svg 2.其他svg标签转换成path 3.最关键的一步 前言 进度条多以圆形.直线以及仪表盘 ...
- 关于安卓自定义进度条(二)
先上gif效果图: 上图中,蓝色的进度条为自定义进度条 注意!!!源码在文末 背景 要实现一个进度条,大部分情况下,都是需要进行自定义的.因为原生就算你自定义了背景图片,但是一旦进度条的高度,超过了一 ...
- flutter 自定义进度条progress
系统自带进度条 有一定局限性,只能设置背景色,前景色要设置动画的颜色值,且不能自定义是否圆角等属性 // 2表示当前的值,10表示最大值 LinearProgressIndicator(value: ...
最新文章
- G - Shuffle‘m Up POJ - 3087
- 架构师课程1-多线程基础
- PyTorch深度学习实践02
- P5371-[SNOI2019]纸牌【矩阵乘法】
- 随机抽取题目程序java_分析并实现一个简单的抽人程序
- 如何在 Zabbix 执行远程主机的脚本或指令?如何用 Zabbix 监控网站的访问量?
- 18大产业的产业链全景图!(高清大图)
- JAVA分布式快速开发基础平台iBase4J
- 51单片机驱动数码管显示
- SpringBoot整合通用Mapper和PageHelper,使用PageHelper.startPage()失效的问题
- QQ界面的MSN聊天软件
- 元宇宙:从现实到虚无祛魅的产物|广州华锐互动
- [iOS]音视频呼叫时手机震动或者播放来电铃声
- React:一、初识react
- 10年后重温《我奋斗了18年才和你坐在一起喝咖啡》
- RmNet,CDC-ECM ,NDIS,RNDIS区别
- 腾讯视频qlv转mp4
- 代码随想录训练营day8
- 【AD封装】2.54间距排针排母插件贴片(带3D)
- 2021物联网开发学习——基于小熊派IoT开发板Bear-Pi-IOT、E53_IA1_智慧农业拓展板与小熊派-鸿蒙·季Bear-Pi-HM Nano并接入Hi-Link