通过改变uiview的layer的frame来实现进度条
2024-08-28 23:07:29
#import <UIKit/UIKit.h> @interface ProgressView : UIView
@property(nonatomic,assign)CGFloat progress;
@property(nonatomic,strong)UIColor *layColor;
@end
#import "ProgressView.h"
@interface ProgressView ()
{
CALayer *progressLayer; CGFloat currentViewWidth;//当前view的宽度
}
@end
@implementation ProgressView
@synthesize progress=_progress;
@synthesize layColor=_layColor; - (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
progressLayer=[CALayer layer];
progressLayer.frame=CGRectMake(, , , frame.size.height);
progressLayer.backgroundColor=[UIColor redColor].CGColor;
[self.layer addSublayer:progressLayer];
currentViewWidth=frame.size.width;
}
return self;
}
-(void)setProgress:(CGFloat)progress
{
_progress=progress;
if (progress<=) {
progressLayer.frame=CGRectMake(, , , self.frame.size.height);
}
else if (progress<=)
{
progressLayer.frame=CGRectMake(, , progress*currentViewWidth, self.frame.size.height);
}
else
{
progressLayer.frame=CGRectMake(, , currentViewWidth, self.frame.size.height);
} }
-(CGFloat)progress
{
return _progress;
}
-(void)setLayColor:(UIColor *)layColor
{
_layColor=layColor;
self.layer.backgroundColor=layColor.CGColor;
}
-(UIColor *)layColor
{
return _layColor;
}
@end
#import "ViewController.h"
#import "ProgressView.h"
@interface ViewController ()
@property(nonatomic,strong)ProgressView *progressView;
@property(nonatomic,strong)NSTimer *timer;
@end @implementation ViewController - (void)viewDidLoad
{
[super viewDidLoad];
self.progressView=[[ProgressView alloc]initWithFrame:CGRectMake(, , , )];
[self.view addSubview:self.progressView];
_timer=[NSTimer scheduledTimerWithTimeInterval: target:self selector:@selector(layerAnimation) userInfo:nil repeats:YES]; }
-(void)layerAnimation
{
self.progressView.progress=arc4random()%/100.0f;
self.progressView.layColor=[UIColor greenColor];
} @end
最新文章
- Webkit 文字和背景效果
- chrome 问题
- Kotlin语法(类和对象)
- fastx_toolkit去除测序数据中的接头和低质量的reads
- Web GIS 离线地图
- 活动 Activity 四种加载模式
- PYTHON 获取机器硬件信息及状态
- Collections.unmodifiableList()的使用与场景
- MD5加密。
- maven依赖冲突
- liunx 安装redis 4.0
- 【HNOI 2018】道路
- git出现warning: LF will be replaced by CRLF的解决方法
- ES6中对象
- 对象生命周期中至少被GC一次后存活
- ext.js的目录结构的简单解释
- MyBatis关联查询,一对多关联查询
- Web API 源码剖析之全局配置
- Linux(Red hat)无网离线安装TensorFlow
- 【转发】jQuery1.9.1至最高版本针对checkbox的调整