iOS階段學習第34天筆記(UI小工具 UISegment-UISlider-UIStepper-UIProgressView-UITextView介紹),uiprogressview
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 //移除某一個segment38 -(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 *)slider20 {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.030 pro.progress = 0.5; 31 pro.progressTintColor = [UIColor redColor]; 32 [self.view addSubview:pro];33 }34 35 //通過步進器改變進度條的值36 -(void)stepperAction:(UIStepper *)stepper37 {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 *)s30 {31 NSLog(@"%d",s.on);32 if (s.on) {33 //開啟活動指標34 [_activity startAnimating];35 }36 else37 {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 *)text16 {17 //文本變化範圍18 NSLog(@"%@",NSStringFromRange(range));19 //常值內容20 NSLog(@"%@",text); 21 return YES; 22 }