添加自定义的View视图类(自定义一个进度条)
2015-12-29 21:34
411 查看
1.新建一个自定义类
CustomProgress.h
.h中声明该自定义的属性及方法
CustomProgress.m
1.初始化一个该类的实例变量
2.设置该变量的属性
CustomProgress.h
#import <UIKit/UIKit.h> @interface CustomProgress : UIView /** 进度条的背景图片 */ @property (strong, nonatomic) UIImage *backImage; /** 进度条的前景图片 */ @property (strong, nonatomic) UIImage *foreImage; /** 进度条的当前值 */ @property (nonatomic)float progress; /** 设置进度,并且判断进度条是否有变化 */ - (void)setProgress:(float)progress withAnimation :(BOOL)animated; @end
.h中声明该自定义的属性及方法
CustomProgress.m
#import "CustomProgress.h" @implementation CustomProgress{ UIImageView *backgroundImageView; UIImageView *foregroundImageView; } //将图片设置到相应的imageview上 //调用backimage的setter方法,给backgroundimageview设置image属性 - (void)setBackImage: (UIImage *)backImage { _backImage = backImage; backgroundImageView.image = backImage; } - (void)setForeImage:(UIImage *)foreImage{ _foreImage = foreImage; foregroundImageView.image = foreImage; } //重写uiview的初始化方法 - (id) initWithFrame:(CGRect)frame{ //用父类创建本类对象 self = [super initWithFrame:frame]; if (self) { //如果创建成功,则添加两个imageview到self上 backgroundImageView = [[UIImageView alloc] init]; backgroundImageView.frame = CGRectMake(0, 0, frame.size.width, frame.size.height); [self addSubview:backgroundImageView]; foregroundImageView = [[UIImageView alloc] init]; foregroundImageView.frame = CGRectMake(0, 0, frame.size.width * _progress, frame.size.height); [self addSubview:foregroundImageView]; } return self; } - (void)setProgress:(float)progress withAnimation: (BOOL)animated{ if (animated) { //进度条的动画设置 [UIView animateWithDuration:1.5 animations:^{ self.progress = progress; }]; }else{ self.progress = progress; } } - (void)setProgress:(float)progress{ //根据传递进来的progress值,改变前景的imageview的宽度,主函数中,用这个方法设置前景图的初始状态 foregroundImageView.frame = CGRectMake(0, 0, self.frame.size.width * progress, self.frame.size.height); } @endmainViewController中调用自定义类
1.初始化一个该类的实例变量
2.设置该变量的属性
- (void) createCustomProgress { CustomProgress *customProgress = [[CustomProgress alloc] initWithFrame:CGRectMake(30, 350, self.view.frame.size.width - 60, 20)]; UIImage *tempImage = [[UIImage imageNamed:@"9"] stretchableImageWithLeftCapWidth:14 topCapHeight:0]; //- (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight: /* (NSInteger)topCapHeight 这个函数是UIImage的一个实例函数,它的功能是创建一个内容可拉伸,而边角不拉伸的图片,需要两个参数,第一个是左边不拉伸区域的 宽度,第二个参数是上面不拉伸的高度。 根据设置的宽度和高度,将接下来的一个像素进行左右扩展和上下拉伸。 注意:可拉伸的范围都是距离leftCapWidth后的1竖排像素,和距离topCapHeight后的1横排像素。 参数的意义是,如果参数指定10,5。那么,图片左边10个像素,上边5个像素。不会被拉伸,x坐标为11和一个像素会被横向复制,y坐标为6的一个像素会被纵向复制。 注意:只是对一个像素进行复制到一定宽度。而图像后面的剩余像素也不会被拉伸。 */ customProgress.backImage = tempImage; customProgress.foreImage = [[UIImage imageNamed:@"8"] stretchableImageWithLeftCapWidth:14 topCapHeight:0]; [customProgress setProgress:0];// customProgress.progress = 0 // customProgress.backgroundColor = [UIColor redColor]; [self.view addSubview:customProgress]; [customProgress setProgress:0.8 withAnimation:YES]; }
相关文章推荐
- Linux下查看、关闭及开启防火墙命令
- Excel公式-求最低价网站名字
- 数据结构(三)
- delphi 更改DBGrid 颜色技巧
- LeetCode Game of Life
- 【转】Geary's C
- 【参考文献--整理版】
- ❀自我唠嗑-UI-UIView
- js入门基础
- 第十六周--数据结构--项目一-- 插入排序之排序中输出每一趟的中间结果
- 对于bson和mongoDB的小记
- 【LVL1_6_c】【思考】X86 c语言结构体数据对齐的规则
- Ubuntu_ROS中应用kinect v2笔记
- EPC电子标签的一些基本知识
- Ubuntu_ROS中应用kinect v2笔记
- Ubuntu_ROS中应用kinect v2笔记
- 设计模式学习 之 策略模式
- 遥望布达拉——DAY11 海通兵站-觉巴村
- CSS3-html,样式与样式表的创建,选择器
- 艺龙十万级服务器监控系统开发的架构和心得