iOS基础-UIControl及其子类

UISegmentedControl的使用

UISegmentedControl是iOS中的分段控件,每个segment都能被点击,相当于继承了若干个button,通常我们会点击不同的segment来接环不同的view

常用方法和属性

initWithItems:独有的初始化方法,用来创建多个分段

setTitle:forSegmentAtIndex:为制定下标的分段设置title

selectedSegmentAtIndex:(property)被选中的segment

tintColor:(property)segmentedControl条的颜色

addTarget:action:forControlEvents:给UISegmentedControl添加事件。其中controlEvent为UIControlEventValueChanged

- (void)viewDidLoad {
    [super viewDidLoad];
#pragma mark  布局
    /*1.初始化分段控件*/
    self.view.backgroundColor = [UIColor colorWithRed:0.94 green:0.50 blue:0.63 alpha:1];
    NSArray *segmentedArray = @[@"1",@"2",@"3",@"4"];
    UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:segmentedArray];
    
    //设置frame
    segmentedControl.frame = CGRectMake(20, 20, 335, 50);
    
    //四种基本样式(已经被淘汰了)
    
    
    //指定下标插入标题
    [segmentedControl insertSegmentWithTitle:@"学习" atIndex:2 animated:NO];
    
    //替换题目
    [segmentedControl setTitle:@"生活" forSegmentAtIndex:1];
    [segmentedControl setTitle:@"红色" forSegmentAtIndex:0];
    [segmentedControl setTitle:@"蓝色" forSegmentAtIndex:1];
    [segmentedControl setTitle:@"灰色" forSegmentAtIndex:2];
    [segmentedControl setTitle:@"绿色" forSegmentAtIndex:3];
    [segmentedControl setTitle:@"下一页" forSegmentAtIndex:4];
    //图片 (会被渲染替换蓝色)
//    [segmentedControl insertSegmentWithImage:[UIImage imageNamed:@"6.png"] atIndex:2 animated:YES];

    //四种基本样式(已经被淘汰了)
    
    //背景颜色
    segmentedControl.tintColor = [UIColor redColor];
    
    [segmentedControl addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
    
    
    
    [self.view addSubview:segmentedControl];
}

#pragma mark  具体方法委托
- (void)segmentAction:(UISegmentedControl *)Seg{
    NSInteger Index = Seg.selectedSegmentIndex;
    NSLog(@"Index %lu",Index);
    switch (Index) {
        case 0:
            [self selectMyView1];
            break;
         case 1:
            [self selectMyView2];
            break;
         case 2:
            [self selectMyView3];
            break;
        case 3:
            [self selectMyView4];
            break;
        case 4:
            [self selectMyView5];
        default:
            break;
    }
    
    
}



#pragma mark 每个按钮的具体方法实现
//1
- (void)selectMyView1{
    self.view.backgroundColor = [UIColor colorWithRed:0.94 green:0.50 blue:0.63 alpha:1];
}
//2
- (void)selectMyView2{
    self.view.backgroundColor = [UIColor colorWithRed:0.30 green:0.52 blue:0.70 alpha:1];
}
//3
- (void)selectMyView3{
    self.view.backgroundColor = [UIColor grayColor];
}
//4
- (void)selectMyView4{
    self.view.backgroundColor = [UIColor colorWithRed:0.20 green:0.80 blue:0.49 alpha:1];
}
//5
- (void)selectMyView5{
//    self.view.backgroundColor = [UIColor colorWithRed:(arc4random() % 200)/100.0 green:(arc4random() % 200)/100.0 blue:(arc4random() % 200)/100.0 alpha:1];
    SecondViewController *svc = [[SecondViewController alloc]init];
    
    [self presentViewController:svc animated:YES completion:^{
        [svc.imageView startAnimating];//进入第二个界面,时的状态
    }];
    
    [svc release];
}

UISlider概述

UISlider是iOS中的滑块控件

通常用于控制视频播放进度,控制音量等

它也是继承与UIControl,滑块提供了一系列连续的值,滑块停在不同位置,获取到滑块上的值也不同

UISlider常用属性

minimumValue:设置滑块的最小值

maximumValue:设置滑块的最大值

value:设置滑块的当前值

