苹果的来袭, 正在改变着我们的生活,最近发现,周围想学 IOS 开发的人渐渐多了起来, 于是,总结了些UI控件的一些基本东西,UIslider, UISementedControl, Switch, UIStepper的基本应用,希望对他们能够帮助.
声明 在 MRC 在手动控制内存机制下进行
// 进度条
UISlider *slider = [[UISlider alloc] initWithFrame:CGRectMake(0, 600, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
slider.minimumValue = 0; //最小值
slider.maximumValue = 5.0f; //最大值
slider.minimumTrackTintColor = [UIColor yellowColor]; //进行过的进度条颜色
slider.maximumTrackTintColor = [UIColor greenColor]; //未进行过的进度条颜色
slider.thumbTintColor = [UIColor redColor]; //控制按钮的颜色
[self.view addSubview:slider];
[slider release];
[slider addTarget:self action:@selector(slider:) forControlEvents:UIControlEventValueChanged]; //target action 方法
- (void)slider:(UISlider *)sli {
UIImageView *imageV = (UIImageView *)[self.view viewWithTag:1000];
[imageV setAnimationDuration:sli.value]; //根据value读取进度
[imageV startAnimating]; //开始播放
}
// 分段控制器
NSArray *str = [NSArray arrayWithObjects:@"1",@"2",@"3", nil];
UISegmentedControl *segmented = [[UISegmentedControl alloc] initWithItems:str];
segmented.frame = CGRectMake(30, 50, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
segmented.backgroundColor = [UIColor yellowColor];
segmented.selectedSegmentIndex = 0; //设置当前所选择的分段
segmented.tintColor = [UIColor blackColor]; //设置主色调
[segmented setImage:[UIImage imageNamed:@"2.png"] forSegmentAtIndex:2]; //在指定位置插入图片
[self.view addSubview:segmented];
[segmented release];
[segmented addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged]; //添加事件
- (void)segmentAction:(UISegmentedControl *) seg {
NSLog(@"%ld", seg.selectedSegmentIndex);
}
// UISwitch 开关
UISwitch *sw = [[UISwitch alloc] initWithFrame:CGRectMake(0, 350, 375, 667)];
sw.backgroundColor = [UIColor redColor]; //设置背景颜色
sw.thumbTintColor = [UIColor grayColor];
sw.tintColor = [UIColor purpleColor];
sw.onTintColor = [UIColor blueColor];
[sw addTarget:self action:@selector(switchActin:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:sw];
[sw release];
- (void)switchActin:(UISwitch *)sw {
// switch 只有两种状态
NSLog(@"%d", sw.isOn);
}
// - / +
UIStepper *step = [[UIStepper alloc] initWithFrame:CGRectMake(0, 500, 100, 20)];
step.backgroundColor = [UIColor purpleColor];
[self.view addSubview:step];
[step release];
// 默认最小值为0, 最大值为 100
step.value = 100;
step.maximumValue = 200;
step.minimumValue = 0;
// 默认增加值为 1
step.stepValue = 5;
[step addTarget:self action:@selector(stepper:) forControlEvents:UIControlEventValueChanged];
- (void)stepper:(UIStepper *)step {
NSLog(@"%f", step.value);
}