5.UISlider 选择器
- (void)addSlider
{ //创建UISlider对象
UISlider *pSlider = [[UISlider alloc]initWithFrame:CGRectMake(10, 195, 300, 20)];
//设置最小值
pSlider.minimumValue = 0;
//最大值
pSlider.maximumValue = 100;
//是否可以持续滑动
pSlider.continuous = YES;
//设置初始值
pSlider.value = 50;
//透明度
self.pImageView.alpha = pSlider.value/100;
//最小值处的图片
pSlider.minimumValueImage = [UIImage imageNamed:@"apple_min"];//最小处的图片
//最大值处的图片
pSlider.maximumValueImage = [UIImage imageNamed:@"apple_max.png"];//最大处的图片
//设置当前的图片
[pSlider setThumbImage:[UIImage imageNamed:@"apple_thumb.png"] forState:UIControlStateNormal];//当前状态的图片
//添加对应动作,注意:CotrolEvents:valueChanged
[pSlider addTarget:self action:@selector(sliderValueChange:) forControlEvents:UIControlEventValueChanged]; //选择器事件
[self.view addSubview:pSlider];
//创建显示当前值的label
self.pLabel = [[UILabel alloc]initWithFrame:CGRectMake(180, 150, 86, 25)];
//设置背景色
self.pLabel.backgroundColor = [UIColor lightGrayColor];
//设置当前文本内容
self.pLabel.text = [NSString stringWithFormat:@"亮度:%%%d",(int)pSlider.value];
[self.view addSubview:self.pLabel];
}
#pragma mark-----------Slider SEL-----------------
- (void)sliderValueChange:(UISlider *)slider
{
//显示当前亮度
self.pLabel.text = [NSString stringWithFormat:@"亮度:%%%d",(int)slider.value];
//透明度
self.pImageView.alpha = slider.value/100;
}
这个例子中添加很多关于 slider中的最大值最小值图标和显示图标的自定义.可以作为参考
6.UISwitch 开关
//添加开关
- (void)addSwitch
{
UISwitch *pSwitch = [[UISwitch alloc]initWithFrame:CGRectMake(220, 10, 10, 50)];
//pSwitch.backgroundColor = [UIColor redColor];
//给开关添加关联动作
[pSwitch addTarget:self action:@selector(switchMethod:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:pSwitch];
}
#pragma mark--------------Switch SEL------------------
- (void)switchMethod:(id)sender
{
//找到开关
UISwitch *pSwitch = (UISwitch *)sender;
if (pSwitch.isOn)//根据开关的状态设置背景图hidden
{
[self.pImageView setHidden:NO];
}
else
{
[self.pImageView setHidden:YES];
}
}
7.UISegmentControl
- (void)addSegmentControl
{ //创建数组
NSArray *pArr = [NSArray arrayWithObjects:@"1",@"2", nil];
//根据数组对象初始化SegmentControl对象
UISegmentedControl *pSegment = [[UISegmentedControl alloc]initWithItems:pArr];
[pSegment setFrame:CGRectMake(10, 240, 300, 40)];
//设置该分段控件的Style
[pSegment setSegmentedControlStyle:UISegmentedControlStyleBordered];
//设置分段控件选择的段数
[pSegment setSelectedSegmentIndex:0];
//添加关联动作
[pSegment addTarget:self action:@selector(segmentMethod:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:pSegment];
}
#pragma mark-----------------Segment SEL--------------------
- (void)segmentMethod:(UISegmentedControl *)segment
{
NSString *pMessage = [NSString stringWithFormat:@"你选择了第%d段,该段宽度已调整",segment.selectedSegmentIndex];
for (int i = 0; i < [segment numberOfSegments]; i++)
{ //根据你选择的分段的Index进行响应的操作
if (i == [segment selectedSegmentIndex])
{
[segment setWidth:180 forSegmentAtIndex:i];
}
else
{
[segment setWidth:120 forSegmentAtIndex:i];
}
}
//警告框
UIAlertView *pAlert = [[UIAlertView alloc]initWithTitle:@"通知" message:pMessage delegate:nil cancelButtonTitle:@"确认" otherButtonTitles: nil];
[pAlert show];
[pAlert release];
}
8.加载显示器
addActivityIndicatorView
//其他控件 //提示正在加载文件的太阳光加载显示按钮
- (void)addActivityIndicatorView
{
//创建初始化ActivityIndicatorView(Style)
self.pActivity = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
[self.pActivity setFrame:CGRectMake(10, 280, 45, 45)];
//设置是否隐藏
self.pActivity.hidden = NO;
[self.view addSubview:self.pActivity];
//创建按钮,关联对应方法
UIButton *pBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[pBtn setFrame:CGRectMake(10, 330, 45,30 )];
[pBtn setTitle:@"转" forState:UIControlStateNormal];
[pBtn addTarget:self action:@selector(doRotate:) forControlEvents:UIControlEventTouchUpInside];
pBtn.tag = 110;
[self.view addSubview:pBtn];
}
- (void)doRotate:(id)sender
{
UIButton *pBtn = (UIButton *)sender;
[self.pActivity isAnimating]?[self.pActivity stopAnimating]:[self.pActivity startAnimating];
//根据Activity是否运行来设置按钮的title
[pBtn setTitle:(self.pActivity.isAnimating?@"停":@"转")forState:UIControlStateNormal];
self.pActivity.hidden = NO;
}
9.进度条的显示读取
//进度条读取
- (void)addProgress
{ //创建进度条并初始化
self.pProgress = [[UIProgressView alloc]initWithFrame:CGRectMake(10, 360, 300, 30)];
//设置Style
self.pProgress.progressViewStyle = UIProgressViewStyleBar;
//分别设置进度条完成未完成的颜色
self.pProgress.trackTintColor = [UIColor redColor];
self.pProgress.progressTintColor = [UIColor greenColor];
[self.view addSubview:self.pProgress];
//创建用来显示进度的label
self.pProgressValueLabel = [[UILabel alloc]initWithFrame:CGRectMake(100, 370, 110, 30)];
self.pProgressValueLabel.backgroundColor = [UIColor clearColor];
self.pProgressValueLabel.text = [NSString stringWithFormat:@"加载:%%%.1f",self.progressValue*100];
[self.view addSubview:self.pProgressValueLabel];
//通过计时器完成循环调用
pTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(progressMethod:) userInfo:nil repeats:YES];
}
#pragma mark--------------NSTimer SEL----------------------
- (void)progressMethod:(id)sender
{
self.progressValue += 0.1;
self.pProgress.progress = self.progressValue;
self.pProgressValueLabel.text = [NSString stringWithFormat:@"加载:%%%.1f",self.progressValue*100];
//[self.pActivity startAnimating];
if (self.progressValue >= 1)
{
self.progressValue = 0;
self.pProgressValueLabel.text = @"加载完成";
//[self.pActivity stopAnimating];
//self.pActivity.hidden = NO;
//停止计时器
[pTimer invalidate];
}
}
这个设置比较有意思的是 我们使用了 Timer进行有规律的加载.如果针对实际情况,我们可能要根据我们的加载进度进行加载.