iOS學習-其他常用控制項

來源:互聯網
上載者:User

標籤:

  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學習-其他常用控制項

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.