UI基础 - 训练营:使用UISlider变换视图

使用 UISlider 变换视图

1 - 我们利用 UISlider 控制 RGB 的值从而达到视图颜色变更的目的。下面代码中 UISlider 改变视图颜色、UISwitch 隐藏视图、UIStepper 控制视图大小

// - ViewController

复制代码
  1 #import "RootViewController.h"
  2 @interface RootViewController(){
  3     
  4     CGFloat  _red;
  5     CGFloat  _green;
  6     CGFloat  _blue;
  7     UIView * _aView;
  8 }
  9 @property (nonatomic, retain)UIView *redView;
 10 @property (nonatomic, retain)UIView *greenView;
 11 
 12 @end
 13 
 14 @implementation RootViewController
 15 
 16 - (void)viewDidLoad {
 17     [super viewDidLoad];
 18 
 19     NSArray *arrayTitle = [NSArray arrayWithObjects:@"红色视图",@"绿色视图", nil];
 20     UISegmentedControl *segment = [[UISegmentedControl alloc] initWithItems:arrayTitle];
 21     segment.frame = CGRectMake(60, 80, 200, 30);
 22     // _redView | _greenView 相互切换
 23     [segment addTarget:self action:@selector(trigger:) forControlEvents:UIControlEventValueChanged];
 24     [self.view addSubview:segment];
 25     
 26     
 27     // 红色视图
 28     self.redView = [[UIView alloc] initWithFrame:CGRectMake(30, 140, 115, 180)];
 29     _redView.backgroundColor = [UIColor redColor];
 30     [self.view addSubview:_redView];
 31     // 绿色视图
 32     self.greenView = [[UIView alloc] initWithFrame:CGRectMake(175, 140, 115, 180)];
 33     _greenView.backgroundColor = [UIColor greenColor];
 34     [self.view addSubview:_greenView];
 35 
 36     
 37     // 红色视图
 38     // 标题
 39     UILabel *redLabel = [[UILabel alloc] initWithFrame:CGRectMake(30, 350, 40, 30)];
 40     redLabel.text = @"R";
 41     redLabel.font = [UIFont boldSystemFontOfSize:20];
 42     redLabel.textColor = [UIColor redColor];
 43     [self.view addSubview:redLabel];
 44     // UISlider
 45     UISlider *redSlider = [[UISlider alloc] initWithFrame:CGRectMake(80, 350, 210, 30)];
 46     redSlider.tag = 100;
 47     // 滑动条颜色
 48     redSlider.minimumTrackTintColor = [UIColor redColor];
 49     redSlider.maximumTrackTintColor = [UIColor darkGrayColor];
 50     [self.view addSubview:redSlider];
 51     [redSlider addTarget:self action:@selector(changeColor:) forControlEvents:UIControlEventValueChanged];
 52     
 53     
 54     // 绿色视图
 55     UILabel *greenLabel = [[UILabel alloc] initWithFrame:CGRectMake(30, 390, 40, 30)];
 56     greenLabel.text = @"G";
 57     greenLabel.font = [UIFont boldSystemFontOfSize:20];
 58     greenLabel.textColor = [UIColor greenColor];
 59     [self.view addSubview:greenLabel];
 60     
 61     
 62     UISlider *greenSlider = [[UISlider alloc] initWithFrame:CGRectMake(80, 390, 210, 30)];
 63     greenSlider.tag = 101;
 64     greenSlider.minimumTrackTintColor = [UIColor greenColor];
 65     greenSlider.maximumTrackTintColor = [UIColor darkGrayColor];
 66     [self.view addSubview:greenSlider];
 67     [greenSlider addTarget:self action:@selector(changeColor:) forControlEvents:UIControlEventValueChanged];
 68     
 69     
 70     UILabel *blueLabel = [[UILabel alloc] initWithFrame:CGRectMake(30, 430, 40, 30)];
 71     blueLabel.text = @"B";
 72     blueLabel.font = [UIFont boldSystemFontOfSize:20];
 73     blueLabel.textColor = [UIColor blueColor];
 74     [self.view addSubview:blueLabel];
 75 
 76     
 77     UISlider *blueSlider = [[UISlider alloc] initWithFrame:CGRectMake(80, 430, 210, 30)];
 78     blueSlider.tag = 102;
 79     blueSlider.minimumTrackTintColor = [UIColor blueColor];
 80     blueSlider.maximumTrackTintColor = [UIColor darkGrayColor];
 81     [self.view addSubview:blueSlider];
 82     [blueSlider addTarget:self action:@selector(changeColor:) forControlEvents:UIControlEventValueChanged];
 83 
 84     
 85     UILabel *swiLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 500, 80, 30)];
 86     swiLabel.text = @"隐藏/显示";
 87     [self.view addSubview:swiLabel];
 88 
 89     
 90     // UISwitch 大小不能自定义,只能定义它的位置
 91     UISwitch *swit = [[UISwitch alloc] initWithFrame:CGRectMake(85, 500, 30, 50)];
 92     swit.on = YES;//初始为 ON 的一边
 93     [swit addTarget:self action:@selector(showOrHideView:) forControlEvents:UIControlEventValueChanged];
 94     [self.view addSubview:swit];
 95     
 96 
 97     UILabel *setLabel = [[UILabel alloc] initWithFrame:CGRectMake(140, 500, 80, 30)];
 98     setLabel.text = @"减小/增大";
 99     [self.view addSubview:setLabel];
