美文网首页搬砖
ios-UI基础控件-UIControl及其子类

ios-UI基础控件-UIControl及其子类

作者: 风一样的程序员 | 来源:发表于2016-06-17 15:51 被阅读131次
    我引领着星光的降临

    UIControl简介

    • UIControl是有控制功能的视图(比如UIButton,UISlider,UISegmentControl等)的父类
    • 只要是跟控制有关的控件都是继承于该类
    • UIControl这个类通常我们并不直接使用,而是使用其子类

    UISegmentControl

    1.UISegmentedControl是iOS中的分段控件,每个segment都能够被点击,相当于继承了若干个Button。通常我们使用不同的segment切换不同的View
    2.什么是UISegmentControl?

    分段控制

    3.方法属性

    方法属性
    UISlider

    1.滑块控件通常用于控制视频播放进度,控制音量
    2.UISlider是什么?

    音量控制
    • 方法属性
    常用属性

    代码示例

    • UISegmentControl
    #pragma mark - 分段控制器
        NSMutableArray *imageArray = [NSMutableArray array];
        for (int i = 1; i < 5; i++) {
            [imageArray addObject:[NSString stringWithFormat:@"%d",i]];
        }
        UISegmentedControl *control = [[UISegmentedControl alloc] initWithItems:imageArray];
        control.selectedSegmentIndex = 1;
        
        for (int i = 0; i < 4; i++) {
            [control setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%d.png", i+1]] forSegmentAtIndex:i];
        }
        
        control.frame = CGRectMake(150, 100, 150, 100);
        NSLog(@"%f", [control widthForSegmentAtIndex:0]);
        control.tintColor = [UIColor redColor];
        [control setWidth:70.0 forSegmentAtIndex:1];//设置指定索引选项的宽度
        control.tag = 1001;
        [self.view addSubview:control];
        
        
        //添加事件
        [control addTarget:self action:@selector(changeView:) forControlEvents:UIControlEventValueChanged];
    
    • UISlider
    #pragma mark - UISlider
        UISlider *slider = [[UISlider alloc] initWithFrame:CGRectMake(200, 250, 300, 50)];
        slider.maximumValue = 0;
        slider.maximumValue = 255;
        slider.value = 0;
        slider.transform = CGAffineTransformRotate(slider.transform, M_PI_2);
        [self.view addSubview:slider];
        [slider addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged];
    
    • UISwitch
    #pragma mark - UISwitch
        UISwitch *switchBtn = [[UISwitch alloc] initWithFrame:CGRectMake(200, 200, 100, 50)];
        switchBtn.on = YES;
        switchBtn.onTintColor = [UIColor blackColor];
        switchBtn.thumbTintColor = [UIColor orangeColor];
        switchBtn.tintColor = [UIColor greenColor];
        [self.view addSubview:switchBtn];
        [switchBtn addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];
    
    • UIPageControl
    #pragma mark - pageControl
        _page = [[UIPageControl alloc] initWithFrame:CGRectMake(100, self.view.bounds.size.height-200, 200, 40)];
        //设置页面个数
        _page.numberOfPages = 4;
        //设置当前页 默认为0
        _page.currentPage = 1;
        _page.backgroundColor = [UIColor blueColor];
        _page.tag = 1001;
        //小圆点的颜色
        _page.currentPageIndicatorTintColor = [UIColor blackColor];
        _page.pageIndicatorTintColor = [UIColor purpleColor];
        //给pageControl添加事件
        [_page addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventValueChanged];
        [self.view addSubview:_page];
        //循环创建5个图片
        _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 300, 200, 200)];
        _imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"pic%ld.jpg", _page.currentPage+1]];
        [self.view addSubview:_imageView];
    //    [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(changePage) userInfo:nil repeats:YES];
        NSTimer *timer = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(changePage) userInfo:nil repeats:YES];
        NSRunLoop *runloop = [NSRunLoop currentRunLoop];
        [runloop addTimer:timer forMode:NSDefaultRunLoopMode];
    

    相关文章

      网友评论

        本文标题:ios-UI基础控件-UIControl及其子类

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