使用UISlider实现指定范围选择
创建一个滑动条并且把它放在试图控制器的视图中。
@interface SliderView:UIViewController
@property(non atomic,strong)UISlider *mySlider;
@end
@synthesize mySlider;
-(void)viewDidLoad{
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
self.mySlider = [ [UISlider alloc] initWithFrame:CGRectMake(0.0f,0.0f,200.0f,23.0f)];
self.mySlider.center = self.view.center;
self.mySlider.minimumValue = 0.0f;//选择范围的最小值
self.mySlider.maximumValue= 100.0f;//选择范围的最大值
self.mySlider.value = self.mySlider.maximumValue / 2.0;//当前值
self.mySlider.continuous = NO;//默认YES,若为YES,thumb滑动时会连续调用slider d target。
[self.mySlider addTarget:self action:@selecter(sliderValueChanged:) forControlEvents:UIControlEventValueChanged];
[self.view addSubView:self.mySlider];
}
slider上的小滑块叫做thumb;
target
-(void)sliderValueChanged:(UISlider *)paramSender{
if([paramSender isEqual:self.mySlider]){
NSLog(@"%@",paramSender.value);
}
}
slider 的 thumb 图片选中设置
setThumbImage:forState:方法
UIControlStateNormal //正常状态下的thumb ,即没有点击的。
UIControlStateHighlighted //选中状态的thumb,即用户点击thumb时显示的图片
[self.mySlider setThumbImage:[UIImage imaged:@"ThumbNormal.png"] forState:UIControlStateNormal];
[self.mySlider setThumbImage:[UIImage imaged:@"ThumbHighlighted.png"] forState:UIControlStateHighlighted];
UISegmentedControl 做简单选项分组
@interface SegmentedControl :UIViewController
@property(non atomic,strong)UISegmentedControl *mySegmentedControl;
@end;
@synthesize mySegmentedControl;
-(void)viewDidLoad{
[super viewDidLoad];
self.view.backgrounderColor = [UIColor whiteColor];
NSArray *segments = [[NSArray alloc] initWithObjects:@"iPhone",@"iPad",nil];
self.mySegmentedControl = [[UISegmentedControl alloc]initWithItems:segments];
self.mySegmentedControl.center = self.view.center;
[self.view addSubview:self.mySegmentedControl];
}
通过一组string来初始化控件中要显示的不同选项,还可以通过initWithObjects:initializer 方法来初始化控件,参数可以为string还可以为图片。
怎么知道选中哪一个了呢?答案很简单,添加一个target
-(void)segmentChanged:(UISegmentedControl *)paramSender{
if([paramSender isEqual:self.mySegmentedControl]){
NSInteger selectedSegmentIndex = [paramSender selectedSegmentIndex];
NSString *selectedSegmentText = [paramSender titleForSegmentAtIndex:selectedSegmentIndex];
NSLog(@"Segment %ld with %@ text is selected",(long)selectedSegmentIndex,selectedSegmentText);
}
}
在viewDidLoad 里面add后面加上这句:
[self.mySegmentedControl addTarget:self action:@selector(segmentChanged:) forControlEvents:UIControlEventValueChanged];
运行程序你会得到以下结果(从左到右点击)
Segment 0 with One text is selected
Segment 1 with Two text is selected
Segment 2 with Three text is selected
Segment 3 with Four text is selected
selectedSegmentIndex 方法找到当前选项中的索引
titleForSegmentAtIndex 方法得到对应文本
你可能注意到了,一旦用户选中了一个选项,那个选项会一直保持着选中状态,那么怎么才能选中之后该选项马上变回原始状态呢,有办法请看:
self.mySegmentedControl.momentary = YES;
想在控件上面加载图片可以这样:
-(void)viewDidLoad{
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
NSArray *segments = [ [NSArray alloc]initWithObjects:@"One",@"Two",[UIImage imageNamed:@"Three.png"],@"Four",nil];
self.mySegmentedControl = [ [UISegmentedControl alloc]initWithItems:segments];
CGRectsegmentedFrame = self.mySegmentedControl.frame;
segmentedFrame.size.height = 64.0f;
segmentedFrame.size.width = 300.0f;
self.mySegmentedControl.frame = segmentedFrame;
self.mySegmentedControl.center = self.view.center;
[self.view addSubview:self.mySegmentedControl];
}
分段控件的空间风格:
typedef enum{
UISegmentedControlStylePlain,
UISegmentedControlStyleBordered,
UISegmentedControlStyleBar,
UISegmentedControlStyleBezeled ,//接近矩形
}UISegmentedControlStyle;