//自己新建的一个类,随便取一个名字,我的叫Myprogressvie.h
#import <UIKit/UIKit.h>
@interface Myprogressvie : UIView
{
//定义第一个视图
UIView *bigView;
//定义第一个视图
UIView *smallView;
//定义一个按钮
UIButton *button;
UITextField *text;
//定义一个定时器
NSTimer *timer;
}
@end
#import "Myprogressvie.h"
@implementation Myprogressvie
//定义一个全局变量number1,number2
int number1 ,number2;
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// NSLog(@"请输入数字");
// scanf("%d",&num);
//创建大视图
bigView=[[UIView alloc] initWithFrame:CGRectMake(50, 100, 280, 20)];
bigView.backgroundColor=[UIColor redColor];
bigView.layer.cornerRadius=10;
[self addSubview:bigView];
//创建文本
text=[[UITextField alloc] initWithFrame:CGRectMake(100, 150, 80, 50)];
// text.backgroundColor=[UIColor grayColor];
text.borderStyle=1;
text.keyboardType=UIKeyboardTypeNumberPad;
[self addSubview:text];
//创建button
button=[[UIButton alloc] initWithFrame:CGRectMake(200, 150, 80, 50)];
button.backgroundColor=[UIColor grayColor];
[button setTitle:@"确认" forState:UIControlStateNormal];
[self addSubview:button];
//button触发事件
[button addTarget:self action:@selector(test) forControlEvents:UIControlEventTouchUpInside];
}
return self;
}
//定时器调用添加第二个视图的方法
-(void)test
{
NSLog(@"确认");
//定时器定时,0.2代表每隔0.2秒重复调用addview方法
timer=[NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(addview) userInfo:nil repeats:YES];
}
//添加第二个视图的方法
-(void)addview
{
//number2是键盘录入的数
number2= text.text.intValue;
if (number1<=number2)
{
//number1是一个全局变量
number1++;
smallView=[[UIView alloc] initWithFrame:CGRectMake(50, 100, 2.8*number1, 20)];
smallView.backgroundColor=[UIColor blueColor];
smallView.layer.cornerRadius=10;
[self addSubview:smallView];
}
}
@end
//ViewController.m文件
self.progressView=[[Myprogressvie alloc] initWithFrame:CGRectMake(0, 0, 414, 400)];
self.progressView.backgroundColor=[UIColor grayColor];
[self.view addSubview:self.progressView];
初学者,有什么错误的地方还请多多见谅!