通用控件(UIControl、UISegmentControl、UISwitch、UISlider、UIButton)使用示例
基类控件UIControl、分段控件UISegmentControl、开关控间UISwitch、滑块控件UISlider、按钮控件UIButton
-(void) viewDidLoad{
//属性
enabled //默认启用
selected //选中空间是其属性置为YES
contentVerticalAlignment //垂直对齐方式 4种
contentHorizontalAlignment //水平对齐方式 4种
//事件通知
[myControl addTarget:myDelegate action:@"myActionMethod:" forControlEvents:UIControlEventValueChanged]; //17种ControlEvent事件
[myControl removeTarget:myDelegate action:nil forControlEvents:UIControlEventAllEvents]; //删除所有事件
NSSet *myActions = [myControl allTargets]; //获得一个控件所有指定动作的列表,返回一个NSSet
NSArray *myAction = [myControl actionsForTarget:myDelegate forControlEvent:UIControlEventValueChanged];//获取针对某一特定事件目标的全部动作列表
/*************分段控件UISegmentdControl*************/
UISegmentedControl *segmentedControl = [[UISegmentedControlalloc] initWithItems:nil];
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar; //控件风格小按钮,适合导航栏:
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBezeled; //控件风格
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBordered; //控件风格黑边的大白按钮,适合表格单元
segmentedControl.segmentedControlStyle = UISegmentedControlStylePlain; //控件风格灰边大白按钮,适合偏好设置单元
segmentedControl.tintColor = [UIColor blueColor]; //渲染色彩
//添加片段,从0单元开始 ,可加入标题或图片两种情况
[segmentedControl insertSegmentWithTitle:@"First"atIndex:0animated:NO];
[segmentedControl insertSegmentWithTitle:@"Second"atIndex:1animated:NO];
UIImage *myImage = [UIImage imageNamed:@"1.png"];
[segmentedControl insertSegmentWithImage:myImage atIndex:2animated:NO];
//删除片段
[segmentedControl removeSegmentAtIndex:1 animated:YES]; //删除单个
[segmentedControl removeAllSegments]; //删除所有
//片段标题
[segmentedControl setTitle:@"Third" forSegmentAtIndex:2];//设置标题
NSString *myTitle = [segmentedControl titleForSegmentAtIndex:0];//读取标题
//图像
[segmentedControl setImage:[UIImageimageNamed:@"2.png"] forSegmentAtIndex:0];//设置图像
UIImage *myImage = [segmentedControl imageForSegmentAtIndex:3]; //读取图片
//设置片段宽度
[segmentedControl setWidth:64.0 forSegmentAtIndex:0];
//瞬时单击
segmentedControl.momentary = YES; //按钮被按下后很快恢复,默认为选中状态就一直保持
//初始化默认片段
segmentedControl.selectedSegmentIndex = 0; //初始指定第0个选中
//显示控件
[parentView addSubview:segmentedControl]; //添加到父视图
self.navigationItem.titleView = segmentedControl; //添加到导航栏(通过视图控制器)
//读取控件
int selectIndex = segmentedControl.selectedSegmentIndex; //对应当前被选总的片段号码
[segmentedControl addTarget:selfaction:@selector(controlPressed:) forControlEvents:UIControlEventValueChanged];
/*************开关控件UISwitch**************/
//创建控件
UISwitch *aSwitch = [UISwitch alloc] initWithFrame:CGRectMake(170, 5, 0, 0);//初始化,大小设置为0*0,表明将自动设定
//显示控件
[parentView addSubview: mySwitch]; //添加到父视图
self.navigationItem.titleView = mySwitch; //添加到导航栏(通过视图控制器)
//开关位置
BOOL *swithPosition = mySwitch.on; //读取开关状态,表示是否打开
[aSwitch setOn:YES animated:YES]; //设置打开
[aSwitch addTarget:selfaction:@selector(switchStatusChanged:) forControlEvents:UIControlEventValueChanged];//切换开关状态时触发动作
/***********滑块控件UISlider**************/
//创建控件
UISlider *slider = [[UISlider alloc] initWithFrame:CGRectMake(0, 0, 200, 0)];//高度可忽略
//设置控件范围, 默认为0.0--1.0
[slider setMinimumValue:0.0];
slider.maximumValue = 100.0;
//设置默认值
slider.value = 50.0;
//在控件一端显示图像
[slider setMinimumTrackImage:[UIImageimageNamed:@"min.png"] forState:UIControlStateNormal];//滑块可用状态 4种
[slider setMaximumTrackImage:[UIImageimageNamed:@"max.png"] forState:UIControlStateNormal];
//显示控件
[parentView addSubview: slider];//添加到父视图
self.navigationItem.titleView = slider;//添加到导航栏
//读取控件
float value = slider.value;
//滑块值变化时触发事件
[slider addTarget:selfaction:@selector(sliderValueChanged:) forControlEvents:UIControlEventValueChanged];
/*********按钮控件UIButton**********/
UIButton *myButton = [UIButtonbuttonWithType:UIButtonTypeRoundedRect]; //按钮风格,6种
myButton.frame = CGRectMake(25.0, 25.0, 100.0, 100.0);
//设置标题
[myButton setTitle:@"Click Here"forState:UIControlStateNormal];
//设置背景图像,设置后则只显示这个图片了
// [myButton setImage:[UIImage imageNamed:@"dealer_detail_background.png"] forState:UIControlStateNormal];
//设置标题的颜色
[myButton setTitleColor:[UIColorredColor] forState:UIControlStateNormal];
//设置标题的阴影
[myButton setTitleShadowColor:[UIColorgrayColor] forState:UIControlStateNormal];
//设置按钮的背景
[myButton setBackgroundImage:[UIImageimageNamed:@"cartype.png"] forState:UIControlStateNormal];
myButton.adjustsImageWhenHighlighted = NO; //默认情况下,按钮按下时图像颜色变淡, 要禁用则设为NO
myButton.adjustsImageWhenDisabled = NO; //默认情况下,按钮被禁用时图像颜色变深, 禁用则设为NO
myButton.showsTouchWhenHighlighted = YES; //可令按钮在按下时发光。可用于信息按钮或重要的按钮
[self.view addSubview:myButton];
}
//SegmentedControl触发的动作
-(void)controlPressed:(id)sender{
UISegmentedControl *control = (UISegmentedControl *)sender;
if (control == mySegmentedControl) {
int x = control.selectedSegmentIndex;
/*添加代码,对片段变化做出响应*/
}
}
//UISwitch触发的动作
-(void)switchStatusChanged:(id)sender{
UISwitch *control = (UISwitch *)sender;
if (control == mySwitch) {
BOOL ison = control.on;
/*添加代码,处理开关的状态*/
}
}
//UISlider触发的动作
-(void)sliderValueChanged:(id)sender{
UISlider *control = (UISlider *)sender;
if (control == mySlider) {
float value = control.value;
/*添加代码,对滑块的值做出响应*/
}
}