效果如下:
ViewController.h
1 #import <UIKit/UIKit.h> 2 3 @interface ViewController : UIViewController<UIActionSheetDelegate, UIAlertViewDelegate> 4 @property (strong, nonatomic) UIProgressView *progressView; 5 6 @end
ViewController.m
1 #import "ViewController.h" 2 3 @interface ViewController () 4 - (void)layoutUI; 5 - (void)updateProgressView:(UIProgressView *)progressView; 6 - (void)showAlert:(NSString *)msg; 7 @end 8 9 @implementation ViewController 10 11 - (void)viewDidLoad { 12 [super viewDidLoad]; 13 14 [self layoutUI]; 15 } 16 17 - (void)didReceiveMemoryWarning { 18 [super didReceiveMemoryWarning]; 19 // Dispose of any resources that can be recreated. 20 } 21 22 - (void)layoutUI { 23 _progressView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault]; //设置进度条样式;默认值是UIProgressViewStyleDefault,设置的枚举值决定了进度条的高度大小 24 _progressView.frame = CGRectMake(0, 0, 220, 0); 25 _progressView.center = self.view.center; 26 [self.view addSubview:_progressView]; 27 } 28 29 - (void)viewDidAppear:(BOOL)animated { 30 [super viewDidAppear:animated]; 31 [self performSelector:@selector(updateProgressView:) 32 withObject:_progressView 33 afterDelay:5]; 34 } 35 36 - (void)viewWillDisappear:(BOOL)animated { 37 [super viewWillDisappear:animated]; 38 _progressView.hidden = YES; 39 } 40 41 - (void)updateProgressView:(UIProgressView *)progressView { 42 if (!progressView.hidden) { 43 if (progressView.progress < 1.0) { 44 progressView.progress += 0.1; 45 [self performSelector:@selector(updateProgressView:) 46 withObject:progressView 47 afterDelay:1]; 48 } else { 49 UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"下载完成" 50 delegate:self 51 cancelButtonTitle:@"取消" 52 destructiveButtonTitle:@"重新下载" otherButtonTitles:@"确定1", @"确定2", nil]; 53 [actionSheet showInView:self.view]; 54 55 NSLog(@"[actionSheet title]=%@", [actionSheet title]); 56 NSLog(@"[actionSheet numberOfButtons]=%ld", (long)[actionSheet numberOfButtons]); 57 } 58 } 59 } 60 61 - (void)showAlert:(NSString *)msg { 62 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"ActionSheet选择项" 63 message:msg 64 delegate:self 65 cancelButtonTitle:nil 66 otherButtonTitles:@"确定", nil]; 67 [alert show]; 68 } 69 70 #pragma mark - ActionSheet 71 - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { 72 switch (buttonIndex) { 73 case 0: 74 [self showAlert:[NSString stringWithFormat:@"buttonIndex=%ld, destructiveButtonTitle=重新下载", buttonIndex]]; 75 _progressView.progress = 0.0; 76 [self updateProgressView:_progressView]; 77 break; 78 case 1: 79 case 2: 80 [self showAlert:[NSString stringWithFormat:@"buttonIndex=%ld, otherButtonTitles=确定1和确定2", buttonIndex]]; 81 [self performSelector:@selector(updateProgressView:) 82 withObject:_progressView 83 afterDelay:5]; 84 break; 85 case 3: 86 [self showAlert:[NSString stringWithFormat:@"buttonIndex=%ld, cancelButtonTitle=取消", buttonIndex]]; 87 _progressView.hidden = YES; 88 break; 89 default: 90 break; 91 } 92 } 93 94 #pragma mark - AlertView 95 - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { 96 NSLog(@"buttonIndex=%ld, otherButtonTitles=确定", (long)buttonIndex); 97 } 98 99 @end
时间: 2024-10-16 15:28:08