步进UIStepper、滑动块UISlider:当它们作为事件,被触发时,它们的值会发生改变。正因为如此,触发该事件时,可以一张一张翻阅浏览图片,,,,
步进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,每次步进的距离
1 //滑动块触发事件(值value发生改变,范围mininum~maxnum) 2 - (IBAction)sliderValueChange:(UISlider *)sender 3 { 4 NSLog(@"%lf",sender.value); 5 //将滑动块的值取出并赋给步进,使得步进和滑动块事件状态相同 6 [self.stepper setValue:sender.value]; 7 }
滑动块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 //步进触发事件(值value发生改变,范围mininum~maxnum,可以设置步进大小step值) 2 - (IBAction)stepperValueChange:(UIStepper *)sender 3 { 4 NSLog(@"%lf",sender.value); 5 //将步进的值取出并赋给滑动块,使得步进和滑动块事件状态相同 6 [self.slider setValue:sender.value]; 7 }
开关UISwitch:
@property(nonatomic,getter=isOn) BOOL on; //有两种状态,开on或关off
1 //开关触发事件(状态state只有两种形式:on、off) 2 - (IBAction)switchValueChange:(UISwitch *)sender 3 { 4 NSLog(@"%@",sender.isOn?@"开":@"关"); 5 }
以下用步进和滑动块、开关做的一个图片浏览器
源码如下:
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;//显示当前图片的索引 18 @property (assign,nonatomic) NSInteger total;//图片的个数 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 //取出数组中的字典 65 NSDictionary *dicImage = self.imageInfos[index]; 66 NSString *imageName = [dicImage objectForKey:@"icon"]; 67 NSString *imageTitle = [dicImage objectForKey:@"title"]; 68 69 //设置显示的图片 70 self.imageView.image = [UIImage imageNamed:imageName]; 71 72 //设置显示的图片的标题 73 self.labelTitle.text = imageTitle; 74 75 //设置当前信息:索引/总数 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