美文网首页
步进器和分栏控制器

步进器和分栏控制器

作者: yz_wang | 来源:发表于2016-11-16 10:14 被阅读0次

1. 定义步进器和分栏控制器

在ViewController.h中定义对象和声明属性:

p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #d12f1b}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; min-height: 13.0px}p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo}p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px 'Heiti SC Light'; color: #008400}p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #bb2ca2}span.s1 {font-variant-ligatures: no-common-ligatures; color: #78492a}span.s2 {font-variant-ligatures: no-common-ligatures}span.s3 {font-variant-ligatures: no-common-ligatures; color: #bb2ca2}span.s4 {font-variant-ligatures: no-common-ligatures; color: #703daa}span.s5 {font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures; color: #000000}span.s6 {font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures}span.s7 {font-variant-ligatures: no-common-ligatures; color: #000000}

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

{
    //定义步进器对象,按照一定的数值来调整某个数据
    UIStepper* _stepper;
    
    //定义分栏控制器
    UISegmentedControl* _segControl;
}

@property(retain, nonatomic) UIStepper* stepper;
@property(retain, nonatomic) UISegmentedControl* segControl;

@end

在ViewController.m中同步属性:

p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px 'Heiti SC Light'; color: #008400}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo}span.s1 {font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures}span.s2 {font-variant-ligatures: no-common-ligatures}span.s3 {font-variant-ligatures: no-common-ligatures; color: #bb2ca2}span.s4 {font-variant-ligatures: no-common-ligatures; color: #4f8187}

//同步属性
@synthesize segControl = _segControl;
@synthesize stepper = _stepper;

2. 步进器

在ViewController.m的viewDidLoad中添加:

p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px 'Heiti SC Light'; color: #008400}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo}p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; min-height: 13.0px}p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #703daa}p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #008400}p.p6 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #3d1d81}span.s1 {font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures; color: #000000}span.s2 {font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures}span.s3 {font-variant-ligatures: no-common-ligatures}span.s4 {font-variant-ligatures: no-common-ligatures; color: #4f8187}span.s5 {font-variant-ligatures: no-common-ligatures; color: #703daa}span.s6 {font-variant-ligatures: no-common-ligatures; color: #3d1d81}span.s7 {font-variant-ligatures: no-common-ligatures; color: #272ad8}span.s8 {font-variant-ligatures: no-common-ligatures; color: #008400}span.s9 {font: 11.0px 'Heiti SC Light'; font-variant-ligatures: no-common-ligatures; color: #008400}span.s10 {font-variant-ligatures: no-common-ligatures; color: #000000}span.s11 {font: 11.0px 'Heiti SC Light'; font-variant-ligatures: no-common-ligatures}span.s12 {font: 11.0px 'Apple Color Emoji'; font-variant-ligatures: no-common-ligatures}span.s13 {font-variant-ligatures: no-common-ligatures; color: #bb2ca2}

    //创建步进器对象
    _stepper=[[UIStepper alloc]init];
    _stepper.frame = CGRectMake(100, 100, 80, 40); //宽高不能变更
    
    _stepper.minimumValue=0;
    _stepper.maximumValue=100;
    _stepper.stepValue=5;
    _stepper.value=10;  //设置当前值,默认为0

    
    _stepper.tintColor=[UIColor yellowColor];
    _stepper.backgroundColor=[UIColor blackColor];
    
    //是否可以重复响应事件(按住➕能不能一直加,而且越来越快,还是需要松开再按)
    _stepper.autorepeat=YES;
    //是否将步进结果通过事件函数每一次都响应出来
    _stepper.continuous=YES;
    
    
    //添加事件函数
    [_stepper addTarget:self action:@selector(stepChange) forControlEvents:UIControlEventValueChanged];
    
    [self.view addSubview:_stepper];

在函数外实现事件函数:

p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #d12f1b}span.s1 {font-variant-ligatures: no-common-ligatures}span.s2 {font-variant-ligatures: no-common-ligatures; color: #bb2ca2}span.s3 {font-variant-ligatures: no-common-ligatures; color: #000000}span.s4 {font-variant-ligatures: no-common-ligatures; color: #3d1d81}span.s5 {font-variant-ligatures: no-common-ligatures; color: #4f8187}span.s6 {font-variant-ligatures: no-common-ligatures; color: #703daa}

-(void)stepChange
{
    NSLog(@"value=%f",_stepper.value);
}
屏幕快照 2016-11-16 上午9.49.35.png 屏幕快照 2016-11-16 上午10.11.25.png

3. 分栏控制器

在ViewController.m的viewDidLoad中添加:

p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #008400}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #703daa}p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo}p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; min-height: 13.0px}p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px 'Heiti SC Light'; color: #008400}p.p6 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #3d1d81}p.p7 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #4f8187}span.s1 {font-variant-ligatures: no-common-ligatures; color: #000000}span.s2 {font-variant-ligatures: no-common-ligatures}span.s3 {font: 11.0px 'Heiti SC Light'; font-variant-ligatures: no-common-ligatures}span.s4 {font-variant-ligatures: no-common-ligatures; color: #4f8187}span.s5 {font-variant-ligatures: no-common-ligatures; color: #3d1d81}span.s6 {font-variant-ligatures: no-common-ligatures; color: #703daa}span.s7 {font-variant-ligatures: no-common-ligatures; color: #272ad8}span.s8 {font-variant-ligatures: no-common-ligatures; color: #008400}span.s9 {font: 11.0px 'Heiti SC Light'; font-variant-ligatures: no-common-ligatures; color: #008400}span.s10 {font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures; color: #000000}span.s11 {font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures}span.s12 {font-variant-ligatures: no-common-ligatures; color: #d12f1b}span.s13 {font-variant-ligatures: no-common-ligatures; color: #bb2ca2}

    //创建分栏控件
    _segControl=[[UISegmentedControl alloc]init];
    _segControl.frame=CGRectMake(10, 200, 300, 40); //宽度可变,高度不可变
    
    //添加两个个按钮元素
    [_segControl insertSegmentWithTitle:@"0 dollar" atIndex:0 animated:NO];
    [_segControl insertSegmentWithTitle:@"5 dollar" atIndex:1 animated:NO];
    //当前默认按钮索引设置
    _segControl.selectedSegmentIndex=0;
    
    [_segControl addTarget:self action:@selector(segChange) forControlEvents:UIControlEventValueChanged];
    
    _segControl.tintColor=[UIColor orangeColor];
    _segControl.backgroundColor=[UIColor yellowColor];
    
    [self.view addSubview:_segControl];

在函数外实现事件函数:

p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #703daa}span.s1 {font-variant-ligatures: no-common-ligatures}span.s2 {font-variant-ligatures: no-common-ligatures; color: #bb2ca2}span.s3 {font-variant-ligatures: no-common-ligatures; color: #000000}span.s4 {font-variant-ligatures: no-common-ligatures; color: #3d1d81}span.s5 {font-variant-ligatures: no-common-ligatures; color: #d12f1b}span.s6 {font-variant-ligatures: no-common-ligatures; color: #4f8187}

-(void)segChange
{
    NSLog(@"%l",_segControl.selectedSegmentIndex);
}

默认样式


设置主体颜色橘色,背景颜色黄色:

屏幕快照 2016-11-16 上午9.57.30.png

返回点击的分栏索引:

屏幕快照 2016-11-16 上午10.14.11.png

相关文章

网友评论

      本文标题:步进器和分栏控制器

      本文链接:https://www.haomeiwen.com/subject/uxtrpttx.html