码迷,mamicode.com
首页 > 移动开发 > 详细

IOS UI学习 UI 十个小控件 初度学习

时间:2015-09-17 21:33:15      阅读:368      评论:0      收藏:0      [点我收藏+]

标签:

1.  UISwitch 开关

 1 - (void)viewDidLoad
 2 {
 3     [super viewDidLoad];
 4     self.view.backgroundColor = [UIColor whiteColor];
 5     self.title = @"开关";
 6     //开关创建
 7     UISwitch * sw = [[UISwitch alloc]initWithFrame:CGRectMake(10, 100, 100, 100)];
 8     //设置开关的位置
 9     sw.center = self.view.center;
10     sw.onTintColor = [UIColor yellowColor];
11     //注册事件  绑定事件
12     
13     //UIControlEventValueChanged  触发控件的事件 一旦触发就会让target 调用action方法
14     [sw addTarget:self action:@selector(changeColor:) forControlEvents:UIControlEventValueChanged];
15     //开关的小圆点的颜色
16     sw.thumbTintColor = [UIColor blackColor];
17     //设置开关状态
18     sw.on = YES;
19     [self.view addSubview:sw];
20     
21 }
22 
23 -(void)changeColor:(UISwitch *)swi
24 {
25     //根据开关的ON属性来设置相应的事件
26     if (swi.on)
27     {
28         self.view.backgroundColor = [UIColor cyanColor];
29         //onTintColor 镂空的颜色
30         swi.onTintColor = [UIColor blueColor];
31         swi.tintColor = [UIColor orangeColor];
32     }
33     else
34     {
35         self.view.backgroundColor = [UIColor whiteColor];
36     }
37 }

2. UISlider 滑块

 1 - (void)viewDidLoad
 2 {
 3     [super viewDidLoad];
 4     self.title = @"滑块";
 5     self.view.backgroundColor = [UIColor whiteColor];
 6     //创建滑块
 7     UISlider * slider = [[UISlider alloc] initWithFrame:CGRectMake(10, 100, 350, 200)];
 8     //最好把值得范围设置在 0 和 1 之间
 9     //最大值
10     slider.maximumValue = 1;
11     //最小值
12     slider.minimumValue = 0;
13     //滑块的当前值
14     slider.value = 0.6;
15     //小于滑块当前值得一边的颜色
16     slider.minimumTrackTintColor = [UIColor redColor];
17     //大于滑块当前值得一边的颜色
18     slider.maximumTrackTintColor = [UIColor cyanColor];
19     //点的颜色
20     slider.thumbTintColor = [UIColor grayColor];
21     
22     slider.backgroundColor = [UIColor lightGrayColor];
23 
24     //注册事件 绑定事件
25     [slider addTarget:self action:@selector(changeBackgroundColor:) forControlEvents:UIControlEventValueChanged];
26     
27     [self.view addSubview:slider];
28 }
29 
30 -(void)changeBackgroundColor:(UISlider*)slider
31 {
32     NSLog(@"%.2f",slider.value);
33     self.view.alpha = slider.value;
34 }

3. UIActivityIndicatorView   活动指示器 菊花控件

 1 - (void)viewDidLoad
 2 {
 3     [super viewDidLoad];
 4     self.title = @"菊花";
 5     self.view.backgroundColor = [UIColor whiteColor];
 6     
 7     UIActivityIndicatorView * act = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(10, 100, 100, 100)];
 8     
 9     act.backgroundColor = [UIColor lightGrayColor];
