標(biāo)題: iOS開發(fā)之使用CALayer封裝下載進(jìn)度條 [打印本頁]
作者: niujia 時(shí)間: 2015-7-18 00:34
標(biāo)題: iOS開發(fā)之使用CALayer封裝下載進(jìn)度條
效果圖

代碼
//
// XRProgressView.h
// 使用CALayer封裝下載進(jìn)度條
//
// Created by 寒竹子 on 15/6/14.
// Copyright (c) 2015年 寒竹子. All rights reserved.
//
/**
* 下載進(jìn)度條封裝
*/
#import <UIKit/UIKit.h>
@interface XRProgressView : UIView
@property (nonatomic, assign) CGFloat progress;
@property (nonatomic, strong) UIColor * progressColor;
@end
//
// XRProgressView.m
// 使用CALayer封裝下載進(jìn)度條
//
// Created by 寒竹子 on 15/6/14.
// Copyright (c) 2015年 寒竹子. All rights reserved.
//
#import "XRProgressView.h"
@interface XRProgressView ()
@property (nonatomic, strong) CALayer * progressLayer; // 進(jìn)度layer
@property (nonatomic, assign) CGFloat currentWidth; // 當(dāng)前l(fā)ayer的width
@end
@implementation XRProgressView
- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
self.layer.backgroundColor = [UIColor clearColor].CGColor;
self.layer.borderWidth = .5f;
// 創(chuàng)建layer
_progressLayer = [CALayer layer];
_progressLayer.frame = CGRectMake(0, 0, 0, self.frame.size.height);
// 將progressLayer添加到當(dāng)前View的layer中
[self.layer addSublayer:_progressLayer];
// 保存width
_currentWidth = self.frame.size.width;
}
return self;
}
#pragma mark - getters and setters
@synthesize progress = _progress;
// 設(shè)置進(jìn)度
- (void)setProgress:(CGFloat)progress
{
_progress = progress;
if (_progress < 0.0f) {
self.progressLayer.frame = CGRectMake(0, 0, 0, self.frame.size.height);
}else if (_progress <= 1.0f) {
self.progressLayer.frame = CGRectMake(0, 0, _progress * self.currentWidth, self.frame.size.height);
}else {
self.progressLayer.frame = CGRectMake(0, 0, self.currentWidth, self.frame.size.height);
}
}
- (CGFloat)progress
{
return _progress;
}
@synthesize progressColor = _progressColor;
// 設(shè)置layer背景顏色
- (void)setProgressColor:(UIColor *)progressColor
{
_progressColor = progressColor;
self.progressLayer.backgroundColor = _progressColor.CGColor;
}
- (UIColor *)progressColor
{
return _progressColor;
}
@end
//
// ViewController.m
// 使用CALayer封裝下載進(jìn)度條
//
// Created by 寒竹子 on 15/6/14.
// Copyright (c) 2015年 寒竹子. All rights reserved.
//
#define KBorder 20.0f
#import "ViewController.h"
#import "XRProgressView.h"
@interface ViewController ()
@property (nonatomic, strong) XRProgressView * progressView;
@property (nonatomic, strong) NSTimer * timer;
@property (nonatomic, assign) CGFloat progress; // 下載進(jìn)度
@end
@implementation ViewController
- (instancetype)init
{
if (self = [super init]) {
_progress = 0.0f;
}
return self;
}
// 起定時(shí)器
- (void)startTimer
{
_timer = [NSTimer scheduledTimerWithTimeInterval:.1f
target:self
selector:@selector(updateProgress)
userInfo:nil
repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
}
// 模擬更新下載進(jìn)度
- (void)updateProgress
{
if (_progress > 1.0f) {
_progress = 0.0f;
}else {
_progress += .01f;
}
self.progressView.progress = _progress;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.progressView = [[XRProgressView alloc] initWithFrame:CGRectMake(KBorder, KBorder * 2.0f, self.view.frame.size.width - KBorder * 2.0f, 4.0f)];
self.progressView.layer.masksToBounds = YES;
self.progressView.layer.cornerRadius = self.progressView.frame.size.height / 2.0f;
self.progressView.progressColor = [UIColor redColor];
[self.view addSubview:self.progressView];
// 開啟定時(shí)器
[self startTimer];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end
歡迎光臨 (http://www.torrancerestoration.com/bbs/) |
Powered by Discuz! X3.1 |