IOS开发之XCode学习013:步进器和分栏控件

作者: iOS 发布时间: 2021-01-27 浏览: 1245 次 编辑

此工程文件实现功能:

1、定义UIStepperUISegmentedControl对象和属性

2、设置UIStepper和UISegmentedControl的基本属性,如最小值

3、添加事件函数

===========================ViewController.h脚本==============================

#import 

 @interface ViewController : UIViewController

{

    //定义步进器对象

    //按照一定的数值来调整某个数据

    UIStepper* _stepper;

    

    //定义分栏控件对象

    UISegmentedControl* _segControl;

}

//属性的定义

@property (retain,nonatomic) UIStepper* stepper;

@property (retain,nonatomic) UISegmentedControl* segControl;

@end

===========================ViewController.m脚本==============================

@synthesize stepper = _stepper;

@synthesize segControl = _segControl;

 

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    

    //创建步进器对象

    _stepper = [[UIStepper alloc] init];

    

    //设置位置,宽高不能变更

    _stepper.frame = CGRectMake(100, 100, 80, 40);

    

    //设置步进器的最小值

    _stepper.minimumValue = 0;

    

    //设置步进器的最大值

    _stepper.maximumValue = 100;

    

    //设置步进器的当前值,默认值为0

    _stepper.value = 10;

    

    //设置步进值,每次向前或向后步进的步伐值

    _stepper.stepValue = 10;

    

    //是否可以重复响应事件操作,YES:按住“+”或“-”号可以重复执行 NO:按住一次松开,才执行,按住不重复操作

    _stepper.autorepeat = YES;

    

    //是否将步进结果通过事件函数响应出来

    //YES:会把数据的变化过程显示出来,如从100到50,会依次显示100、90、80、70、60、50

    //NO:只显示变化的初始值和默认值,如从100到50,只显示100、50

    _stepper.continuous = YES;

    

    //添加事件函数

    //1:函数实现体

    //2:函数体

    //3:事件值改变的状态

    [_stepper addTarget:self action:@selector(stepChange) forControlEvents:UIControlEventValueChanged];

    

    [self.view addSubview:_stepper];

    

    //创建分栏控件

    _segControl = [[UISegmentedControl alloc] init];

    

    //设置控件位置,宽度可变,高度不可变

    _segControl.frame = CGRectMake(10, 200, 300, 40);

    

    //添加一个按钮元素

    //P1:按钮选项文字

    //P2:按钮的索引位置

    //P3:是否有插入的动画效果

    [_segControl insertSegmentWithTitle:@"0元" atIndex:0 animated:NO];

    [_segControl insertSegmentWithTitle:@"5元" atIndex:1 animated:NO];

    [_segControl insertSegmentWithTitle:@"10元" atIndex:2 animated:NO];

    

    //当前默认按钮索引设置

    _segControl.selectedSegmentIndex = 0;

    

    [_segControl addTarget:self action:@selector(segChange) forControlEvents:UIControlEventValueChanged];

    

    [self.view addSubview:_segControl];

}

 

-(void)segChange

{

    NSLog(@"%d",_segControl.selectedSegmentIndex);

}

 

-(void)stepChange

{

    NSLog(@"step progress,当前值为:%f",_stepper.value);

}

运行结果:


学习总结:

  • 重点:步进器和分栏控件的属性
  • 难点:步进器和分栏控件的使用

源码链接地址:https://pan.baidu.com/s/1yrOLXZZeu9MiOWtMq5-EGA 密码:7t1l