10     
11     //设置菊花 控件的样式 三种
12     /*
13      
14      typedef NS_ENUM(NSInteger, UIActivityIndicatorViewStyle) {
15      UIActivityIndicatorViewStyleWhiteLarge,
16      UIActivityIndicatorViewStyleWhite,
17      UIActivityIndicatorViewStyleGray,
18      };
19      
20      */
21     act.activityIndicatorViewStyle =  UIActivityIndicatorViewStyleGray;
22     
23     [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
24     //开始动画
25     [act startAnimating];
26     //结束动画
27     [act stopAnimating];
28     act.center = self.view.center;
29     [self.view addSubview:act];
30     
31 }
32 
33 //取消 通知栏中的小得 菊花控件 显示  在要退出的视图控制器中实现
34 -(void)viewWillAppear:(BOOL)animated
35 {
36     [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
37 }

4. UIAlertView  弹窗 

需要遵守协议   UIAlertViewDelegate

 1 #import "ViewController4.h"
 2 
 3 @interface ViewController4 () <UIAlertViewDelegate>
 4 
 5 @end
 6 
 7 @implementation ViewController4
 8 
 9 - (void)viewDidLoad
10 {
11     [super viewDidLoad];
12     self.view.backgroundColor = [UIColor whiteColor];
13     self.title = @"弹窗";
14     
15     UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"美食" message:@"我是一专业的吃货" delegate:self cancelButtonTitle:@"不爱吃" otherButtonTitles:@"卡布奇诺",@"",@"驴肉火烧",@"干煸芸豆", nil];
16     alert.delegate =self;
17 
18     [alert show];
19 
20 }
21 
22 -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
23 {
24     switch (buttonIndex)
25     {
26         case 0://取消按钮
27             NSLog(@"%@",alertView.title);
28             [self.navigationController popToRootViewControllerAnimated:YES];
29             break;
30             
31         case 1:
32             NSLog(@"卡布奇诺");
33             break;
34             
35         case 2:
36             NSLog(@"");
37             break;
38             
39         case 3:
40             NSLog(@"驴肉火烧");
41             break;
42             
43         case 4:
44             NSLog(@"干煸芸豆");
45             break;
46         default:
47             break;
48     }
49 }

5. UIProgressView  进度条

 1 - (void)viewDidLoad
 2 {
 3     [super viewDidLoad];
 4     self.view.backgroundColor = [UIColor whiteColor];
 5     
 6     UIProgressView * progress = [[UIProgressView alloc] initWithFrame:CGRectMake(10, 100, 355, 100)];
 7     
 8     //设置样式
 9     progress.progressViewStyle = UIProgressViewStyleBar;
10 
11     progress.backgroundColor = [UIColor orangeColor];
12     //进度
13     progress.progress = 0.2;
14     //未完成进度颜色
15     progress.trackTintColor = [UIColor lightGrayColor];
16     //完成进度颜色
17     progress.progressTintColor = [UIColor cyanColor];
18     //每 0.5 s 让 target 调用 selector方法 重复调用
19     // userInfo   time 定时器 的触发信息
20     _time = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(changeProgress:) userInfo:progress repeats:YES];
21     [self.view addSubview:progress];
22 }
23 
24 -(void)changeProgress:(NSTimer *)time
25 {
26     UIProgressView * progress = time.userInfo;
27     progress.progress += 0.1;
28 }

 

 

6. UIStepper  步进器

 1 - (void)viewDidLoad
 2 {
 3     [super viewDidLoad];
 4     self.view.backgroundColor = [UIColor whiteColor];
 5     self.title = @"步进器";
 6     //大小固定  宽高 无效
 7     UIStepper * stepper = [[UIStepper alloc] initWithFrame:CGRectMake(10, 100, 355, 100)];
 8     //注册事件 绑定事件
 9     [stepper addTarget:self action:@selector(changeLabelValue:) forControlEvents:UIControlEventValueChanged];
10     //步数
11     stepper.stepValue =
12     30;
13     //最大值
14     stepper.maximumValue = 100;
15     //最小值
16     stepper.minimumValue = 0;
17     
18     //设置循环
19     stepper.wraps = YES;
20     [self.view addSubview:stepper];
21     
22     
23     UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(10, 230, 355, 30)];
24     label.text = @"0";
25     label.backgroundColor = [UIColor whiteColor];
26     label.textColor = [UIColor blackColor];
27     label.textAlignment = NSTextAlignmentCenter;
28     
29     
30     label.tag = 100;
31     
32     [self.view addSubview:label];
33     
34 }
35 
36 -(void)changeLabelValue:(UIStepper *)stepper
37 {
38     UILabel * label = (id)[self.view viewWithTag:100];
39     label.text = [NSString stringWithFormat:@"%.0f",stepper.value];
40 }

 

