博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
通过改变uiview的layer的frame来实现进度条
阅读量:6914 次
发布时间:2019-06-27

本文共 2045 字,大约阅读时间需要 6 分钟。

#import 
@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(0, 0, 0, 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<=0) { progressLayer.frame=CGRectMake(0, 0, 0, self.frame.size.height); } else if (progress<=1) { progressLayer.frame=CGRectMake(0, 0, progress*currentViewWidth, self.frame.size.height); } else { progressLayer.frame=CGRectMake(0, 0, 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(20, 20, 290, 3)];    [self.view addSubview:self.progressView];    _timer=[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(layerAnimation) userInfo:nil repeats:YES];        }-(void)layerAnimation{    self.progressView.progress=arc4random()%100/100.0f;    self.progressView.layColor=[UIColor greenColor];}@end

 

转载于:https://www.cnblogs.com/thbbsky/p/4379334.html

你可能感兴趣的文章
我的友情链接
查看>>
12个时间管理妙招
查看>>
Python面向对象之类的成员
查看>>
Win8上iis配置
查看>>
Confluence 6 配置 Office 转换器
查看>>
IT从业人员关注哪些问题
查看>>
Windows 2012 Hyper –V 3.0 New Functions
查看>>
maven部分插件配置demo
查看>>
Grin交易原理详解
查看>>
大数据体系【概念认知】系列-2:存储以及副本策略
查看>>
我的友情链接
查看>>
linux企业常用服务---haproxy+nginx搭建web高可用集群
查看>>
win7 断开 共享连接的操作方法
查看>>
CTSSD服务无法正常启动:Failure 4 in trying to open SV key PROCL-4/PROCL-5 clsctss_r_av2
查看>>
再议OPEN CURSOR与BULK COLLECT
查看>>
我的友情链接
查看>>
jquery attr与prop
查看>>
casatwy组件化方案
查看>>
Linux中ls对文件进行按大小排序和按时间排序
查看>>
Unix/Linux下安装NPM
查看>>