UI day 6 UIImageView UIImage图片类 UISitch开关控件 UIStepper加减控件 U UISegmentedControl 分段控制器

本文详细介绍了iOS中的一些常见UI控件,包括UIImageView和UIImage的使用,如加载图片、等比缩放;UISwitch开关控件的配置和事件响应;UIStepper加减控件的设置;以及UISegmentedControl分段控制器的用法。还涉及到动态图片的播放和自定义样式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


                          UIImageView和UIImage

1.UIImageView是用来显示图片的控件,相当于相框,用来显示UIImage对象

2.初始化UIImage对象,及为其加载图片
有两种方式:
方式一
UIImage *image = [UIImage imageNamed:@"1.JPG"];

方式二:
通过图片的路径加载图片,通过应用程序包找出图片 NSBundle(应用程序包类);
[NSBundle mainBundle]获取当前应用程序包对象
 pathForResource:资源名称  ofType:资源类型

NSString  *filePath = [[NSBundle mainBundle]pathForResource:@"2" ofType:@"JPG"];
UIImage *image2 = [UIImage imageWithContentsOfFile:filePath];

3.总结: 第一种方式:如果这个图片资源被多次使用,使用第一种方式,此种方式会把图片对象添加到应用程序的缓存中,多次使用时比较方便,缺点:占用内存
    
     第二种方式:如果这个图片资源只被使用一次,使用第二种方式,此种方式不会吧图片对象添加到应用程序缓存中,缺点:比较耗时

4. 创建UIImage对象
   
    UIImageView  *imageView = [[UIImageView alloc]initWithImage:image];
    配置属性
    设置frame
    imageView.frame = [UIScreen  mainScreen].bounds;
    重新设置图片
    imageView.image = image2;
    添加到父视图
    [self.view addSubview:imageView];
    [imageView release];

5.  //UIImageview加载动态图片
    //1.准备一组图片,使用数组存放
    NSMutableArray *imageArray = [NSMutableArray arrayWithCapacity:7];
   
   
    //2.使用for循环添加图片
    for (int i = 1 ; i < 8; i++) {
        //确定图片的名字
        NSString *name =[NSString stringWithFormat:@"huoju_%d.tiff",i];
        //初始化image对象
        UIImage *image = [UIImage imageNamed:name];
        //将图片添加到数组中
        [imageArray addObject:image];
    }
   
    NSLog(@"%@",imageArray);
   
    UIImageView *fireImageView =[[UIImageView alloc]initWithFrame:CGRectMake(120, 100, 79, 106)];
    fireImageView.backgroundColor = [UIColor  redColor];
    fireImageView.layer.cornerRadius = 30;
    //设置UIimageview播放动态图片需要的数组
    fireImageView.animationImages = imageArray;
    //设置播放动态图片的时间间隔
    fireImageView.animationDuration = 0.1;
    //设置重复次数
    fireImageView.animationRepeatCount = 100;
   
    //启动动态图片,对动画的配置写在动画开始之前VIP
    [fireImageView startAnimating];
   
    //添加到父视图
    [self.view  addSubview:fireImageView];
    [fireImageView release];

6. //等比缩放图片
    //取出8.JPG
    NSString  *filePath2 = [[NSBundle mainBundle]pathForResource:@"8" ofType:@"JPG"];
    UIImage *image3 = [UIImage imageWithContentsOfFile:filePath2];
    //image3.size中存放的是图片的宽和高
    NSLog(@"%@",NSStringFromCGSize(image3.size));
   
    //UIimageview设置为宽:200,高:未知,用来显示不失真image3
    UIImageView *CFimageView = [[UIImageView alloc]initWithImage:image3];
    CGFloat h = image3.size.height * 200/image3.size.width;
    CFimageView.layer.cornerRadius = 30;
   
    CFimageView.frame = CGRectMake(60, 200, 200, h);
    [self.view addSubview:CFimageView];
   
     [CFimageView  release];

7.练习题制作一个僵尸的动态图片
 //制作僵尸的动态图片
    //准备数组存放图片
    NSMutableArray *zombieArray = [NSMutableArray arrayWithCapacity:22];
    //定义Size变量存储图片大小
    CGSize zSize = CGSizeZero;
    for (int i = 1; i < 23; i++) {
        //图片名
        NSString *name = [NSString stringWithFormat:@"Zombie%d.tiff",i];
        //图片对象
        UIImage *image = [UIImage imageNamed:name];
        //存储图片大小
        zSize = image.size;
        //添加到数组
        [zombieArray addObject:image];
       
    }
    //验证
    //    NSLog(@"%@",zombieArray);
   
    UIImageView *zombieImageView = [[UIImageView alloc]initWithFrame:(CGRectMake(50, 100, 220, zSize.height*220/zSize.width))];
    //给动画数组赋值
    zombieImageView.animationImages = zombieArray;
    //设置每一组动画持续时间
    zombieImageView.animationDuration = 3;
    //0无限重复
    zombieImageView.animationRepeatCount = 0;
    //开始动画
    [zombieImageView startAnimating];
    //添加到父视图
    [self.view addSubview:zombieImageView];
    [zombieImageView release];

                              UISwitch 开关控件