7. UISegmentedControl  分段选择器

 1 - (void)viewDidLoad
 2 {
 3     [super viewDidLoad];
 4     self.view.backgroundColor = [UIColor whiteColor];
 5     self.title = @"分段选择器";
 6     // 你以为马里奥是个简单的游戏吗
 7     NSArray * arr = [[NSArray alloc] initWithObjects:@"马里奥",@"路易吉",@"奇诺比奥",@"耀西", nil];
 8     
 9     //创建
10     UISegmentedControl * segmented = [[UISegmentedControl alloc] initWithItems:arr];
11     segmented.frame = CGRectMake(10, 100, 355, 50);
12     //默认选择的 下标从0开始  不设置 则默认谁都不选择
13     segmented.selectedSegmentIndex = 2;
14     
15 //    segmented.tintColor = [UIColor cyanColor];
16     [self.view addSubview:segmented];
17     //插入
18     [segmented insertSegmentWithTitle:@"库巴" atIndex:2 animated:YES];
19     //删除项
20     [segmented removeSegmentAtIndex:2 animated:YES];
21     
22     //注册事件 绑定事件
23     [segmented addTarget:self action:@selector(changeObj:) forControlEvents:UIControlEventValueChanged];
24     
25     [segmented removeSegmentAtIndex:2 animated:YES];
26 }
27 
28 -(void)changeObj:(UISegmentedControl *)segmented
29 {
30     if (segmented.selectedSegmentIndex == 0)
31     {
32         NSLog(@"马里奥");
33     }
34     else if(segmented.selectedSegmentIndex == 1)
35     {
36         [segmented insertSegmentWithTitle:@"库巴" atIndex:2 animated:YES]; 
37     }
38 }

 

8. UITextView 文本显示框

需要遵守 协议  UITextViewDelegate

 

 1 #import "ViewController8.h"
 2 
 3 @interface ViewController8 () <UITextViewDelegate>
 4 
 5 @end
 6 
 7 @implementation ViewController8
 8 
 9 - (void)viewDidLoad
