#import "JRProgres.h"
@implementation JRProgres
{
UIView * _contentView;
}
- (instancetype)initWithFrame:(CGRect)frame{// 重写构造方法一次创建3个视图,分别为最外层的绿框、子1白底、子2红条
self = [super initWithFrame:frame];
if (self) {
//1. 设置边框为绿色
self.backgroundColor = [UIColor greenColor];
UIView * bgview = [[UIView alloc]initWithFrame:CGRectMake(2, 2, frame.size.width-4, frame.size.height-4)];
bgview.backgroundColor = [UIColor whiteColor];
[self addSubview:bgview];
// 2.设置内容条(边框子视图)
self.backgroundColor = [UIColor greenColor];
_contentView = [[UIView alloc]initWithFrame:CGRectMake(0, 2, 0, bgview.frame.size.height-4)];
_contentView.backgroundColor = [UIColor redColor];
[bgview addSubview:_contentView];
}
return self;
}
- (void)setProgress:(int)flag{
// 1. get当前的frame
CGRect newFrame = _contentView.frame;
int nowLength = ceil(newFrame.size.width);
int totalLength = ceil(self.frame.size.width -4);
// 2. 重新给当前的frame设置宽度
if (flag) {
if ( nowLength<totalLength) {
int flag = newFrame.size.width < (self.frame.size.width -4);// 检测是否超范围
NSLog(@"%g---%g---- %i()(",(newFrame.size.width - 4),self.frame.size.width - 4,flag);//打印结果
newFrame.size.width += (self.frame.size.width - 4)/10.0;
}else{
NSLog(@"超出界限");
NSLog(@"%.15f---",(newFrame.size.width - 4));
}
}else{
if (nowLength - 1 > 0) {
newFrame.size.width -= (self.frame.size.width - 4)/10.0;
}
}
// 3.重新给当前的frame设置宽度
_contentView.frame = newFrame;
}
- (void)autoMethod{
CGRect newFrame = _contentView.frame;
int nowLength = ceil(newFrame.size.width);
int totalLength = ceil(self.frame.size.width -4);
if ( nowLength<totalLength) {
newFrame.size.width += (self.frame.size.width - 4)/10.0;
_contentView.frame = newFrame;
} else {
newFrame.size.width = 0;
_contentView.frame = newFrame;
}
}
#import "AppDelegate.h"
#import "JRProgres.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
{
JRProgres * _pro;
}
UI UIView进度条,模拟手机充电功能
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
self.window.backgroundColor = [UIColor grayColor];
[self.window makeKeyAndVisible];
_pro = [[JRProgres alloc]initWithFrame:CGRectMake(0, 150, 250, 25)];
_pro.center = self.window.center;
[self.window addSubview:_pro];
UI UIView进度条,模拟手机充电功能UI UIView进度条,模拟手机充电功能UI UIView进度条,模拟手机充电功能UI UIView进度条,模拟手机充电功能UI UIView进度条,模拟手机充电功能UI UIView进度条,模拟手机充电功能
UIButton * leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
leftButton.frame = CGRectMake(self.window.center.x-70, 200, 50, 45);
[leftButton setTitle:@"前进" forState:UIControlStateNormal];
leftButton.backgroundColor = [UIColor blueColor];
[self.window addSubview:leftButton];
[leftButton addTarget:self action:@selector(clickRight) forControlEvents:UIControlEventTouchUpInside];
UI UIView进度条,模拟手机充电功能UI UIView进度条,模拟手机充电功能
UIButton * rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
rightButton.frame = CGRectMake(self.window.center.x + 20, 200, 50, 45);
[rightButton setTitle:@"后退" forState:UIControlStateNormal];
rightButton.backgroundColor = [UIColor blueColor];
[self.window addSubview:rightButton];
[rightButton addTarget:self action:@selector(clickLeft) forControlEvents:UIControlEventTouchUpInside];
UI UIView进度条,模拟手机充电功能UI UIView进度条,模拟手机充电功能UI UIView进度条,模拟手机充电功能
[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(autoMethod) userInfo:nil repeats:YES];
return YES;
}
- (void)clickLeft{
[_pro setProgress:0];
}
- (void)clickRight{
[_pro setProgress:1];
}
- (void)autoMethod{
[_pro autoMethod];
}
UI UIView进度条,模拟手机充电功能UI UIView进度条,模拟手机充电功能UI
UIView进度条,模拟手机充电功能
@end
原文地址:http://blog.csdn.net/zx6268476/article/details/45093063