自定义个UIProgressView(可自己设置高度)

由于项目需求中的加载进度条的高度比系统原生的高度要小一些,于是乎就继承UIView自己写了个UIProgressView

 

@interface CustomProgressView : UIView

@property (nonatomic) CGFloat progress; //0.0 ~ 1.0

@property (nonatomic, retain) UIImage *trackImage;

@property (nonatomic, retain) UIImage *progressImage;

@property(nonatomicretainUIColor *progressTintColor;

@property(nonatomic, retain) UIColor *trackTintColor;



@end



@implementation CustomProgressView



- (id)initWithFrame:(CGRect)frame {

    self = [super initWithFrame:frame];

    if(self) {

        self.progressTintColor = nil;

        self.trackTintColor = nil;

        _progress = 0.0f;

    }

    returnself;

}



- (void)drawRect:(CGRect)rect {

    CGFloat width = self.frame.size.width;

    CGFloat height = self.frame.size.height;

    CGFloat radius = height / 2.0f;

    

    CGFloat fillPercent = _progress < 0.0 || _progress > 1.0 ? 0.0 : _progress;

    

    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSaveGState(context);

    

    CGMutablePathRef tarckPathRef = CGPathCreateMutable();

    CGPathAddArc(tarckPathRef, nil, radius, radius, radius, -M_PI_2, -(M_PI_2-M_PI), 1);

    CGPathAddLineToPoint(tarckPathRef, nil, width, height);

    CGPathAddArc(tarckPathRef, nil, width-radius, radius, radius, M_PI_2, -M_PI_21);

    CGPathAddLineToPoint(tarckPathRef, nil, radius, 0);

    CGPathCloseSubpath(tarckPathRef);

    

    CGMutablePathRef progressPathRef = NULL;

    CGFloat fillWidth = width * fillPercent;

    if (fillWidth > 0.0f) {

        progressPathRef = CGPathCreateMutable();

        CGPathAddArc(progressPathRef, nil, radius, radius, radius, -M_PI_2, -(M_PI_2-M_PI), 1);

        CGPathAddLineToPoint(progressPathRef, nil, fillWidth, height);

        CGPathAddArc(progressPathRef, nil, fillWidth-radius, radius, radius, M_PI_2, -M_PI_21);

        CGPathAddLineToPoint(progressPathRef, nil, radius, 0);

        CGPathCloseSubpath(progressPathRef);

    }

    

    CGContextAddPath(context, tarckPathRef);

    [self.trackTintColorsetFill];

    CGContextDrawPath(context, kCGPathFill);

    CGPathRelease(tarckPathRef);

    

    if (progressPathRef) {

        CGContextAddPath(context, progressPathRef);

        [self.progressTintColorsetFill];

        CGContextDrawPath(context, kCGPathFill);

        CGPathRelease(progressPathRef);

    }

    

    CGContextRestoreGState(context);

}



- (void)setProgress:(CGFloat)progress {

    _progress = progress;

    [selfsetNeedsDisplay];

}



- (void)setTrackImage:(UIImage *)trackImage {

    if(_trackImage != trackImage) {

        if(_trackImage) {

            [_trackImage release];

            _trackImage = nil;

        }

        

        _trackImage = [trackImage retain];

        CGFloat leftRightCaps = 3.0;

        UIEdgeInsets capInsets = UIEdgeInsetsMake(0, leftRightCaps, 0, leftRightCaps);

        UIImage *nmImage = _trackImage;

        if (SYSTEM_VERSION >= 6) {

            UIImageResizingMode resizingMode = UIImageResizingModeStretch;

            nmImage = [_trackImage resizableImageWithCapInsets:capInsets

                                                  resizingMode:resizingMode];

            

        } else if (SYSTEM_VERSION >= 5) {

            CGFloat topBottom = 2;

            capInsets.top = topBottom;

            capInsets.bottom = topBottom;

            nmImage = [_trackImage resizableImageWithCapInsets:capInsets];

        }

        nmImage = [self scaleToSize:nmImage size:CGSizeMake(1024.0self.frame.size.height)];

        self.trackTintColor = [UIColor colorWithPatternImage:nmImage];

        [selfsetNeedsDisplay];

    }

}



- (void)setProgressImage:(UIImage *)progressImage {

    if(_progressImage != progressImage) {

        if(_progressImage) {

            [_progressImage release];

            _progressImage = nil;

        }

        _progressImage = [progressImage retain];

        CGFloat leftRightCaps = 6.0;

        UIEdgeInsets capInsets = UIEdgeInsetsMake(0, leftRightCaps, 0, leftRightCaps);

        UIImage *onImage = _progressImage;

        if (SYSTEM_VERSION >= 6) {

            UIImageResizingMode resizingMode = UIImageResizingModeStretch;

            onImage = [_progressImage resizableImageWithCapInsets:capInsets

                                                     resizingMode:resizingMode];

            

        } else if (SYSTEM_VERSION >= 5) {

            CGFloat topBottom = 2;

            capInsets.top = topBottom;

            capInsets.bottom = topBottom;

            onImage = [_progressImage resizableImageWithCapInsets:capInsets];

        }

        onImage = [self scaleToSize:onImage size:CGSizeMake(1024.0self.frame.size.height)];

        self.progressTintColor = [UIColor colorWithPatternImage:onImage];

        [selfsetNeedsDisplay];

    }

}



- (void)setTrackTintColor:(UIColor *)trackTintColor {

    if(_trackTintColor != trackTintColor) {

        if(_trackTintColor) {

            [_trackTintColor release];

            _trackTintColor = nil;

        }

        _trackTintColor = [trackTintColor retain];

        if (!trackTintColor) {

            _trackTintColor = [[UIColor lightGrayColorretain];

        }

        [selfsetNeedsDisplay];

    }

}



- (void)setProgressTintColor:(UIColor *)progressTintColor {

    if(_progressTintColor != progressTintColor) {

        if(_progressTintColor) {

            [_progressTintColorrelease];

            _progressTintColor = nil;

        }

        _progressTintColor = [progressTintColor retain];

        if (!_progressTintColor) {

            _progressTintColor = [[UIColor colorWithHex:0x2e8ae6retain];

        }

        [selfsetNeedsDisplay];

    }

}



- (void)dealloc {

    self.trackImage = nil;

    self.progressImage = nil;

    self.trackTintColor = nil;

    self.progressTintColor = nil;

    [super dealloc];

}



#pragma mark -

#pragma mark Utils

//用来放缩图片的方法,当然,如果你的图片大小刚好,就没有必要了

- (UIImage *)scaleToSize:(UIImage *)img size:(CGSize)size {

    UIGraphicsBeginImageContext(size);

    [img drawInRect:CGRectMake(00, size.width, size.height)];

    UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return scaledImage;

}


@end


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值