UIControl及其子类

UIControl 是具有控制功能的视图(比如UIButton,UISlider,UISegmentedControl等)的父类
只要跟控制有关的控件都是继承与该类

     继承与UIControl的子类:
     UIButton *button
     UITextField *tf
     UISwith
     UISlider
     UISegmentedControl
     UIPageControl
     UIDatePicker
     UIStepper

UIButton

 UIButton *button = [[UIButton alloc] init];
  button.frame = CGRectMake(100, 100, 80, 30);
  button.backgroundColor = [UIColor lightGrayColor];
    button.tag = 1123;
    [button setTitle:@"下一页" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
//button回调方法
- (void)buttonAction:(UIButton *)button{
    SecondViewController *secondVC = [[SecondViewController alloc] init];
    [self presentViewController:secondVC animated:YES completion:NULL];
}

UISwitch*

        UISwitch *swith = [[UISwitch alloc] initWithFrame:CGRectMake(200, 100, 0, 0)];
        //在开关打开的情况下开关被渲染的颜色
        swith.onTintColor = [UIColor redColor];
        
        //边框颜色
        swith.tintColor = [UIColor yellowColor];
        //开关(圆球)颜色
        swith.thumbTintColor = [UIColor purpleColor];
//        //图片基本上不使用
//        swith.onImage = [UIImage imageNamed:@"iconfont-right(3)"];
        
        //设置APP运行时开关的初始值
        //设置开关状态
        swith.on = YES;
        NSLog(@"%d", swith.isOn);
       
        //开始时的状态
        //        [swith setOn:NO animated:YES];
        
        [swith addTarget:self action:@selector(swithAction:) forControlEvents:UIControlEventValueChanged];
  
        [self.view addSubview:swith];
//回调方法
- (void)swithAction:(UISwitch *)swith{
    if (swith.isOn) {
        NSLog(@"打开");
//        [[self.view viewWithTag:1133] setdate:YES animated:NO];
        [self.view addSubview:[self.view viewWithTag:1123]];
    }else{
        NSLog(@"关闭");
    }
}

UIDatePicker 时间选择器

        UIDatePicker *datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 200, self.view.frame.size.width, 200)];
        
        datePicker.datePickerMode = UIDatePickerModeDate;
        
        [datePicker  addTarget:self action:@selector(datePicker:) forControlEvents:UIControlEventValueChanged];
        datePicker.tag = 1133;
        [self.view addSubview:datePicker];
//回调方法
- (void)datePicker:(UIDatePicker *)datePicker{
    
    NSLog(@"%@", datePicker.date);
    //时间格式类 =>转换成字符串
   NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
    NSString *string = [dateFormatter stringFromDate:datePicker.date];
    NSLog(@"%@", string);
}

滑块控件 UISlider

//调取视频的时长,就等于滑块的长度,
//添加手势,点到哪里,对号就到了哪里

         UISlider *slider = [[UISlider alloc] initWithFrame:CGRectMake(10, 600, 355, 20)];
        
//        //滑块最小值
        slider.minimumValue = 1;
//        //滑块最大值
        slider.maximumValue = 20;
        slider.value = 0.5;
        
        //滑块划过的颜色
        slider.minimumTrackTintColor = [UIColor redColor];
        //未划过的颜色
        slider.maximumTrackTintColor = [UIColor greenColor];
        //滑块的颜色
        slider.thumbTintColor = [UIColor blueColor];
        
        //设置滑块图片
        UIImage *image = [UIImage imageNamed:@"iconfont-right_n"];
        UIImage *image2 = [UIImage imageNamed:@"iconfont-right_h"];
        
        //? 给图片加长按手势
        
        [slider setThumbImage:image2 forState:UIControlStateHighlighted];
        [slider setThumbImage:image forState:UIControlStateNormal];
        [slider addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged];
        [self.view addSubview:slider];
//回调方法
- (void)sliderAction:(UISlider *)slider{
    
    NSLog(@"%.2f", slider.value);
    
    UIImageView *imageView = [self.view viewWithTag:2233];
    imageView.animationDuration = slider.value;
    [imageView startAnimating];
    
 /*   //进度条
    UIProgressView   iOS自带视频进度条
    NSTimer   计时器
*/
}

UISegmentedControl 分段控件器,一般用来负责没有层级递进关系的视图的切换

    //数组只能存放字符串或者图片,数组中存的就是每个分段的标题
NSArray *items = @[@"全部订单", @"进行中订单", @"已完成订单"];
    UISegmentedControl *segmentControl = [[UISegmentedControl alloc] initWithItems:items];
    segmentControl.frame = CGRectMake(50, 50, 275, 30);
//    segmentControl.segmentedControlStyle = UISegmentedControlStyleBordered;
    
    //渲染颜色
    segmentControl.tintColor = [UIColor blackColor];
    
    UIImage *image = [[UIImage imageNamed:@"Img312550625.jpg"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    [segmentControl insertSegmentWithImage:image atIndex:1 animated:YES];
    
    //设置选中的分段  的索引
    segmentControl.selectedSegmentIndex = 0;
    
    [segmentControl addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
    
//    UIControl
    
    [self.view addSubview:segmentControl];
//回调方法
- (void)segmentAction:(UISegmentedControl *)segmentControl{
    
    NSLog(@"当前点击的是第%ld个分段", (long)segmentControl.selectedSegmentIndex + 1);
    
    if (segmentControl.selectedSegmentIndex == 1) {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 50, 30) ];
        label.tag = 2233;
        label.backgroundColor = [UIColor greenColor];
        [self.view addSubview:label];
    }
    if (segmentControl.selectedSegmentIndex == 2) {
        UILabel *tempLabel = [self.view viewWithTag:2233];
//        tempLabel.isEnabled = YES;
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(200, 200, 50, 30) ];
        label.backgroundColor = [UIColor greenColor];
        [self.view addSubview:label];
    }
}

你可能感兴趣的:(UIControl及其子类)