iOS阶段学习第34天笔记(UI小组件 UISegment-UISlider-UIStepper-UIProgressView-UITextView介绍)
iOS学习(UI)知识点整理
一、UI小组件
1、UISegmentedControl 分段选择器 实例代码
1 - (void)viewDidLoad { 2 [super viewDidLoad]; 3 //分段选择器 4 //在iOS6里,每个段的宽度会根据字数来决定 5 //iOS7以后,每个段的宽度相同,宽度取决于最长的字数 6 self.view.backgroundColor=[UIColor whiteColor]; 7 seg=[[UISegmentedControl alloc]initWithItems:@[@"消息",@"视频电话"]]; 8 self.navigationItem.titleView=seg; 9 //设置选中的某一个选项 10 seg.selectedSegmentIndex=1; 11 12 UIButton *button=[[UIButton alloc]init]; 13 button.frame=CGRectMake(80, 100, 200, 30); 14 button.backgroundColor=[UIColor blackColor]; 15 [button setTitle:@"添加" forState:UIControlStateNormal]; 16 [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; 17 [button addTarget:self action:@selector(touchButton:) forControlEvents:UIControlEventTouchUpInside]; 18 19 [self.view addSubview:button]; 20 UIButton *rmbutton=[[UIButton alloc]init]; 21 rmbutton.frame=CGRectMake(80, 200, 200, 30); 22 rmbutton.backgroundColor=[UIColor blackColor]; 23 [rmbutton setTitle:@"移除" forState:UIControlStateNormal]; 24 [rmbutton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; 25 [rmbutton addTarget:self action:@selector(touchRmButton:) forControlEvents:UIControlEventTouchUpInside]; 26 [self.view addSubview:rmbutton]; 27 //给seg添加点击事件的监听 28 [seg addTarget:self action:@selector(changeView:) forControlEvents:UIControlEventValueChanged]; 29 } 30 31 //插入一个图片组成的段 32 -(void)touchButton:(UIButton*)button{ 33 //[seg insertSegmentWithTitle:button.titleLabel.text atIndex:2 animated:YES]; 34 [seg insertSegmentWithImage:[UIImage imageNamed:@"005"] atIndex:2 animated:YES]; 35 36 } 37 //移除某一个segment 38 -(void)touchRmButton:(UIButton*)button{ 39 [seg removeSegmentAtIndex:1 animated:YES]; 40 } 41 //按钮点击事件 42 -(void)changeView:(UISegmentedControl*)mseg{ 43 NSLog(@"%li",mseg.selectedSegmentIndex); 44 }
效果图 如下:
2、UISlider 滑动条 实例代码
1 - (void)viewDidLoad { 2 [super viewDidLoad]; 3 //设置的高度无效 4 UISlider *slider = [[UISlider alloc]initWithFrame:CGRectMake(10, 100, 300, 200)]; 5 [self.view addSubview:slider]; 6 //滑块的最大值 7 slider.maximumValue = 1.0; 8 //滑块的最小值 9 slider.minimumValue = 0.0; 10 //滑块的当前值 11 slider.value = 0.1; 12 //是否连续:NO:在滑动过程中,不会响应方法,只有在停止滑动时,才会调用对应的事件方法 13 slider.continuous = YES; 14 [slider addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged]; 15 16 } 17 18 //滑动滑块改变视图背景色 19 -(void)sliderAction:(UISlider *)slider 20 { 21 NSLog(@"%f",slider.value); 22 self.view.backgroundColor = [UIColor colorWithRed:slider.value green:1.0 - slider.value blue:slider.value alpha:1]; 23 24 }
效果图 如下:
3、UIStepper和UIProgressView 步进器与进度条的使用 实例代码
1 @interface ViewController () 2 { 3 UIProgressView *_pro; 4 } 5 @end 6 7 @implementation ViewController 8 - (void)viewDidLoad { 9 [super viewDidLoad]; 10 11 //设置的size信息无效,大小是固定的 12 UIStepper *stepper = [[UIStepper alloc]initWithFrame:CGRectMake(10, 100, 300, 200)]; 13 //设置最大值 14 stepper.maximumValue = 100; 15 //设置最小值 16 stepper.minimumValue = 0; 17 //设置当前值 18 stepper.value = 10; 19 //设置步进值 20 stepper.stepValue = 5; 21 //设置颜色 22 stepper.tintColor = [UIColor orangeColor]; 23 [self.view addSubview:stepper]; 24 [stepper addTarget:self action:@selector(stepperAction:) forControlEvents:UIControlEventValueChanged]; 25 //进度条,高度无效 26 UIProgressView *pro = [[UIProgressView alloc]initWithFrame:CGRectMake(10, 200, 300, 100)]; 27 _pro = pro; 28 29 //值是0.0-1.0 30 pro.progress = 0.5; 31 pro.progressTintColor = [UIColor redColor]; 32 [self.view addSubview:pro]; 33 } 34 35 //通过步进器改变进度条的值 36 -(void)stepperAction:(UIStepper *)stepper 37 { 38 NSLog(@"%f",stepper.value); 39 _pro.progress = stepper.value / stepper.maximumValue; 40 }
效果图 如下:
4、UISwitch和UIActivityIndicatorView 开关与活动指示器的使用 实例代码
9 - (void)viewDidLoad { 10 [super viewDidLoad];
//注意页面背景色不能为白色,否则看不到活动器因为活动器本身为白色 12 self.view.backgroundColor = [UIColor blackColor]; 14 //长宽无效 15 UISwitch *s = [[UISwitch alloc]init]; 16 s.center = self.view.center; 17 //设置UISwitch的开关状态 18 s.on = YES; 20 [self.view addSubview:s]; 21 [s addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged]; 22 _activity = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]; 23 [self.view addSubview:_activity]; 24 _activity.center = CGPointMake(100, 100); 25 [_activity startAnimating]; } 28 29 -(void)switchAction:(UISwitch *)s 30 { 31 NSLog(@"%d",s.on); 32 if (s.on) { 33 //开启活动指示器 34 [_activity startAnimating]; 35 } 36 else 37 { 38 //关闭活动指示器 39 [_activity stopAnimating]; 40 } 41 }
效果图 如下:
5、UITextView 多行文本框 实例代码
1 - (void)viewDidLoad { 2 [super viewDidLoad]; 3 4 //解决多行文本框光标不从左上角开始问题 5 self.automaticallyAdjustsScrollViewInsets = NO; 6 //多行文本框 7 UITextView *tv = [[UITextView alloc]initWithFrame:CGRectMake(10, 100, 300, 100)]; 8 9 tv.backgroundColor = [UIColor grayColor]; 10 [self.view addSubview:tv]; 11 tv.delegate = self; 12 } 13 14 //文本框内容变化时触发;返回值表示能否修改内容 15 - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text 16 { 17 //文本变化范围 18 NSLog(@"%@",NSStringFromRange(range)); 19 //文本内容 20 NSLog(@"%@",text); 21 return YES; 22 }