minimumTrackTinkColor:定义划过区域的颜色

addTarget:action:forControlEvents:给UISlider添加事件,controlEvent为UIControlEventValueChanged

UIImageView常用属性

image:设置图片

animationImages:设置一组动态图片

animationDuration:设置播放一组动态图片的时间

animationRepeatCount:设置重复次数

startAnimating:开始动画

stopAnimating:结束动画

- (void)viewDidLoad {
#pragma  mark  第二个页面
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor orangeColor];
    NSArray *segmentedArray = @[@"上一页",@"2",@"3",@"4"];
    UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:segmentedArray];
    segmentedControl.frame = CGRectMake(20, 20, 335, 50);
    [segmentedControl addTarget:self action:@selector(click:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:segmentedControl];
    
    
    
    //动态图片添加方法
    UIImageView *back = [[UIImageView alloc]initWithFrame:CGRectMake(0, 150, 375, 300)];
    back.image = [UIImage imageNamed:@"2.tiff"];
    [self.view addSubview:back];
    
    
    _imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 150, 375, 300)];
    
    NSMutableArray *arr = [[NSMutableArray alloc]init];
    for (int i = 1; i < 15 ; i++) {
        NSString *str = [NSString stringWithFormat:@"%d.tiff",i];
        
        UIImage *image = [UIImage imageNamed:str];
        [arr addObject:image];
    }
    
    _imageView.animationImages = arr;
    

    
    
    
//    _imageView.animationRepeatCount = 0;
//    _imageView.backgroundColor = [UIColor grayColor];
    //动画开始
    
    

    
    [self.view addSubview:_imageView];
    
    
    /*滑块控件*/
     UISlider *slider = [[UISlider alloc]initWithFrame:CGRectMake(30, 500, 315, 30)];
//    slider.backgroundColor = [UIColor grayColor];
    
    slider.minimumValue = 1;
    slider.maximumValue = 5;
    //滑动线条颜色
    slider.minimumTrackTintColor = [UIColor redColor];
    slider.maximumTrackTintColor = [UIColor blackColor];
    slider.thumbTintColor = [UIColor blueColor];
    //滑块设置
    [slider addTarget:self action:@selector(slider1:) forControlEvents:UIControlEventValueChanged];
    //垂直的滑块
//    slider.transform = CGAffineTransformMakeRotation( M_PI * 0.5 );
    //左侧图片
//    slider.minimumValueImage = [UIImage imageNamed:@"61"];
    
    
    
    
    
    [self.view addSubview:slider];
    
    //UISWith
    UISwitch *swt = [[UISwitch alloc]initWithFrame:CGRectMake(170, 75, 30, 20)];
    
    [swt addTarget:self action:@selector(swt:) forControlEvents:UIControlEventValueChanged];
    swt.on = YES;
    [self.view addSubview:swt];
    
    //UIStepper
    UIStepper *step = [[UIStepper alloc]init];
    step.frame = CGRectMake(170, 110, 30, 20);
    [step addTarget:self action:@selector(stepClick:) forControlEvents:UIControlEventValueChanged];
    
    [self.view addSubview:step];


    UILabel *label = [[UILabel alloc] init];
    label.frame = CGRectMake(110, 110, 60, 27);
    label.backgroundColor = [UIColor whiteColor];
    label.tag = 1;
    
    [self.view addSubview:label];
    
    
    [step release];
    [label release];
    [swt release];
    [back release];
    [slider release];
    
    
    
}

#pragma mark  Slider方法实现
//1.滑动改变图片播放速度
- (void)slider1:(UISlider *)slider{
    _imageView.animationDuration = 5-slider.value;
    [_imageView startAnimating];
    NSLog(@"%.2f",slider.value);
}


//开关
- (void)swt:(UISwitch *)swt{
    if ((swt.on == YES)) {
        [_imageView startAnimating];
    }else
        [_imageView stopAnimating];
    
}

//计数器
- (void)stepClick:(UIStepper *)step{
    UILabel *v = (UILabel *)[self.view viewWithTag:1];
    
    v.text = [NSString stringWithFormat:@"%.2f",step.value];
}


原文地址:https://www.cnblogs.com/dingjianjaja/p/4840848.html