添加自定义的View视图类(自定义一个进度条)

来源:互联网 发布:aws windows 2012 编辑:程序博客网 时间:2024/05/21 19:23

1.新建一个自定义类

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);}@end
mainViewController中调用自定义类

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


0 0
原创粉丝点击