标签:
1 #import "ViewController.h" 2 3 @interface ViewController ()<UITextFieldDelegate> 4 5 // 标题标签 6 @property (strong, nonatomic) UILabel *titleLablel; 7 // 分段控制器 8 @property (strong,nonatomic) UISegmentedControl *segment; 9 // 开关标签 10 @property (strong,nonatomic) UILabel *switchLable; 11 // 开关 12 @property (strong,nonatomic) UISwitch *swtch; 13 // 图片 14 @property (strong,nonatomic) UIImageView *progressView; 15 // 等待指示器 16 @property (strong, nonatomic) UIActivityIndicatorView *progressIndicate; 17 // 下载按钮 18 @property (strong,nonatomic) UIButton *progressBtn; 19 // 进度条 20 @property (strong,nonatomic) UIProgressView *progress; 21 // 定时器 22 @property (strong,nonatomic) NSTimer *timer; 23 // 滑动条 24 @property (strong,nonatomic) UISlider *slider; 25 // 文本框 26 @property (strong,nonatomic) UITextField *textField; 27 28 @end 29 30 @implementation ViewController 31 32 #pragma mark - 标题标签 33 - (void)createTitleLeblel 34 { 35 self.titleLablel = [[UILabel alloc] init]; 36 self.titleLablel.frame = CGRectMake(121, 16, 133, 45); 37 self.titleLablel.font = [UIFont systemFontOfSize:26]; 38 self.titleLablel.textAlignment = NSTextAlignmentCenter; 39 self.titleLablel.text = @"控件大全"; 40 [self.view addSubview: self.titleLablel]; 41 42 } 43 44 #pragma mark - 分段控制器 45 - (void) createSegmentControl 46 { 47 NSArray *array = @[@"first",@"second",@"third",@"fourth",@"defult"]; 48 self.segment =[[UISegmentedControl alloc] initWithItems:array]; 49 self.segment.frame = CGRectMake(22, 69, 331, 29); 50 51 [self.segment addTarget:self action:@selector(segmentValueChanged:) forControlEvents:UIControlEventValueChanged]; 52 [self.view addSubview:self.segment]; 53 } 54 55 #pragma mark - 分段器关联方法 56 -(void)segmentValueChanged:(id)sender 57 { 58 UISegmentedControl *tempSegment = (UISegmentedControl*) sender; 59 switch (tempSegment.selectedSegmentIndex) { 60 case 0: 61 self.view.backgroundColor = [UIColor groupTableViewBackgroundColor]; 62 break; 63 case 1: 64 self.view.backgroundColor= [UIColor orangeColor]; 65 break; 66 case 2: 67 self.view.backgroundColor = [UIColor yellowColor]; 68 break; 69 case 3: 70 self.view.backgroundColor = [UIColor greenColor]; 71 break; 72 case 4: 73 self.view.backgroundColor = [UIColor lightGrayColor]; 74 break; 75 76 default: 77 break; 78 } 79 80 } 81 82 #pragma mark - 开关 83 - (void) createSwitch 84 { 85 self.switchLable = [[UILabel alloc] initWithFrame:CGRectMake(22, 338, 164, 21)]; 86 87 self.switchLable.text = @"开启"; 88 [self.view addSubview:self.switchLable]; 89 90 self.swtch = [[UISwitch alloc] initWithFrame:CGRectMake(304, 333, 51, 31)]; 91 92 [self.swtch addTarget:self action:@selector(switchIsOnOrOff) forControlEvents:UIControlEventValueChanged]; 93 94 [self.swtch setOn:YES]; 95 96 [self.view addSubview:self.swtch]; 97 98 } 99 #pragma mark - 开关关联方法 100 - (void)switchIsOnOrOff 101 { 102 if (self.swtch.isOn) { 103 // NSLog(@"开关打开"); 104 self.titleLablel.hidden = NO; 105 self.progressBtn.hidden = NO; 106 self.progress.hidden = NO; 107 self.slider.hidden = NO; 108 self.textField.hidden = NO; 109 110 111 } 112 else{ 113 // NSLog(@"开关关闭"); 114 self.titleLablel.hidden = YES; 115 self.progressBtn.hidden = YES; 116 self.progress.hidden = YES; 117 self.slider.hidden = YES; 118 self.textField.hidden = YES; 119 } 120 121 } 122 123 #pragma mark - 下载 124 -(void) createDownLoad 125 { 126 //创建图片 127 self.progressView = [[UIImageView alloc] initWithFrame:CGRectMake(80, 119, 214, 170)]; 128 self.progressView.image = [UIImage imageNamed:@"touxiang.jpg"]; 129 self.progressView.hidden =YES; 130 [self.view addSubview:self.progressView]; 131 132 // 下载按钮 133 self.progressBtn = [UIButton buttonWithType:UIButtonTypeSystem]; 134 self.progressBtn.frame = CGRectMake(22, 390, 100, 50); 135 [self.progressBtn setTitle:@"下载图片" forState:UIControlStateNormal]; 136 [self.progressBtn setBackgroundColor:[UIColor redColor]]; 137 [self.progressBtn.titleLabel setFont:[UIFont systemFontOfSize:20]]; 138 [self.progressBtn addTarget:self action:@selector(downloadImage) forControlEvents:UIControlEventTouchUpInside]; 139 [self.view addSubview:self.progressBtn]; 140 141 // 进度条 142 self.progress= [[UIProgressView alloc] initWithFrame:CGRectMake(150, 415, 200 , 2)]; 143 self.progress.progressViewStyle = UIProgressViewStyleDefault; 144 // 进度条默认最小0,最大1 145 self.progress.progress = 0; 146 [self.view addSubview:self.progress]; 147 148 // 等待指示器 149 self.progressIndicate = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]; 150 self.progressIndicate.frame = CGRectMake(169, 185, 37, 37); 151 [self.view addSubview:self.progressIndicate]; 152 153 } 154 #pragma mark - 下载关联方法 155 -(void) downloadImage 156 { 157 self.progress.progress = 0; 158 self.progressView.hidden = YES; 159 [self.progressIndicate startAnimating]; 160 self.timer = [NSTimer timerWithTimeInterval:0.2 target:self selector:@selector(progressAdd) userInfo:nil repeats:YES]; 161 [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes]; 162 163 } 164 #pragma mark - 进度条关联方法 165 -(void)progressAdd 166 { 167 // 进度条没走到1 168 if (self.progress.progress < 1) 169 self.progress.progress +=0.1; 170 // 进度条走到1 171 else 172 { 173 self.progressView.hidden = NO; 174 [self.progressIndicate stopAnimating]; 175 [self.timer invalidate]; 176 } 177 } 178 179 #pragma mark - 滑动条 180 -(void) createSlider 181 { 182 //透明度标签 183 UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(32, 502, 102, 21)]; 184 lable.text = @"改变透明度"; 185 [self.view addSubview:lable]; 186 // 滑动条 187 188 self.slider = [[UISlider alloc] initWithFrame:CGRectMake(201, 502, 154, 31)]; 189 self.slider.minimumValue = 0; 190 self.slider.maximumValue = 1; 191 self.slider.value = 1; 192 [self.slider addTarget:self action:@selector(sliderValueChange) forControlEvents:UIControlEventValueChanged]; 193 [self.view addSubview:self.slider]; 194 195 196 197 } 198 #pragma mark - 滑动条关联方法 199 - (void)sliderValueChange 200 { 201 self.view.alpha = self.slider.value; 202 } 203 204 #pragma mark - 文本框 205 - (void)createTextField 206 { 207 UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(32, 582, 85, 21)]; 208 lable.text = @"文本输入:"; 209 [self.view addSubview:lable]; 210 // 文本框 211 self.textField = [[UITextField alloc] initWithFrame:CGRectMake(203, 579, 156, 30)]; 212 self.textField.delegate = self; 213 self.textField.borderStyle = UITextBorderStyleRoundedRect; 214 [self.view addSubview:self.textField]; 215 216 } 217 218 - (BOOL)textFieldShouldReturn:(UITextField *)textField 219 { 220 // 响应者链(自己玩玩看) 221 222 // 取消文本框第一响应者 223 [self.textField resignFirstResponder]; 224 225 // 收回键盘 226 return YES; 227 } 228 229 - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField 230 { 231 //上下文动画内,改变当前视图坐标 232 [UIView beginAnimations:@"text" context:nil]; 233 [UIView setAnimationDuration:0.3]; 234 235 CGRect rect = self.view.frame; 236 rect.origin.y = -210; 237 self.view.frame = rect; 238 239 [UIView commitAnimations]; 240 return YES; 241 } 242 - (BOOL)textFieldShouldEndEditing:(UITextField *)textField 243 { 244 //上下文动画内,改变当前视图坐标 245 [UIView beginAnimations:@"text" context:nil]; 246 [UIView setAnimationDuration:0.3]; 247 248 CGRect rect = self.view.frame; 249 rect.origin.y = 0; 250 self.view.frame = rect; 251 252 [UIView commitAnimations]; 253 return YES; 254 255 256 257 } 258 259 260 - (void)viewDidLoad { 261 [super viewDidLoad]; 262 263 [self.view setBackgroundColor:[UIColor grayColor]]; 264 265 // 标题 266 [self createTitleLeblel]; 267 // 分段控制器 268 [self createSegmentControl]; 269 // 开关 270 [self createSwitch]; 271 // 下载 272 [self createDownLoad]; 273 // 滑动条 274 [self createSlider]; 275 // 文本框 276 [self createTextField]; 277 278 279 }
标签:
原文地址:http://www.cnblogs.com/oc-bowen/p/5076726.html