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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章