標籤:
1 // 2 // ViewController.m 3 // 其他常用控制項 4 // 5 // Created by 大歡 on 16/1/25. 6 // Copyright © 2016年 bjsxt. All rights reserved. 7 // 8 9 #import "ViewController.h" 10 11 @interface ViewController () 12 13 @property (weak, nonatomic) IBOutlet UILabel *switchLabel; 14 15 @end 16 17 @implementation ViewController 18 19 - (void)viewDidLoad { 20 [super viewDidLoad]; 21 22 // [self createSwitch]; 23 // [self createSegment]; 24 // [self createSlider]; 25 26 27 28 } 29 30 - (void)createSlider { 31 32 //滑塊 33 UISlider * slider = [[UISlider alloc] initWithFrame:CGRectMake(20, 100, CGRectGetWidth(self.view.frame) - 40,30 )]; 34 // slider.backgroundColor = [UIColor grayColor]; 35 slider.minimumValue = 10; 36 slider.maximumValue = 50; 37 //較小值軌道顏色 38 slider.minimumTrackTintColor = [UIColor greenColor]; 39 //較大值軌道顏色 40 slider.maximumTrackTintColor = [UIColor redColor]; 41 //拖動圓圈的顏色 42 slider.thumbTintColor = [UIColor yellowColor]; 43 slider.value = 30; 44 //能否連續調用valuechange 45 // slider.continuous = NO; 46 //最小端添加圖片 47 slider.minimumValueImage = [UIImage imageNamed:@"apply_sex_normal"]; 48 //最大端添加圖片 49 slider.maximumValueImage = [UIImage imageNamed:@"apply_sex_selected"]; 50 51 //滑塊的圖片 52 // [slider setThumbImage:[UIImage imageNamed:@"apply_sex_selected"] forState:UIControlStateNormal]; 53 // //較小端軌道的圖片 54 // [slider setMinimumTrackImage:[UIImage imageNamed:@"apply_sex_normal"] forState:UIControlStateNormal]; 55 56 [slider addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged]; 57 [self.view addSubview:slider]; 58 59 } 60 61 - (void)sliderAction:(UISlider *)slider { 62 63 self.switchLabel.font = [UIFont systemFontOfSize:slider.value]; 64 } 65 66 - (void)createSegment { 67 68 NSArray * array = @[@"red",@"green",@"yellow",@"blue",@"orange"]; 69 70 //分段選取器 71 UISegmentedControl * segment = [[UISegmentedControl alloc] initWithItems:array]; 72 segment.frame = CGRectMake(20, CGRectGetHeight(self.view.frame) - 100, CGRectGetWidth(self.view.frame) - 40, 30); 73 //是否能選中 74 segment.momentary = NO; 75 //文字適應寬度 76 segment.apportionsSegmentWidthsByContent = NO; 77 //插入段子 78 // [segment insertSegmentWithTitle:@"apple" atIndex:1 animated:YES]; 79 // [segment setImage:[UIImage imageNamed:@"onimage"] forSegmentAtIndex:2]; 80 segment.tintColor = [UIColor orangeColor]; 81 [segment addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged]; 82 [self.view addSubview:segment]; 83 84 } 85 86 - (void)segmentAction:(UISegmentedControl *)segment { 87 88 NSInteger index = segment.selectedSegmentIndex; 89 switch (index) { 90 case 0: 91 self.view.backgroundColor = [UIColor redColor]; 92 break; 93 case 1: 94 self.view.backgroundColor = [UIColor greenColor]; 95 break; 96 case 2: 97 self.view.backgroundColor = [UIColor yellowColor]; 98 break; 99 case 3:100 self.view.backgroundColor = [UIColor blueColor];101 break;102 case 4:103 self.view.backgroundColor = [UIColor orangeColor];104 break;105 default:106 break;107 }108 }109 110 - (void)createSwitch {111 112 //開關113 UISwitch * sw = [[UISwitch alloc] initWithFrame:CGRectMake(100, 100, 0, 0)];114 //開關的狀態115 sw.on = YES;116 //開關開啟時的顏色117 sw.onTintColor = [UIColor redColor];118 //開關關閉時的顏色119 sw.tintColor = [UIColor cyanColor];120 //開關圓圈的顏色121 sw.thumbTintColor = [UIColor yellowColor];122 123 //適用於iOS6.0124 // sw.onImage = [UIImage imageNamed:@"onimage"];125 // sw.offImage = [UIImage imageNamed:@"onimage"];126 127 //UIControlEventValueChanged128 129 [sw addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];130 131 [self.view addSubview:sw];132 133 }134 135 - (void)switchAction:(UISwitch *)sw {136 137 if (sw.on) {138 self.switchLabel.hidden = NO;139 } else {140 self.switchLabel.hidden = YES;141 }142 143 }144 145 @end
iOS學習-其他常用控制項