欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

iOS 自定义进度条

程序员文章站 2022-05-26 19:30:03
...

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 (progressViewStyle
    GGProgressViewStyleAllFillet)
    {
    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
相关标签: 进度条