iOS基础-UIControl及其子类

摘要:
UISegmentedControl的使用UISegmented control是iOS中的一种分段控件。每个片段都可以单击,这相当于继承了几个按钮。通常,我们单击不同的段来连接不同的视图通用方法和属性initWithItems:一种独特的初始化方法,用于创建多个段setTitle:forSegmentAtIndex:set titleselectedSegments,用于带有下标的段

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];
}


免责声明:文章转载自《iOS基础-UIControl及其子类》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Linux netstat命令详解和使用例子(显示各种网络相关信息)数据管理(六)下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

随便看看

POI操作word和html相互转化

下面是里两个类:第一个类是html转为word,第二个是word转html(最下面附上jar包下载链接)packagecom.wz.poi.wordHtml;/***2018/4/24*@authorAdministrator**/importjava.io.BufferedReader;importjava.io.ByteArrayInputStream;...

git:将两个请求合并为一个请求

Gitrebase ihEAD~2解释:此命令可以以文本形式显示您提交的两次请求。如果数字2被4替换,则您最近四次提交的信息将显示如下:1 pick56a06efchange1:删除一个空白行2 pickedbeab5change2:addlogonMainActivity34#Rebase23198ba..Edbeab5onto23198ba5#6#命令:...

eventUtil

}elseif(element.attachEvent){element.aattchEvent('on'+类型,}else{element['on'+type]=处理程序;}else{element['on'+类型]=null;函数(事件){returnevent.type;}否则{event.returnValue=false;...

boost的下载和安装(windows版)

1 Introduction boost是一个准C++标准库,相当于STL的延续和扩展。它的设计理念类似于STL,它使用泛型来最大化重用。对于2boost的下载和安装,我们可以在官方boost网站上下载最新的boost版本。因为boost库可以像标准库一样在多个平台上运行,所以它只以源代码的形式正式提供。这是因为boost windows的安装版本不仅与处理...

css动画延迟好像有点怪

项目需要使用动画Css。自定义时,会发现设置动画延迟和动画持续时间的总时间不正确,这将导致动画丢失。例如,bounceInLeft动画从左侧出现,然后抖动。当初始动画延迟为0时,动画持续时间为1s,动画已完成,但如果设置该值,动画延迟为1s且动画持续时间是2s,则动画未完成。具体的动画是从左侧出现,然后在1s延迟后直接到达终点,但没有抖动。然后我用w3c写了...

可用的rtmp互联网地址

Rtmp:vlc使用ffmpeg获取Rtmp网络流。代码文件路径:vlc-2.2.1 modulesassesavio。hvlc-2.2.1模块。c在模块的开放回调函数OpenAvio中,使用以下代码打开rtmp网络流。avio_打开(&amp;avio_FLAG_READ);//或者这个avio_open2(&amp;sys-&gt...