iOS:步进UIStepper、滑动块UISlider、开关UISwitch的基本使用
步進(jìn)UIStepper、滑動(dòng)塊UISlider:當(dāng)它們作為事件,被觸發(fā)時(shí),它們的值會(huì)發(fā)生改變。正因?yàn)槿绱?觸發(fā)該事件時(shí),可以一張一張翻閱瀏覽圖片,,,,
步進(jìn)UIStepper:
@property(nonatomic) double minimumValue; ? ? ? ? ? ? ? ? // default 0. must be less than maximumValue
@property(nonatomic) double maximumValue; ? ? ? ? ? ? ? ?// default 100. must be greater than minimumValue
@property(nonatomic) double stepValue; ? ? ? ? ? ? ? ? ? ? ? ? // default 1. must be greater than 0,每次步進(jìn)的距離
1 //滑動(dòng)塊觸發(fā)事件(值value發(fā)生改變,范圍mininum~maxnum) 2 - (IBAction)sliderValueChange:(UISlider *)sender 3 { 4 NSLog(@"%lf",sender.value); 5 //將滑動(dòng)塊的值取出并賦給步進(jìn),使得步進(jìn)和滑動(dòng)塊事件狀態(tài)相同 6 [self.stepper setValue:sender.value]; 7 }?
滑動(dòng)塊UIStepper:
@property(nonatomic) float value; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? // default 0.0. this value will be pinned to min/max
@property(nonatomic) float minimumValue; ? ? ? ? ? ? ? ? ? // default 0.0. the current value may change if outside new min value
@property(nonatomic) float maximumValue;? // default 1.0. the current value may change if outside new max value
1 //步進(jìn)觸發(fā)事件(值value發(fā)生改變,范圍mininum~maxnum,可以設(shè)置步進(jìn)大小step值) 2 - (IBAction)stepperValueChange:(UIStepper *)sender 3 { 4 NSLog(@"%lf",sender.value); 5 //將步進(jìn)的值取出并賦給滑動(dòng)塊,使得步進(jìn)和滑動(dòng)塊事件狀態(tài)相同 6 [self.slider setValue:sender.value]; 7 }?
開關(guān)UISwitch:
@property(nonatomic,getter=isOn) BOOL on; ? ? ? ? ? ? ? ?//有兩種狀態(tài),開on或關(guān)off
1 //開關(guān)觸發(fā)事件(狀態(tài)state只有兩種形式:on、off) 2 - (IBAction)switchValueChange:(UISwitch *)sender 3 { 4 NSLog(@"%@",sender.isOn?@"開":@"關(guān)"); 5 }?
?
? ?以下用步進(jìn)和滑動(dòng)塊、開關(guān)做的一個(gè)圖片瀏覽器
? ?源碼如下:
1 // ViewController.m 2 // 02-圖片瀏覽器 3 // 4 // Created by ma c on 15/8/27. 5 // Copyright (c) 2015年 bjsxt. All rights reserved. 6 // 7 8 #import "ViewController.h" 9 10 @interface ViewController () 11 @property (weak, nonatomic) IBOutlet UILabel *labelTitle; 12 @property (weak, nonatomic) IBOutlet UILabel *labelInfo; 13 @property (weak, nonatomic) IBOutlet UIImageView *imageView; 14 @property (weak, nonatomic) IBOutlet UIStepper *stepper; 15 @property (weak, nonatomic) IBOutlet UISlider *slider; 16 @property (strong,nonatomic) NSArray *imageInfos; 17 @property (assign,nonatomic) NSInteger index;//顯示當(dāng)前圖片的索引 18 @property (assign,nonatomic) NSInteger total;//圖片的個(gè)數(shù) 19 @end 20 21 @implementation ViewController 22 - (IBAction)stepperValueChange:(UIStepper *)sender 23 { 24 [self.slider setValue:sender.value]; 25 self.index = [self.stepper value]; 26 [self setImageIndex:self.index]; 27 } 28 - (IBAction)sliderValueChange:(UISlider *)sender 29 { 30 [self.stepper setValue:sender.value]; 31 self.index = [self.slider value]; 32 [self setImageIndex:self.index]; 33 } 34 35 - (void)viewDidLoad { 36 [super viewDidLoad]; 37 //加載圖片信息 38 NSString *path = [[NSBundle mainBundle] pathForResource:@"images" ofType:@"plist"]; 39 self.imageInfos = [NSArray arrayWithContentsOfFile:path]; 40 41 42 //初始化 43 self.total = [self.imageInfos count]; 44 self.index = 0; 45 46 self.stepper.minimumValue = 0; 47 self.stepper.maximumValue = self.total - 1; 48 self.stepper.value = 0; 49 self.stepper.stepValue = 1; 50 51 self.slider.minimumValue = 0; 52 self.slider.maximumValue = self.total - 1; 53 self.slider.value = 0; 54 55 56 self.imageView.contentMode = UIViewContentModeScaleAspectFit; 57 [self setImageIndex:0]; 58 59 } 60 61 -(void)setImageIndex:(NSUInteger)index 62 { 63 64 //取出數(shù)組中的字典 65 NSDictionary *dicImage = self.imageInfos[index]; 66 NSString *imageName = [dicImage objectForKey:@"icon"]; 67 NSString *imageTitle = [dicImage objectForKey:@"title"]; 68 69 //設(shè)置顯示的圖片 70 self.imageView.image = [UIImage imageNamed:imageName]; 71 72 //設(shè)置顯示的圖片的標(biāo)題 73 self.labelTitle.text = imageTitle; 74 75 //設(shè)置當(dāng)前信息:索引/總數(shù) 76 self.labelInfo.text = [NSString stringWithFormat:@"%ld/%ld",self.index+1,self.total]; 77 } 78 79 - (void)didReceiveMemoryWarning { 80 [super didReceiveMemoryWarning]; 81 // Dispose of any resources that can be recreated. 82 } 83 84 @end?
轉(zhuǎn)載于:https://www.cnblogs.com/XYQ-208910/p/4764382.html
總結(jié)
以上是生活随笔為你收集整理的iOS:步进UIStepper、滑动块UISlider、开关UISwitch的基本使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: YII 增加登陆认证
- 下一篇: 利用Quartz2D-contex绘制三