iPhone之实现自定义进度条Progress

本文介绍了如何在iPhone上不使用UIProgressView,而是通过继承UIView,利用UIImageView创建背景和填充图片来实现自定义进度条效果。通过定时器动态改变填充图片的x坐标,模拟进度变化。

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

iPhone之实现自定义进度条Progress


思路:没有继承UIProgressView,而是继承UIView,添加了两个UIImageView,一个是背影图,再在上面添加了一个填充图并把x坐标设置到屏的最左边,然后一个定时器,隔一段时间改变填充图的x的坐标,这样就实现了类似进度这样的效果。

效果图:


代码:

CustomProgress.h

//
//  CustomProgress.h
//  Chok_passenger
//
//  Created by 任海丽 on 13-7-11.
//  Copyright (c) 2013年 任海丽. All rights reserved.
//
 
#import <UIKit/UIKit.h>
 
@protocol  CustomProgressDelegate<NSObject>
//修改进度标签内容
- (void)changeTextProgress:(NSString*)string;
//进度条结束时
- (void)endTime;
@end
 
@interface CustomProgress : UIView
 
// 背景图像
@property (retain, nonatomic) UIImageView *trackView;
// 填充图像
@property (retain, nonatomic) UIImageView *progressView;
 
@property (retain, nonatomic) NSTimer *progressTimer; //时间定时器
@property (nonatomic) CGFloat targetProgress; //进度
@property (nonatomic) CGFloat currentProgress; //当前进度
 
@property (nonatomic, strong)id<CustomProgressDelegate> delegate;
 
- (void)setProgress:(CGFloat)progress;//设置进度
 
@end

CustomProgress.m

//
//  CustomProgress.m
//  Chok_passenger
//
//  Created by 任海丽 on 13-7-11.
//  Copyright (c) 2013年 任海丽. All rights reserved.
//
 
#import "CustomProgress.h"
 
@implementation CustomProgress
 
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor clearColor];
// 背景图像
_trackView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
[_trackView setImage:[UIImage imageNamed:@"wait_progress_back.png"]];
_trackView.clipsToBounds = YES;//当前view的主要作用是将出界了的_progressView剪切掉,所以需将clipsToBounds设置为YES
[self addSubview:_trackView];
// 填充图像
_progressView = [[UIImageView alloc] initWithFrame:CGRectMake(0-frame.size.width, 0, frame.size.width, frame.size.height)];
[_progressView setImage:[UIImage imageNamed:@"wait_progress.png"]];
[_trackView addSubview:_progressView];
 
_currentProgress = 0.0;
 
}
return self;
}
 
- (void)setProgress:(CGFloat)progress{
if (0 == progress) {
self.currentProgress = 0;
[self changeProgressViewFrame];
return;
}
 
_targetProgress = progress;
if (_progressTimer == nil)
{
//创建定时器
_progressTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(moveProgress) userInfo:nil repeats:YES];
}
}
 
//
//修改进度
- (void) moveProgress
{
//判断当前进度是否大于进度值
if (self.currentProgress < _targetProgress)
{
self.currentProgress = MIN(self.currentProgress + 0.1*_targetProgress, _targetProgress);
if (_targetProgress >=10) {
[_delegate changeTextProgress:[NSString stringWithFormat:@"%d %%",(int)self.currentProgress]];
}else{
[_delegate changeTextProgress:[NSString stringWithFormat:@"%.1f %%",self.currentProgress]];
}
 
//改变界面内容
[self changeProgressViewFrame];
 
} else {
//当超过进度时就结束定时器,并处理代理方法
[_progressTimer invalidate];
_progressTimer = nil;
[_delegate endTime];
}
}
//修改显示内容
- (void)changeProgressViewFrame{
//只要改变frame的x的坐标就能看到进度一样的效果
_progressView.frame = CGRectMake(self.frame.size.width * (_currentProgress/_targetProgress) - self.frame.size.width, 0, self.frame.size.width, self.frame.size.height);
}
 
@end

本文固定链接: http://www.zhouxiaodong.net/?p=99 | 忧郁小洞洞的站


下载链接:http://download.youkuaiyun.com/detail/rhljiayou/5870535

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值