iPhone之實現自訂進度條Progress

來源:互聯網
上載者:User
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.csdn.net/detail/rhljiayou/5870535

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.