// UISwitch开关的使用
UISwitch *sw=[[UISwitch alloc]initWithFrame:CGRectMake(100,65, 100,50)];//创建switch,并设置大小
[sw addTarget:selfaction:@selector(change:) forControlEvents:UIControlEventValueChanged];//创建事件
[self.view addSubview:sw];//添加到父视图
//UISegmentedControl的使用
//设置标题和个数数组
NSArray *ary=@[@"1",@"2",@"3",@"4"];
UISegmentedControl *seg=[[UISegmentedControl alloc]initWithItems:ary];
seg.frame=CGRectMake(50, 100, 200, 50);
//设置seg的标题和图片,都起到类似标题的作用,没有改变点击后的背景效果
[seg setTitle:@"123"forSegmentAtIndex:0];
[seg setImage:[UIImage imageNamed:@"Airplane"] forSegmentAtIndex:0];
//设置整个segde背景图片
//[seg setBackgroundImage:[UIImageimageNamed:@"Airplane"] forState:UIControlStateNormalbarMetrics:UIBarMetricsDefault];
//下面这是添加图片的时候所用方法,但不是添加背景图片,而是类似于标题的图片,用不熟练的话很困惑的。
//[seg insertSegmentWithImage:[UIImageimageNamed:@"Airplane"] atIndex:0 animated:YES];
//seg.segmentedControlStyle=UISegmentedControlStylePlain;//设置样式
seg.segmentedControlStyle=UISegmentedControlStyleBar;
//当样式设为bar时,这个时候,setTintColor才有效果,否则无效
[seg setTintColor:[UIColor redColor]];//seg的背景颜色
[seg setSelectedSegmentIndex:0];//首先显示index=0的内容
seg.momentary=NO;//设置在点击后是否恢复原样
seg.multipleTouchEnabled=NO;//可触摸
[seg addTarget:selfaction:@selector(selectBtn:) forControlEvents:UIControlEventValueChanged];//添加事件
//创建集合,体现标题如果是文字的话,可以设置字号,字体,颜色等
NSDictionary *dic=[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],UITextAttributeTextColor,[UIFont fontWithName:@"SnellRoundhand-Bold" size:24],UITextAttributeFont ,nil];
[seg setTitleTextAttributes:dic forState:UIControlStateNormal];
[self.view addSubview:seg];
//UISlider的使用
UISlider *slider=[[UISlider alloc]initWithFrame:CGRectMake(50, 150, 150, 50)];//创建slider滑块,并指定位置大小
slider.backgroundColor=[UIColor clearColor];//背景颜色
slider.minimumValue=0.2;//设置最小值
slider.maximumValue=1;//设置最大值
slider.value=1;//设置当前值
self.view.alpha=1;//设置当前视图的透明度
[slider addTarget:selfaction:@selector(sder:) forControlEvents:UIControlEventValueChanged];//事件
[self.view addSubview:slider];
//UIPickerView的使用
pickView=[[UIPickerView alloc]initWithFrame:CGRectMake(0,200, 320,216)];
pickView.delegate=self;//设置代理
pickView.dataSource=self;
pickView.showsSelectionIndicator=YES;
contentAry=[[NSArray alloc]initWithObjects:@"水瓶座",@"双鱼座",@"天蝎座",@"白羊座",@"金牛座",@"双子座",@"巨蟹座",@"射手座",@"处女座",@"天秤座",@"狮子座",@"摩羯座",nil];
lab1=[[UILabel alloc]initWithFrame:CGRectMake(0,0, 320,58)];
lab1.backgroundColor=[UIColor redColor];
[self.view addSubview:lab1];
[self.view addSubview:pickView];
//UIDatePicker日历控件的使用
datePick=[[UIDatePicker alloc]initWithFrame:CGRectMake(0,200, 320,480)];//创建datepicker日历控件
//设置日历控件显示日期为当前日期
datePick.date=[NSDate date];
datePick.datePickerMode=UIDatePickerModeDateAndTime;//日期选择模式
[self.viewaddSubview:datePick];
#pragma mark 返回显示的列数
//返回显示的列数
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView*)pickerView{
return 1;
}
#pragma mark 返回当前显示的列数
//返回当前显示的列数,不用的话会崩溃
-(NSInteger)pickerView:(UIPickerView *)pickerViewnumberOfRowsInComponent:(NSInteger)component{
lab1.text=@"水瓶座";//初始设置为显示数组的第一个元素,否则刚开始会默认为空
return [contentAry count];
}
#pragma mark 设置当前列显示的行数
//设置当前列显示的行数,不用的话会显示问号
-(NSString*)pickerView:(UIPickerView*)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
return [contentAry objectAtIndex:row];
}
#pragma mark 点击你所选择的行数
//点击你所选择的行数,不用里面的数据的话,lab1的内容不会改变。
-(void)pickerView:(UIPickerView*)pickerView didSelectRow:(NSInteger)rowinComponent:(NSInteger)component{
NSString *result=nil;
result=[contentAryobjectAtIndex:row];
NSLog(@"result:%@",result);
lab1.text=result;
}
#pragma mark 滑块控件sder事件
-(void)sder:(UISlider*)sender{
//value滑块控件的当前值,它在最大值和最小值之间0~1
//minimumValue 滑块控件的最小值
//maximumValue 滑块控件的最大值
self.view.alpha=sender.value;//设置让当前页面的透明度等于滑块的当前值
}
#pragma mark selectBtn事件
-(void)selectBtn:(UISegmentedControl*)sender{
//选择段下标
int index=sender.selectedSegmentIndex;
NSLog(@"%d,%@",index,[sendertitleForSegmentAtIndex:index]);
}
#pragma mark change事件
-(void)change:(UISwitch*)sender{
//on BOOL值,获取/设置开关状态
//-(void)setON:(BOOL)onanimated:(BOOL)animated 动画设置控件的开关状态
//UIControlEventValueChanged 事件。用户点击开关控件,会触发此事件!
if(sender.on) {//当BOOL值为on时,执行orangeColor
self.view.backgroundColor=[UIColororangeColor];
}else if(sender){//当BOOL值不为on时,执行grayColor
self.view.backgroundColor=[UIColorgrayColor];
}
}