UIControl及其子类

一.UIControl

UIControl是UIView的子类,当然也是UIResponder的子类。UIControl对象采用了一种新的事件处理机制,将前一节直接处理的触摸事件转换成简单操作,这样可以无需关心用户访问控件的具体方式。触摸事件到达UIControl对象(由响应者链派遣)后,在UIResponder的方法中(如touchBegan:withEvent)中,UIControl将标准的触摸事件转换为特殊的控件事件,简单的理解就是,UIControl把复杂的触摸事件封装成了简单的易于使用的控件事件。UIControl是诸如UISlider、UISwitch、UIPageControl等控件的父类,它本身也包含了一些属性和方法,一般不直接使用UIControl类,而是使用它的子类。

- (void)UIControlExplain
{
    // UIControlEvents 控件响应事件激活条件枚举组
    // UIControlState  控件存在形式枚举组
    
    // UIControlContentHorizontalAlignment 控件内容垂直位置信息枚举组
    // UIControlContentVerticalAlignment 控件内容水平位置信息枚举组
}

二.UISwitch

- (void)UISwitchExplain
{
    UISwitch *firstSwitch = [[UISwitch alloc]initWithFrame:CGRectMake(100, 100, 0, 0)];
//    firstSwitch.backgroundColor = [UIColor cyanColor];
    // 开关打开时,背景框的填充色
    firstSwitch.onTintColor = [UIColor cyanColor];
    // 开关关闭时,背景框的填充色
    firstSwitch.tintColor = [UIColor redColor];
    // 开关小圆点的填充色
    firstSwitch.thumbTintColor = [UIColor blueColor];
    firstSwitch.on = YES;
    //设置开关初始 开、关状态(有无动画)
    [firstSwitch setOn:YES animated:YES];
    // 加载开关
    [self.view addSubview:firstSwitch];
    [firstSwitch release];
    [firstSwitch addTarget:self action:@selector(switchAction:) forControlEvents:(UIControlEventTouchUpInside)];

    UIImageView *imgV = [[UIImageView alloc]initWithFrame:CGRectMake(50,200 , 300, 300)];
    imgV.tag = 100;
    imgV.image = [UIImage imageNamed:@"W020100317577179692793.jpg"];
    [self.view addSubview:imgV];
    [imgV release];
}

- (void)switchAction:(UISwitch *)myswitch
{
    NSLog(@"%d",myswitch.on);
    UIImageView *imgV = [self.view viewWithTag:100];
    if (myswitch.on ==1) {
        imgV.image = [UIImage imageNamed:@"201TUU7-3.jpg"];
    }else
    {
      imgV.image = [UIImage imageNamed:@"W020100317577179692793.jpg"];
    }
}

三.UISlider

- (void)UISliderExplain
{
    UISlider *slider = [[UISlider alloc]initWithFrame:CGRectMake(50, self.view.frame.size.width -50 ,300, 100)];
    //设置最大值
    slider.maximumValue = 8.f;
    // 按钮填充色
    slider.thumbTintColor = [UIColor orangeColor];
    // 划过后的进度条填充色
    slider.minimumTrackTintColor = [UIColor redColor];
    // 未划过后的进度条填充色
    slider.maximumTrackTintColor = [UIColor blueColor];
     slider.backgroundColor = [UIColor cyanColor];
    // 添加响应事件
    [slider addTarget:self action:@selector(sliderAction:) forControlEvents:(UIControlEventValueChanged)];
    [self.view addSubview:slider];
    [slider release];
    UIImageView *imgV = [[UIImageView alloc]initWithFrame:CGRectMake(80, 80, 150, 150)];
    imgV.tag = 1000;
    NSMutableArray *nArry = [NSMutableArray array];
    for (int i = 1; i<8; i++) {
         NSString *str= [NSString  stringWithFormat:@"huoju_%d.tiff",i];
        UIImage *image = [UIImage imageNamed:str];
        [nArry addObject:image];
    }
    imgV.animationImages = nArry;
    imgV.animationDuration = 2.f;
    imgV.animationRepeatCount = -1;
    [imgV startAnimating];
    [self.view addSubview:imgV];
    [imgV release];  
}

- (void)sliderAction:(UISlider *)slider
{
    NSLog(@"%f",slider.value);
    UIImageView *imgV = [self.view viewWithTag:1000];
    if (slider.value!=0) {
        imgV.animationDuration = 2/slider.value;
    }
    [imgV startAnimating];
}