1. UISwitch开关控件 继承UIControl
    //创建UIswitch对象
    UISwitch *aSwitch = [[UISwitch alloc]initWithFrame:CGRectMake(100, 50, 0, 0)];
    //配置switch边框渲染颜色
    aSwitch.tintColor = [UIColor yellowColor];
    //配置控件内部颜色
    aSwitch.onTintColor = [UIColor blueColor];
    //设置按钮的颜色
    aSwitch.thumbTintColor = [UIColor redColor];
    //switch关联事件
    //self是指的视图控制器对象
    [aSwitch addTarget:self action:@selector(handleSwitch:) forControlEvents:(UIControlEventValueChanged)];//当状态代表的数值改变的时候事件触发
   
    [self.view addSubview:aSwitch];
    [aSwitch release];


#pragma mark  switch的关联事件
- (void)handleSwitch:(UISwitch *)aSwitch
{
    //首先应该获取开关控件当前的状态
    switch ((int)aSwitch.on ) {
        case YES:
            NSLog(@"开了");
            break;
        case NO:
            NSLog(@"关了");
            break;

           
        default:
            break;
    }
}


                                          UIStepper加减控件


//UIStepper 加减控件 继承UIcontrol没有初始化方法
    UIStepper *aStepper = [[UIStepper alloc]initWithFrame:CGRectMake(100, 100, 0, 0)];
    aStepper.layer.cornerRadius = 10;
    //设置边框颜色
    aStepper.tintColor = [UIColor redColor];
    //设置背景颜色
    aStepper.backgroundColor = [UIColor cyanColor];
   
    //设置aStepper的最小值  默认最小值是0.0
    aStepper.minimumValue =10.0;
    //设置aStepper的最大值  默认是100
    aStepper.maximumValue = 20.0;
    //设置长按按钮时数值是否自动增加或减小 默认是增加
    aStepper.autorepeat = NO;
    //设置控件所代表的数值,当大于最大值或小于最小值的时候,是否从另一头开始
//    aStepper.wraps = YES;
    //设置点击按钮时,数值的变化值,就是每次增多少
    aStepper.stepValue = 5;
   
   
    //astepper关联事件
    [aStepper addTarget:self action:@selector(handleStepper:) forControlEvents:(UIControlEventValueChanged)];
       [self.view addSubview:aStepper];
    [aStepper release];
#pragma mark  UIStepper的关联事件
- (void)handleStepper:(UIStepper *)aStep
{
    //aStep.value)代表这个控件当前的数值
    NSLog(@"%f",aStep.value);
}

                                    UISegmentedControl 分段控制器

 //UISegmentedControl分段控制器,次控件由多个分段组成,每一个分段就相当于一个button
   
    //创建segmentedControl对象,并为每个分段添加title
    NSArray *titles = @[@"红色",@"绿色背景",@"蓝色",@"橙色",@"紫色"];
    UISegmentedControl  *aSengment = [[UISegmentedControl alloc]initWithItems:titles];
   
    //UISegmentedControl每个标题的宽度默认是等分总宽度的
    aSengment.frame = CGRectMake(20, 40, 280, 40);
    //设置UISegmentedControl的外观颜色
    aSengment.tintColor = [UIColor magentaColor];
    //设置默认选中的分段
    aSengment.selectedSegmentIndex = 0;
    self.view.backgroundColor = [UIColor redColor];
    //修改分段的宽度
    [aSengment setWidth:60 forSegmentAtIndex:1];
    //UISegmentedControl关联事件
    [aSengment addTarget:self action:@selector(handleSengment:) forControlEvents:(UIControlEventValueChanged)];
        //添加到父视图
    [self.view addSubview:aSengment];
    [aSengment release];


#pragma mark  UISegmentedControl的关联事件
- (void)handleSengment:(UISegmentedControl *)aSegmet{
   
    //    aSegmet.selectedSegmentIndex 返回当前分段控制器被选中的下标
    //    NSLog(@"%ld",aSegmet.selectedSegmentIndex);
    switch (aSegmet.selectedSegmentIndex) {
        case 0:
            self.view.backgroundColor = [UIColor redColor];
            break;
        case 1:
            self.view.backgroundColor = [UIColor greenColor];
            break;
        case 2:
            self.view.backgroundColor = [UIColor blueColor];
            break;
        case 3:
            self.view.backgroundColor = [UIColor orangeColor];
            break;
        case 4:
            self.view.backgroundColor = [UIColor purpleColor];
            break;
        default:
            break;
    }
   
   
   
   
   
}


                       UISlider滑块控件

               

//UISlider滑块控件继承UIcontrol主要用来当前播放进度,控制音量
    UISlider *aSlider = [[UISlider alloc]initWithFrame:CGRectMake(20, 100, 280, 30)];
    //设置滑块最小值
    aSlider.minimumValue = 0.0;
    //设置滑块最大值
    aSlider.maximumValue = 1.0;
    //设置滑块当前的数值
    aSlider.value = 0.5;
    //设置划过区域的颜色
    aSlider.minimumTrackTintColor = [UIColor orangeColor];
    //设置未划过去的颜色
    aSlider.maximumTrackTintColor  = [UIColor blackColor];
    //设置滑块的图片
    [aSlider setThumbImage:[UIImage imageNamed:@"200"] forState:(UIControlStateNormal)];
 
   
//    slider添加关联事件
    [aSlider addTarget:self action:@selector(handleSlider:) forControlEvents:(UIControlEventValueChanged)];
   
   
    //添加到父视图上
    [self.view addSubview:aSlider];
    [aSlider release];
   
    self.view.backgroundColor =[UIColor blueColor];
                       
#pragma mark  UISlider的关联事件
- (void)handleSlider:(UISlider *)aSlider
{
    //返回的是当前滑块所在位置代表的数值
    NSLog(@"%.2f",aSlider.value);
    //通过滑块控制视图的透明度
    self.view.alpha = aSlider.value;
}











































评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值