UISegmentControl,UISwich,UIPageControl

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UISegmentControl,UISwich,UIPageControl相关的知识,希望对你有一定的参考价值。

  1 #import "ViewController.h"
  2 
  3 @interface ViewController ()
  4 @property (strong,nonatomic) UIView *red;
  5 @property (strong,nonatomic) UIView *blue;
  6 @property (strong,nonatomic) UIView *yellow;
  7 @end
  8 
  9 @implementation ViewController
 10 
 11 
 12 - (void)viewDidLoad {
 13     [super viewDidLoad];
 14     // Do any additional setup after loading the view, typically from a nib.
 15     //登录页面
 16     self.red = [[UIView alloc] initWithFrame:self.view.frame];
 17     self.red.backgroundColor = [UIColor redColor];
 18     [self.view addSubview:self.red];
 19     UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(100, 200, 200, 50)];
 20     lable.text = @"登录页面";
 21     lable.font = [UIFont systemFontOfSize:40];
 22     [self.red addSubview:lable];
 23     
 24     //注册页面
 25     self.blue = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 414, 736)];
 26     self.blue.backgroundColor = [UIColor blueColor];
 27     [self.view addSubview:self.blue];
 28     UILabel *lable1 = [[UILabel alloc] initWithFrame:CGRectMake(100, 200, 200, 50)];
 29     lable1.text = @"注册页面";
 30     lable1.font = [UIFont systemFontOfSize:40];
 31     [self.blue addSubview:lable1];
 32     
 33     
 34     //忘记密码页面
 35     self.yellow = [[UIView alloc] initWithFrame:self.view.frame];
 36     self.yellow.backgroundColor = [UIColor yellowColor];
 37     [self.view addSubview:self.yellow];
 38     UILabel *lable2 = [[UILabel alloc] initWithFrame:CGRectMake(100, 200, 200, 50)];
 39     lable2.text = @"忘记密码";
 40     lable2.font = [UIFont systemFontOfSize:40];
 41     [self.yellow addSubview:lable2];
 42     
 43 #pragma mark------UISegmentControl----------
 44     //创建分段控件
 45     UISegmentedControl *segmented = [[UISegmentedControl alloc] initWithItems:@[@"登录",@"注册",@"忘记密码"]];
 46     segmented.frame = CGRectMake(0, 60, self.view.frame.size.width, 80);
 47        //设置默认选中值
 48     segmented.selectedSegmentIndex = 0;
 49     
 50     //为指定的下标分段设置标题
 51     [segmented setTitle:@"抗日胜利" forSegmentAtIndex:1];
 52     
 53     //添加点击事件
 54     [segmented addTarget:self action:@selector(segmentedAction:) forControlEvents:UIControlEventValueChanged];
 55     
 56     segmented.tag = 999;
 57     
 58     //添加到视图
 59     [self.view addSubview:segmented];
 60     
 61 #pragma mark---------UIPageControl-----------
 62     //创建滑块
 63     UIPageControl *pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(100, self.view.frame.size.height - 100, 300, 40)];
 64     //设置pageController的页数
 65     pageControl.numberOfPages = 4;
 66     //设置当前页(默认为0)
 67     pageControl.currentPage = 1;
 68     //设置颜色
 69     pageControl.backgroundColor = [UIColor blackColor];
 70     //添加事件
 71     [pageControl addTarget:self action:@selector(pangeControlAction:) forControlEvents:UIControlEventValueChanged];
 72     [self.view addSubview:pageControl];
 73     //循环创建4个图片
 74     for (int i = 0; i < 4; i++) {
 75         UIImageView *picture = [[UIImageView alloc] initWithFrame:self.view.frame];
 76         [picture setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%d.jpg",i+1]]];
 77         picture.tag = 1000 + i;
 78         [self.view addSubview:picture];
 79     }
 80     pageControl.tag  =1111;
 81     [self.view bringSubviewToFront:pageControl];
 82     
 83 #pragma mark-------UISwitch
 84     //创建一个UISwitch  注意:这里的frame只有origin起作用,size使用系统默认大小
 85     UISwitch *firstSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(100, 170, 0, 0)];
 86     
 87     //设置开关开启时候的颜色
 88     firstSwitch.onTintColor = [UIColor greenColor];
 89     
 90     //设置开关风格颜色
 91     firstSwitch.tintColor = [UIColor orangeColor];
 92     
 93     //设置开关按钮颜色
 94     firstSwitch.thumbTintColor = [UIColor redColor];
 95     
 96     //设置开关状态
 97     
 98     firstSwitch.on = YES;
 99     [firstSwitch addTarget:self action:@selector(action2:) forControlEvents:UIControlEventValueChanged];
100     
101     [self.view addSubview:firstSwitch];
102     [self.view bringSubviewToFront:firstSwitch];
103 
104     
105     
106 }
107 //switch事件
108 -(void)action2:(UISwitch *)first
109 {
110     UIPageControl *page = (UIPageControl *)[self.view viewWithTag:1111];
111     if (first.on == NO) {
112         page.userInteractionEnabled = NO;
113     }else
114     {
115         page.userInteractionEnabled = YES;
116     }
117 }
118 //pageControl事件
119 -(void)pangeControlAction:(UIPageControl *)pageControl
120 {
121     [self.view bringSubviewToFront:[self.view viewWithTag:pageControl.currentPage + 1000]];
122     [self.view bringSubviewToFront:pageControl];
123     
124    // UISegmentedControl *segment = (UISegmentedControl *)[self.view viewWithTag:999];
125     //[self.view bringSubviewToFront:segment];
126     
127 
128    }
129 
130 
131 //segmengted事件
132 -(void)segmentedAction:(UISegmentedControl *)segment
133 {
134     switch (segment.selectedSegmentIndex) {
135         case 0:
136             [self.view insertSubview:self.red belowSubview:segment];
137             break;
138         case 1:
139             [self.view insertSubview:self.blue belowSubview:segment];
140             break;
141         case 2:
142             [self.view insertSubview:self.yellow belowSubview:segment];
143             break;
144             
145         default:
146             break;
147     }
148 }
149 
150 - (void)didReceiveMemoryWarning {
151     [super didReceiveMemoryWarning];
152     // Dispose of any resources that can be recreated.
153 }
154 
155 @end

 

以上是关于UISegmentControl,UISwich,UIPageControl的主要内容,如果未能解决你的问题,请参考以下文章

使用 UISegmentControl 切换视图时 UISearchBar 消失

UISegmentedControl 在视图控制器之间切换

自定义控件之AndroidSegmentControlView,仿IOS平台UISegmentControlView,继承自View

[原创]自定义控件之AndroidSegmentControlView,仿IOS平台UISegmentControlView,继承自View

分段控制切换视图?

遍历 UISegmentedControl 值