10 {
11     [super viewDidLoad];
12     self.view.backgroundColor = [UIColor cyanColor];
13     self.title = @"文本显示框";
14     
15     UITextView * textV = [[UITextView alloc] initWithFrame:CGRectMake(10, 70, 355, 200)];
16     
17     //继承于UIScrollView  √√√√√
18     textV.textAlignment = NSTextAlignmentLeft;
19     textV.font = [UIFont systemFontOfSize:15];
20     // 可以设置textView的留白
21     self.automaticallyAdjustsScrollViewInsets = NO;
22     
23     textV.textColor = [UIColor blueColor];
24     textV.delegate = self;
25     
26     [self.view addSubview:textV];
27     
28 }
29 
30 
31 
32 #pragma mark 协议方法
33 -(void)textViewDidBeginEditing:(UITextView *)textView
34 {
35     
36 }
37 -(void)textViewDidChange:(UITextView *)textView
38 {
39     
40 }
41 -(void)textViewDidChangeSelection:(UITextView *)textView
42 {
43     
44 }
45 
46 -(void)textViewDidEndEditing:(UITextView *)textView
47 {
48     
49 }
50 -(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
51 {
52     return YES;
53 }
54 -(BOOL)textViewShouldBeginEditing:(UITextView *)textView
55 {
56     //将要编辑
57     return YES;
58 }
59 -(BOOL)textViewShouldEndEditing:(UITextView *)textView
60 {
61     //
62     return YES;
63 }
64 
65 
66 - (void)didReceiveMemoryWarning
67 {
68     [super didReceiveMemoryWarning];
69     // Dispose of any resources that can be recreated.
70 }
71 
72 @end

 

9. UIWebView 网页显示控件

 1 - (void)viewDidLoad
 2 {
 3     [super viewDidLoad];
 4 
 5     self.view.backgroundColor = [UIColor blueColor];
 6     self.title = @"网页显示控件";
 7     
 8     UIWebView * webV = [[UIWebView alloc] initWithFrame:self.view.frame];
 9     NSString * str = @"http://www.baidu.com";
10     //转网址
11     NSURL * url = [NSURL URLWithString:str];
12     //请求
13     NSURLRequest * quest = [NSURLRequest requestWithURL:url];
14     //加载数据
15     [webV loadRequest:quest];
16     
17     [self.view addSubview:webV];
18 }

 

 

10. UIActionSheet 底部弹窗  

需要 遵守协议  UIActionSheetDelegate

 1 - (void)viewDidLoad
 2 {
 3     [super viewDidLoad];
 4     self.view.backgroundColor = [UIColor whiteColor];
 5     // 通过一个按钮的点击 调出 UIActionSheet
 6     UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
 7     btn.frame = CGRectMake(10, 100, 100, 40);
 8     btn.backgroundColor = [UIColor cyanColor];
 9     [btn addTarget:self action:@selector(shared:) forControlEvents:UIControlEventTouchUpInside];
10     btn.tag = 100;
11     [self.view addSubview:btn];
12 }
13 
14 
15 -(void)shared:(UIButton*)btn
16 {
17     //创建  需要遵守协议  UIActionSheetDelegate
18     UIActionSheet * sheet = [[UIActionSheet alloc] initWithTitle:@"分享" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"微信" otherButtonTitles:@"新浪微博",@"百度贴吧",@"豆瓣一刻", nil];
19     
20     //设置 sheet 显示
21     [sheet showInView:self.view];
22 }
23 
24 -(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
25 {
26     UIButton * btn = (id)[self.view viewWithTag:100];
27     if (buttonIndex == 0)
28     {
29         NSLog(@"微信");
30         btn.backgroundColor = [UIColor greenColor];
31     }
32     else if(buttonIndex == 1)
33     {
34         NSLog(@"新浪微博");
35         btn.backgroundColor = [UIColor redColor];
36     }
37     else if(buttonIndex == 2)
38     {
39         NSLog(@"百度贴吧");
40         btn.backgroundColor = [UIColor blueColor];
41     }
42     else if(buttonIndex == 3)
43     {
44         NSLog(@"豆瓣一刻");
45         btn.backgroundColor = [UIColor greenColor];
46     }
47     else if(buttonIndex == 4)
48     {
49         NSLog(@"取消");
50         btn.backgroundColor = [UIColor blackColor];
51     }
52 }

 

11. UIAlertController 弹窗控制器

 

 1 - (void)viewDidLoad
 2 {
 3     [super viewDidLoad];
 4     self.view.backgroundColor = [UIColor whiteColor];
 5     
 6     //// 通过一个按钮的点击 调出 UIAlertController
 7     UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
 8     btn.frame = CGRectMake(10, 100, 100, 40);
 9     btn.backgroundColor = [UIColor cyanColor];
10     [btn addTarget:self action:@selector(alertV:) forControlEvents:UIControlEventTouchUpInside];
11     btn.tag = 100;
12     [self.view addSubview:btn];
13     
14 }
15 
16 -(void)alertV:(UIButton *)btn
17 {
18     UIAlertController * alertC = [UIAlertController alertControllerWithTitle:@"能力者" message:@"正义联盟" preferredStyle:UIAlertControllerStyleAlert];
19     //preferredStyle UIAlertController
20     //两种
21     
22     /*
23      
24      typedef NS_ENUM(NSInteger, UIAlertControllerStyle) {
25      UIAlertControllerStyleActionSheet = 0,
26      UIAlertControllerStyleAlert
27      } NS_ENUM_AVAILABLE_IOS(8_0);
28      
29      */
30     [alertC addAction:[UIAlertAction actionWithTitle:@"超人" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
31         NSLog(@"I am SuperMan");
32     }]];
33     
34     [alertC addAction:[UIAlertAction actionWithTitle:@"钢铁侠" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
35         NSLog(@"I am iconMan");
36     }]];
37     
38     [alertC addAction:[UIAlertAction actionWithTitle:@"斯凯" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
39         NSLog(@"I am Skey");
40     }]];
41     
42     //UIAlertActionStyleDestructive  变红
43     [alertC addAction:[UIAlertAction actionWithTitle:@"郑吒" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
44         NSLog(@"你们这些人,凡人的智慧");
45     }]];
46     // UIAlertController 是一个控制器 , 可以推出 使用 present
47     [self presentViewController:alertC animated:YES completion:nil];
48 
49     //按钮 样式
50     //三种
51     /*
52      
53      typedef NS_ENUM(NSInteger, UIAlertActionStyle) {
54      UIAlertActionStyleDefault = 0,
55      UIAlertActionStyleCancel,
56      UIAlertActionStyleDestructive
57      }
58      
59      */
60 }

 

IOS UI学习 UI 十个小控件 初度学习

标签:

原文地址:http://www.cnblogs.com/cccccy/p/4817500.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!