1.UISwitch 开关控件
2.UIActivityIndicatorView 活动提示视图
3.UISlider 滑块控件
4.UISegmentedControl 分段选择
5.UIStepper 计数器/步进器
6.UIProgrecessView 进度条
7.UITextView 文本视图
8.UIActionSheet 动作列表
9.UIAlertView 警告视图
10. 扩展知识
UISwitch 开关控件

创建显示
UISwitch *mySwich=[[UISwitch alloc]initWithFrame:CGRectMake(100, 100, 100, 30)];
[self.view addSubview:mySwich];
注意:宽度和高度对控件没有影响。
控件的大小是固定的 width是51;higth是31;
属性;
状态是真还是假 一开始启动时的状态。
mySwich.on=YES;
设置关状态下的颜色
mySwich.tintColor=[UIColor redColor];
设置开状态下的颜色
mySwich.onTintColor=[UIColor blueColor];
设置圆点的颜色
mySwich.thumbTintColor=[UIColor yellowColor];
事件处理
细节:事件类型—ValueChanged
[mySwich addTarget:self action:@selector(dealSwith:) forControlEvents:UIControlEventValueChanged];
UIActivityIndicatorView 活动提示视图

需求:在网路下载中,提示用户正在下载中 ,那么我们就可以使用活动指示器。
self.view.backgroundColor=[UIColor colorWithRed:1 green:1 blue:1 alpha:0.5];
创建添加
UIActivityIndicatorView *aiv=[[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
UIActivityIndicatorViewStyleWhiteLarge,白的大号的
UIActivityIndicatorViewStyleWhite,白的正常号的
UIActivityIndicatorViewStyleGray,灰的。
[self.view addSubview:aiv];
只有中心,没有大小
aiv.center=CGPointMake(160, 200);
注意:要有开始动画
[aiv startAnimating];
设置颜色
aiv.color=[UIColor purpleColor];
模拟5s之后下载完成
5s后执行方法dealTimerout:
[NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(dealTimerout:) userInfo:aiv repeats:NO];
}
-(void)dealTimerout:(NSTimer *)timer
{
UIActivityIndicatorView *aiv=timer.userInfo;
[aiv stopAnimating];
}
UISlider 滑块控件

创建
UISlider *slider=[[UISlider alloc]initWithFrame:CGRectMake(100, 100, 200, 30)];
[self.view addSubview:slider];
注意:控件的高度是不变的higth=30;
属性
设置最大值
slider.maximumValue=100;
最小值
slider.minimumValue=1;
当前值
slider.value =10;
事件处理
[slider addTarget:self action:@selector(dealSlider:) forControlEvents:UIControlEventValueChanged];
}
-(void)dealSlider:(UISlider *)s
{
NSLog(@”value=%f”,s.value);
}
UISegmentedControl 分段选择

需求:游戏中选择种族
UISegmentedControl *sc=[[UISegmentedControl alloc]initWithItems:@[@”人族”,@”精灵族”,@”矮人”,@”兽人”,@”迈雅”]];
sc.frame=CGRectMake(10, 100, 300, 30);
[self.view addSubview:sc];
属性
当前选择项
sc.selectedSegmentIndex=2;
设置色调
sc.tintColor=[UIColor redColor];
插入一个段
[sc insertSegmentWithTitle:@”亡灵族” atIndex:5 animated:YES];
事件处理
[sc addTarget:self action:@selector(dealSc:) forControlEvents:UIControlEventValueChanged];
}
-(void)dealSc:(UISegmentedControl *)sc
{
NSLog(@”sc=%d”,sc.selectedSegmentIndex);
}
UIStepper 计数器/步进器

需求:购买界面上添加控件,选择构面数量
UIStepper *stepper=[[UIStepper alloc]init];
stepper.center=CGPointMake(250, 100);
[self.view addSubview:stepper];
[stepper addTarget:self action:@selector(dealSteeper:) forControlEvents:UIControlEventValueChanged];
stepper.minimumValue=1;
stepper.maximumValue=100;
stepper.value=1;
textField=[[UITextField alloc]initWithFrame:CGRectMake(10, 85, 190, 30)];
textField.borderStyle=UITextBorderStyleRoundedRect;
textField.text=@”1”;
textField.delegate=self;
[self.view addSubview:textField];
}
-(void)dealSteeper:(UIStepper *)steeper
{
NSLog(@”%f”,steeper.value);
textField.text=[NSString stringWithFormat:@”%d”,(int)steeper.value];
}
UIProcessView 进度条

常用显示耗时任务,的完成进度
数据下载,载入文件,游戏加载
UIProgressView *progressView=[[UIProgressView alloc]initWithProgressViewStyle:UIProgressViewStyleDefault];
progressView.frame=CGRectMake(20, 100, 280, 30);
[self.view addSubview:progressView];
模拟网路下载
[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(dealProgress:) userInfo:progressView repeats:YES];
}
-(void)dealProgress:(NSTimer *)timer
{
UIProgressView *pv=timer.userInfo;
进度条当前进度0.0~1.0;
pv.progress+=0.02;
}
UITextView 文本视图
textView=[[UITextView alloc]initWithFrame:CGRectMake(10, 40, 300, 430)];
[self.view addSubview:textView];
textView.text=@”致虚级,守静笃,万物并作,无以归复。致虚级,守静笃,万物并作,无以归复。”;
textView.font=[UIFont systemFontOfSize:24];
如何回收键盘
输入框按return时候不是结束,而是换行
UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
textView.inputAccessoryView=view;
UIButton *button=[UIButton buttonWithType:UIButtonTypeSystem];
button.frame=CGRectMake(260, 7, 60, 30);
[button setTitle:@”返回” forState:UIControlStateNormal];
[button addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
[view addSubview:button];
}
-(void)btnClick:(UIButton *)button
{
[textView resignFirstResponder];
}
UIActionSheet 动作列表

UIButton *shareButton=[UIButton buttonWithType:UIButtonTypeSystem];
[self.view addSubview:shareButton];
[shareButton setTitle:@”分享” forState:UIControlStateNormal];
shareButton.frame=CGRectMake(100, 100, 100, 30);
[shareButton addTarget:self action:@selector(dealShare:) forControlEvents:UIControlEventTouchUpInside];
}
-(void)dealShare:(UIButton *)button
{
UIActionSheet *actionSheet=[[UIActionSheet alloc]initWithTitle:@”分享” delegate:self cancelButtonTitle:@”取消” destructiveButtonTitle:nil otherButtonTitles:@”新浪微博”,@”微信圈子”,@”微信好友”,@”邮件”,@”短信”, nil];
显示出来。
[actionSheet showInView:self.view];
}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
可以知道点击了第几项。
NSLog(@”点击了第%d个按钮”,buttonIndex);
}
UIAlertView 警告视图
可以输入用户名和密码的输入
UIAlertView *alertView = [[UIAlertView alloc] init];
alertView.title = @”输入用户名和密码”;
alertView.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
[alertView addButtonWithTitle:@”取消”];
[alertView addButtonWithTitle:@”完成”];
[alertView show];
alertView.delegate = self;
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;
{
UITextField *nameText = [alertView textFieldAtIndex:0];
UITextField *passwordText = [alertView textFieldAtIndex:1];
NSLog(@”name:%@ pass:%@”,nameText.text,passwordText.text);
}