UI基础之-UIImage,UIImageView, UISegmentedControl,UISlider
来源:互联网 发布:魔兽争霸3 原生 mac 编辑:程序博客网 时间:2024/05/16 07:37
一 UIImageView和 UIImage
1. UIImage 继承自 NSObject,是用来保存图片资源的
UIImageView继承自 UIView, 用来显示图片的控件,相当于一个相框,用来 展示UIImage 对象的.
2. UIImage 创建图片对象
1⃣获取图片资源的一种方式
UIImage *image = [UIImage imageNamed:@”name.jpg”];
缺点:第一次使用的时候检索比较麻烦,(浪费时间)
优点:这种方式会在拷贝一份放在内存中,以后使用的时候不用在去文件中检索,直接在内存中取就可以了(节省时间)
2⃣获取图片二
①静态图片
也可以使用路径创建图片对象:
NSString *string = [[NSBundlemainBundle]pathForResouce:@”name” ofType:@”jpg”];
UIImage *image1 =[UIImageimageWithContentsOfFile:filePath];
优点:查找的时候比较方便,能通过路径直接拿到,节省时间.
缺点:以后每次查找都要通过路径去找(浪费时间)
创建 UIImageView对象
UIImageView*view = [[UIImageView alloc]initWithImage:image];
为 UIImageView设置位置:
view.frame = CGRectMake(40,30,240,320);
添加到视图:
[self.view addSubview:view];
[view release];
②UIImageView 加载动态图片
NSMutabelArray *array = [NSMutableArrayarrayWithCapacity:0];
图片方式一:
使用for 循环遍历,获得图片
for(inti = 0;i < 22; i++){
NSString*stringName = [NSString stringWithFormat:@”Zombie%d”,i+ 1];
NSString*filePath = [[NSBundle mainBundle]pathForResource:stringName ofType:@”tiff”];
UIImage *image = [UIImageimageWithContentsOfFile:filePath];
}
获取图片方法二:(获取图片名字和格式)
NSString *stringName = [NSStringstringWithFormat:@”Zombie%d.tiff”,i + 1];
UIImage *image = [UIImageimageNamed:stringName];
[array addObject:image];}
//创建对象:
UIImageView *imageView =[[UIImageViewalloc]initWithFrame:CGRectMake(20,30,280,408)];
//设置 imageView 播放的动态图片数组
imageView.animationImages = array;
//设置动态图片的播放速度
imageView.animationDuration = 1;
//设置动态图片循环的次数
imageView.animationRepeatCount = 0; //循环次数为0的时候无限次循环
[self.view addSubview:imageView];
[imageView release];
//设置开始播放动态图片 一定要加上!!!!!
[imageView startAnimating];
3. 等比缩放图片的方法
UIImage*image = [UIImage imageNamed:@”name.jpg”];
UIImageView *imageView = [[UIImageViewalloc]initWithImage:image];
//等比计算出 imageView 的高度
CGFloat heigh = 300*image.size.height/image.size.width;
imageView.frame =CGRectMake(10,20,300,heigh);
[self.view addSubview:imageView];
[imageView release];
二 UISegmentedControl分段控件的使用
1. 有多个分段组成的视图控件,每个分段相当于一个按钮,继承自 UIControl
2. 创建一个 segmentedControl 对象
NSArray *itemArray = @[@”首页”,@”公司简介”,@”产品介绍”];
UISegmentedControl *segmented = [[UISegmentedControl alloc]initWithItems: itemArray];
//设置大小 frame
segmented.frame =CGRectMake(20,30,280,40);
// 设置默认选中的分段
[segmented setSelectedSegmentIndex:1];
//修改字体大小
NSDictionary *dic =@{NSFontAttributeName:[UIFont boldSystemForntOfSize:18],NSForegroundColorAttributeName:[UIColorredColor]};
[segmented setTitleTextAttributes:dicforState:UIControlStateNormal];
//设置分段的颜色
segmented:tintColor = [UIColorblackColor];
//改变标题
[segmented setTitle:@”首页”forSegmentAtIndex:0];
//设置宽度
[segmented setWidth:100 forSegmentAtIndex:1];
//点击事件
[segmented addTarget:selfaction:@selector(segmentCR:) forControlEvents:UIControlEventValueChanged];
UIControlEventValueChanged 事件通常使用 switch进行实现(sender.selectedSegmentIndex)
三 UISlider 滑块控件
1. 创建滑块对象
UISlider *slider = [UISlideralloc]initWithFrame:CGRectMake(10,100,300,10);
slider.minimumTrackTintColor =[UIColor redColor];
slider.maximumTrackTintColor = [UIColorblueColor];
//设置最大值和最小值
slider.minimumValue = 0.0;
slider.maximumValue = 1.0;
//设置滑竿的事件(当选择的值改变的时候触发)
[slider addTarget:self action:@selector(sliderValueChanged:)forControlEvents:UIControlEventValueChanged];
UIControl是控制类控件的基类,凡是继承自 UIControl 都可以实现 addTarget/action方法,也可以通过 removeTarget:action:移除事件.
当我们想为一个类拥有addTarget:action: 方法只需要让他继承 UIControl 就可以.
如果你想定义一个视图,而且能够响应事件,那就它继承自 UIControl.
- UI基础之-UIImage,UIImageView, UISegmentedControl,UISlider
- UI - UISegmentedControl、UISlider、UIImageView、UISwitch
- UI 常用方法总结之--- UIImageView UISlider UISegmentedControl (不断更新中)
- UI一揽子计划 6 (UIControl、UISegmentedControl、UIImageView插入数组图片、UISlider)
- UI 第六节 uisegmentedControl uiimage uislider 课后习题
- 【IOS 开发】基本 UI 控件详解 (UISegmentedControl | UIImageView | UIProgressView | UISlider | UIAlertView )
- xib系列---基本 UI 控件详解 (UISegmentedControl | UIImageView | UIProgressView | UISlider | UIAlertView )
- 【IOS 开发】基本 UI 控件详解 (UISegmentedControl | UIImageView | UIProgressView | UISlider | UIAlertView )
- UISegmentedControl + UISlider +UIImageView的动画效果
- UISlider/UISegmentedControl/UISetpper/UISwitch/UIProgressView/UIPageControl/UIImageView
- IOS--UI--LessonUIControl UIImageView/ UISegmentedControl
- 黑马程序员_iOS 的基础控件之UILabel,UISlider,UIImageView
- 随意细解:UI -- UISegmentedControl、UISlider
- Snail—UI学习之UIImage及UIImageView
- UI基础学习之(九):UISlider
- UI基础学习之(八):UISegmentedControl
- AVAudioPlayer&UIImageView的Tomcat连续播放&UISlider&UISegmentedControl
- UI day 6 UIImageView UIImage图片类 UISitch开关控件 UIStepper加减控件 U UISegmentedControl 分段控制器
- ExtJS5.0的mvvm分层思想
- 【C++】C++文件操作详解(ifstream、ofstream、fstream)
- SecureCRT上传下载文件教程(Zmodem使用教程)
- JAVA之Map
- 无限极健康素的功效你了解吗
- UI基础之-UIImage,UIImageView, UISegmentedControl,UISlider
- Exchange 2013 的会议室邮箱用户一直无法正常登陆。
- hdu1852 快速模幂
- listview 定位到某个item
- 1001. A+B Format (20)
- tomcat 服务器配置 https 双向认证
- C/C++拾遗(十九):MFC - 一个简单的窗口程序
- CAS 客户端配置
- linux 下安装Djiango