Objective-C:步進UIStepper、滑動塊UISlider、開關UISwitch的基本使用

來源:互聯網
上載者:User

標籤:

步進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 *)sender3 {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 *)sender3 {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 *)sender3 {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 @end20 21 @implementation ViewController22 - (IBAction)stepperValueChange:(UIStepper *)sender23 {24     [self.slider setValue:sender.value];25     self.index = [self.stepper value];26     [self setImageIndex:self.index];27 }28 - (IBAction)sliderValueChange:(UISlider *)sender29 {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)index62 {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

 

Objective-C:步進UIStepper、滑動塊UISlider、開關UISwitch的基本使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.