【学习ios之路:UI系列】ios中常见的几种控件.(UISlider,UISwitch,UIStepper,UISegmentedControl)
一.滑块控件(UISlider) 效果如下
注:系统原来效果如下
具体功能运用:
①创建滑块控件(UISlider),设置控件大小,并添加到window视图上,后释放
UISlider *slider = [[UISlider alloc] initWithFrame:CGRectMake(20, 30, 280, 100)]; [self.window addSubview:slider]; [slider release];②设置最大值,最小值,以及开始时滑动条所在的位置
//设置最大值10 [slider setMaximumValue:10]; //设置最小值0 [slider setMinimumValue:0]; slider.value = 5;//当前位置是4.显示效果如上图.③设置滑动条的颜色,即滑动前和滑动后的颜色
slider.minimumTrackTintColor = [UIColor orangeColor];//设置移动前线条的颜色 slider.maximumTrackTintColor = [UIColor blackColor];//设置移动后线条的颜色④设置滑动控件左右的图片
slider.minimumValueImage = [UIImage imageNamed:@"[email protected]"];//设置滚动条最左边的图片 slider.maximumValueImage = [UIImage imageNamed:@"[email protected]"];//设置滚动条最右边的图片⑤设置手指点击时按钮的颜色
slider.thumbTintColor = [UIColor grayColor];//设置中间按钮移动按钮的颜色⑥将按钮换成图片,正常状态下
[slider setThumbImage:[UIImage imageNamed:@"[email protected]"] forState:UIControlStateNormal];⑦移动时触发的响应事件
1).触发状态为:UIControlEventValueChanged,当触发时,slider.value的值会随之改变
2)当UISlider的进度值发生改变时,会自动调用self的function:方法,并且将UISlider作为第一个参数传入.
[slider addTarget:self action:@selector(function:) forControlEvents:UIControlEventValueChanged];⑧其他功能
//设置轴迹图片 - (void)setMinimumTrackImage:(UIImage *)image forState:(UIControlState)state; - (void)setMaximumTrackImage:(UIImage *)image forState:(UIControlState)state; //通过状态获取点击按钮的图片 - (UIImage *)thumbImageForState:(UIControlState)state; //通过事件触发状态获取足迹图片. - (UIImage *)minimumTrackImageForState:(UIControlState)state; - (UIImage *)maximumTrackImageForState:(UIControlState)state; //获取当前状态时的对应要的图片 UIImage *image = slider.currentThumbImage; <span style="font-size:18px;"></span><pre name="code" class="objc">UIImage *image = slider.currentMinimumTrackImage;UIImage *image = slider.currentMaximumTrackImage;
二.开关控件(UISwitch) 效果如下
开关控件的常见操作:
①创建开关控件,设置大小,添加到视图上,并释放
UISwitch *switchView = [[UISwitch alloc] initWithFrame:CGRectMake(50, 100, 280, 100)]; [self.window addSubview:switchView]; [switchView release];②设置开关状态
BOOL setting = switchView.isOn;//获得开关状态 [switchView setOn:setting animated:YES];//设置开关状态③设置颜色
//<span style="font-size:14px;">开关控件切换槽<span class="s6"> </span>颜色</span>.如将上面的绿色替换成橘黄色 switchView.onTintColor = [UIColor orangeColor]; //设置按钮的颜色 switchView.thumbTintColor = [UIColor redColor]; //<span style="font-size:14px;">开关控件外部的颜色</span> switchView.tintColor = [UIColor purpleColor];④设置图片(在ios7 中无效果)
switchView.onImage = [UIImage imageNamed:@"[email protected]"]; switchView.offImage = [UIImage imageNamed:@"[email protected]"];⑤开关控件触发的事件处理
[switchView addTarget:self action:@selector(switchTag:) forControlEvents:UIControlEventValueChanged];实现代码如下switchTag:
- (void)switchTag:(UISwitch *)switchTag { BOOL setting = switchTag.isOn; [swithch setOn:setting animated:YES]; if (setting) { NSLog(@"on"); }else { NSLog(@"off"); } }三.计数器控件(UIStepper)效果如下
创建功能如下
①创建计数器控件,并释放
UIStepper *step = [[UIStepper alloc] init]; step.frame = CGRectMake(50, 300, 300, 300); [self.window addSubview:step]; [step release];②设置最大值和最小值,以及每次加减时的值
每按一次减号,value就会自动减去一定的数值(数值大小由stepValue决定)
每按一次加号,value就会自动增加一定的数值(数值大小由stepValue决定)
step.maximumValue = 1; step.minimumValue = 0.1; step.stepValue = 0.1;//每次增减的值③设置是否循环和是否连续
step.continuous = YES;//默认是yes step.wraps = YES;//判断是否循环,即到最大值后又从最小值开始.④设置颜色,如上图的紫色
step.tintColor = [UIColor purpleColor];//设置按钮的颜色; [step setBackgroundColor:[UIColor orangeColor]];//设置背景颜色 [step setBackgroundImage:[UIImage imageNamed:@"[email protected]"] forState:UIControlStateNormal];⑤其它操作
//设置背景图片,再某个状态时 - (void)setBackgroundImage:(UIImage*)image forState:(UIControlState)state //在某个状态时,获取背景图片, - (UIImage*)backgroundImageForState:(UIControlState)state⑥数字控件的事件处理
[step addTarget:self action:@selector(stepTag:) forControlEvents:UIControlEventValueChanged]; //事件响应方法实现 - (void)stepTag:(UIStepper *)stepper { [self.window viewWithTag:200].alpha = stepper.value; }四. 选项卡控件UISegmentedControl,效果图如下
实现操作如下:
NSArray *arr = [NSArray arrayWithObjects: @"轻拍", @"长按", @"轻扫", @"平移", @"捏合", @"旋转", nil]; UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:arr]; segmentedControl.frame = CGRectMake(10, 40, 300, 40); [segmentedControl addTarget:self action:@selector(function:) forControlEvents:UIControlEventValueChanged]; [self.window addSubview:segmentedControl];其他操作
- (void)removeSegmentAtIndex:(NSUInteger)segment animated:(BOOL)animated; - (void)removeAllSegments;//输出所有选项卡 <span style="font-size:18px;"></span><pre name="code" class="objc"> segmentedControl.tintColor = [UIColor orangeColor];//设置边框颜色 <span style="font-size:18px;"></span><pre name="code" class="objc"><pre name="code" class="objc"> NSInteger count = segmentedControl.numberOfSegments;//计算个数
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。