ios 初体验< UISegmentedControl 分段控件>

 小知识:  数组快速创建 @[@"",@"",@"",@"".......],字典快速创建方法:@{@"",@"",@"",@""......} ,可变,和不可变,

快速实例化一个 UISegmentedControl *segmentedContrl = [[UISegmentedControl alloc]initWithItems:@[@"红色",@"黄色",@"灰色"]];  //里面装的是个数组

 UISegmentedControl 继承自UIControl  添加点击事件

  设置其位置 通过获取 屏幕居中点 

 segmentedContrl.bounds = CGRectMake(0, 0, 250, 30);

    segmentedContrl.center = CGPointMake(self.view.center.x, self.view.center.y + 15);

    默认选中由于里面是个数组

    segmentedContrl.selectedSegmentIndex = 0 ;

    默认字体 从字典里面去取

    [segmentedContrl setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor orangeColor]} forState:UIControlStateNormal];

    添加事件

    [segmentedContrl addTarget:self action:@selector(controlProgress:) forControlEvents:UIControlEventValueChanged];

    [self.view addSubview:segmentedContrl];

 点击事件

- (void)controlProgress:(UIView *)sender{

    //判断该对象是否是 switch

    if ([sender isKindOfClass:[UISwitch class]]) {

        强转 判断传进来是

        UISwitch *switchContol = (UISwitch *)sender;

        条件表达式 判断开关动画的显示

        switchContol.on ? [_indicatorView startAnimating]:[_indicatorView stopAnimating];

    }

    if ([sender isKindOfClass:[UISlider class]]) {

        强转

        UISlider *slider = (UISlider *)sender;

        self.view.alpha = slider.value;

    }

    if ([sender isKindOfClass:[UISegmentedControl class]]) {

        强转

        UISegmentedControl *segmentedContrl = (UISegmentedControl *)sender;

         把颜色存入一个数组

        NSArray *colorArry = @[[UIColor redColor],[UIColor yellowColor],[UIColor grayColor]];

        改变颜色

        self.view.backgroundColor = colorArry[segmentedContrl.selectedSegmentIndex];

    }

}

原文地址:https://www.cnblogs.com/tanglie/p/6492468.html