效果如下:
ViewController.h
1 #import <UIKit/UIKit.h> 2 #import "KMProgressViewWithLabel.h" 3 4 @interface ViewController : UIViewController 5 @property (strong, nonatomic) KMProgressViewWithLabel *progressViewCustom; 6 7 @end
ViewController.m
1 #import "ViewController.h" 2 3 @interface ViewController () 4 - (void)layoutUI; 5 - (void)updateProgressView:(KMProgressViewWithLabel *)component; 6 @end 7 8 @implementation ViewController 9 10 - (void)viewDidLoad { 11 [super viewDidLoad]; 12 13 [self layoutUI]; 14 } 15 16 - (void)didReceiveMemoryWarning { 17 [super didReceiveMemoryWarning]; 18 // Dispose of any resources that can be recreated. 19 } 20 21 - (void)layoutUI { 22 self.navigationItem.title = @"在进度条中显示进度百分比"; 23 self.view.backgroundColor = [UIColor whiteColor]; 24 25 _progressViewCustom = [[KMProgressViewWithLabel alloc] init]; 26 27 UIBarButtonItem *barBtnProgress = [[UIBarButtonItem alloc] initWithCustomView:_progressViewCustom]; 28 UIBarButtonItem *barBtnFlexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; 29 [self setToolbarItems:@[barBtnFlexibleSpace, barBtnProgress, barBtnFlexibleSpace] animated:NO]; 30 } 31 32 - (void)viewWillAppear:(BOOL)animated { 33 [self.navigationController setNavigationBarHidden:NO animated:animated]; 34 [self.navigationController setToolbarHidden:NO animated:animated]; 35 } 36 37 - (void)viewDidAppear:(BOOL)animated { 38 [super viewDidAppear:animated]; 39 [self performSelector:@selector(updateProgressView:) 40 withObject:_progressViewCustom 41 afterDelay:5]; 42 } 43 44 - (void)viewWillDisappear:(BOOL)animated { 45 [super viewWillDisappear:animated]; 46 _progressViewCustom.hidden = YES; 47 } 48 49 - (void)updateProgressView:(KMProgressViewWithLabel *)component { 50 if (!component.hidden) { 51 if (component.progressView.progress < 1.0) { 52 component.progressView.progress += 0.1; 53 component.lblProgress.text = [NSString stringWithFormat:@"下载进度: %.0f%%", component.progressView.progress * 100]; 54 [self performSelector:@selector(updateProgressView:) 55 withObject:component 56 afterDelay:1]; 57 } else { 58 component.lblProgress.text = @"下载成功"; 59 } 60 } 61 } 62 63 @end
KMProgressViewWithLabel.h
1 #import <UIKit/UIKit.h> 2 3 @interface KMProgressViewWithLabel : UIView 4 @property (strong, nonatomic) UILabel *lblProgress; 5 @property (strong, nonatomic) UIProgressView *progressView; 6 7 @end
KMProgressViewWithLabel.m
1 #import "KMProgressViewWithLabel.h" 2 3 @interface KMProgressViewWithLabel () 4 - (void)layoutUI; 5 @end 6 7 @implementation KMProgressViewWithLabel 8 9 - (id)init { 10 if (self = [super init]) { 11 [self layoutUI]; 12 } 13 return self; 14 } 15 16 - (void)layoutUI { 17 _lblProgress = [[UILabel alloc] init]; 18 _lblProgress.textAlignment = NSTextAlignmentCenter; 19 _lblProgress.textColor = [UIColor whiteColor]; 20 _lblProgress.font = [UIFont boldSystemFontOfSize:14.0]; 21 _lblProgress.shadowColor = [UIColor blackColor]; 22 [self addSubview:_lblProgress]; 23 24 _progressView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleBar]; 25 [self addSubview:_progressView]; 26 27 self.frame = CGRectMake(0, 0, _progressView.bounds.size.width, _progressView.bounds.size.height * 5); 28 } 29 30 /** 31 * 重新绘制UILabel和UIProgressView的位置和大小外观 32 */ 33 - (void)layoutSubviews { 34 CGRect newPoint = self.bounds; 35 newPoint.size.height -= _progressView.frame.size.height; 36 _lblProgress.frame = newPoint; 37 38 newPoint = _progressView.frame; 39 newPoint.origin.y = _lblProgress.frame.size.height + 4.0; 40 _progressView.frame = newPoint; 41 } 42 43 @end
AppDelegate.h
1 #import <UIKit/UIKit.h> 2 3 @interface AppDelegate : UIResponder <UIApplicationDelegate> 4 @property (strong, nonatomic) UIWindow *window; 5 @property (strong, nonatomic) UINavigationController *navigationController; 6 7 @end
AppDelegate.m
1 #import "AppDelegate.h" 2 #import "ViewController.h" 3 4 @interface AppDelegate () 5 @end 6 7 @implementation AppDelegate 8 9 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 10 _window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 11 ViewController *viewController = [[ViewController alloc] init]; 12 _navigationController = [[UINavigationController alloc] initWithRootViewController:viewController]; 13 _window.rootViewController = _navigationController; 14 //[_window addSubview:_navigationController.view]; //当_window.rootViewController关联时,这一句可有可无 15 [_window makeKeyAndVisible]; 16 return YES; 17 } 18 19 - (void)applicationWillResignActive:(UIApplication *)application { 20 } 21 22 - (void)applicationDidEnterBackground:(UIApplication *)application { 23 } 24 25 - (void)applicationWillEnterForeground:(UIApplication *)application { 26 } 27 28 - (void)applicationDidBecomeActive:(UIApplication *)application { 29 } 30 31 - (void)applicationWillTerminate:(UIApplication *)application { 32 } 33 34 @end
时间: 2024-10-13 00:15:42