四.UISegmentedControl

- (void)segmentedControlExplain
{
    UISegmentedControl *segmentCtrl = [[UISegmentedControl alloc]initWithItems:@[@"1",@"2",@"3"]];
    segmentCtrl.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 60);
    segmentCtrl.backgroundColor = [UIColor cyanColor];
    // 初始选中框的index
    segmentCtrl.selectedSegmentIndex = 1;
    // 填充色
    segmentCtrl.tintColor = [UIColor redColor];
    // (重写)设置对应index下的标题
    [segmentCtrl setTitle:@"首页" forSegmentAtIndex:1];
    segmentCtrl.tag= 30; 
//    [segmentCtrl setImage:[UIImage imageNamed:@"huoju_1.tiff"]forSegmentAtIndex:0];
    
     // 添加点击事件
    [segmentCtrl addTarget:self action:@selector(segmentAction:) forControlEvents:(UIControlEventValueChanged)];
    [self.view addSubview:segmentCtrl];
    [segmentCtrl release];

    for (int i = 0; i<2; i++) {
        NSArray *arry = @[@"添加",@"删除"];
        UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];
        button.frame = CGRectMake(50+150*i, 100, 100, 100);
        [button setTitle:arry[i] forState:(UIControlStateNormal)];
        [button setTitleColor:[UIColor redColor] forState:(UIControlStateNormal)];
        [button addTarget:self action:@selector(buttonAction:) forControlEvents:(UIControlEventTouchUpInside)];
        button.tag = i;
        [self.view addSubview:button];
    }
}
- (void)segmentAction:(UISegmentedControl *)segment
{
    NSInteger index = segment.selectedSegmentIndex;
    NSLog(@"%ld",index);
    if (index==0) {
        self.view.backgroundColor= [UIColor blueColor];
    }else if (index==1) {
        self.view.backgroundColor = [UIColor greenColor];
    }else{
        self.view.backgroundColor = [UIColor blackColor];
    }
    NSLog(@"%ld",segment.selectedSegmentIndex);
}
- (void)buttonAction:(UIButton *)button
{
    NSInteger tag = button.tag;
     UISegmentedControl *segment = [self.view viewWithTag:30];
    if (tag==0) {
        [segment insertSegmentWithTitle:[NSString stringWithFormat:@"%lu",segment.numberOfSegments+1]atIndex:segment.numberOfSegments animated:YES];
    } else
    {
        [segment removeSegmentAtIndex:segment.numberOfSegments-1 animated:YES];
    }
}

五.UIPageControl

- (void)UIPageControlExplain
{
    UIImageView *imgV = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    imgV.backgroundColor = [UIColor cyanColor];
    imgV.image = [UIImage imageNamed:@"W020100317577179692793.jpg"];
    imgV.tag = 88;
    [self.view addSubview:imgV];
    [imgV release];
    
    UIPageControl *pageCtrl = [[UIPageControl alloc]initWithFrame:CGRectMake(50, 500, self.view.frame.size.width-100, 40)];
    
    // 小圆点之间的距离,大小事固定的
    // 小圆点可以超出自身frame显示,但超出frame区域的不能响应点击事件
    
    // 小圆点个数
    pageCtrl.numberOfPages = 5;
    pageCtrl.backgroundColor = [UIColor blackColor];
    //当前圆点个数
    pageCtrl.currentPage = 1;
    //未选择小圆点填充色
    pageCtrl.pageIndicatorTintColor = [UIColor blueColor];
    // 选中的小圆点填充色
    pageCtrl.currentPageIndicatorTintColor = [UIColor redColor];
    
    //添加点击事件
    [pageCtrl addTarget:self action:@selector(pageAction:) forControlEvents:(UIControlEventValueChanged)];

    [self.view addSubview:pageCtrl];
    [pageCtrl release];
}

- (void)pageAction:(UIPageControl *)page
{
    NSInteger index = page.currentPage ;
    UIImageView *imgV = [self.view viewWithTag:88];
    switch (index) {
        case 0:
            imgV.image = [UIImage imageNamed:@"images (1).jpeg"];
            break;
        case 1:
            imgV.image = [UIImage imageNamed:@"W020100317577179692793.jpg"];
            break;
        case 2:
            imgV.image = [UIImage imageNamed:@"201TUU7-3.jpg"];
            break;
        default:
            break;
    }
}

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