iOS学习-其他常用控件
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.0 124 // sw.onImage = [UIImage imageNamed:@"onimage"]; 125 // sw.offImage = [UIImage imageNamed:@"onimage"]; 126 127 //UIControlEventValueChanged 128 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