100     
101     
102     // stepper:减小/放大
103     UIStepper *stepper = [[UIStepper alloc] initWithFrame:CGRectMake(220, 500, 60, 40)];
104     stepper.tag = 105;
105     [stepper addTarget:self action:@selector(changeSize:) forControlEvents:UIControlEventValueChanged];
106     stepper.minimumValue =10;  // 最小高度
107     stepper.maximumValue = 210;// 最大高度
108     stepper.value = 0;     // 初始值
109     stepper.stepValue = 10;// 递变量10
110     [self.view addSubview:stepper];
111 }
112 
113 -(void)trigger:(UISegmentedControl*)segment{
114     
115     switch (segment.selectedSegmentIndex) {
116             
117         case 0:{
118             _aView = _redView;
119             UIStepper *stepper1 = (UIStepper *)[self.view viewWithTag:105];
120             stepper1.value = _aView.bounds.size.height;
121             //NSLog(@"%f",stepper1.value);
122             break;
123         }
124             
125         case 1:{
126             
127             _aView = _greenView;
128             UIStepper *stepper1 = (UIStepper *)[self.view viewWithTag:105];
129             stepper1.value = _aView.bounds.size.height;
130             // NSLog(@"%f",stepper1.value);
131             break;
132         }
133         default:
134             break;
135     }
136 }
137 
138 - (void)changeSize:(UIStepper *)stepper{
139 
140     if (stepper.value > _aView.bounds.size.height ) {
141         
142         _aView.bounds = CGRectMake(_aView.bounds.origin.x, _aView.bounds.origin.y, _aView.bounds.size.width, _aView.bounds.size.height + stepper.stepValue);
143     }
144     
145     if (stepper.value < _aView.bounds.size.height) {
146         
147         _aView.bounds = CGRectMake(_aView.bounds.origin.x, _aView.frame.origin.y , _aView.bounds.size.width, _aView.bounds.size.height - stepper.stepValue);
148     }
149 }
150 
151 - (void)changeColor:(UISlider *)slider{
152     if (slider.tag == 100) {
153         _red = slider.value;
154     }
155     if (slider.tag == 101) {
156         _green = slider.value;
157     }
158     if (slider.tag == 102) {
159         _blue = slider.value;
160     }
161     // 颜色
162     _aView.backgroundColor = [UIColor colorWithRed:_red green:_green blue:_blue alpha:1.0];
163 }
164 
165 - (void)showOrHideView:(UISwitch *)swit{
166     if (swit.on == YES) {
167         _aView.hidden = NO;
168     }
169     if (swit.on == NO) {
170         _aView.hidden = YES;
171     }
172 }
173 
174 @end
复制代码

运行效果:初始化 | 选中绿色视图(修改)

          

 

posted on   低头捡石頭  阅读(138)  评论(0编辑  收藏  举报

编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示