我有UITextField和Progress视图。我希望该用户在文本字段中输入值,并且在该时间内,进度视图应填充至100%且平滑动画,并且应在标签中显示进程完成百分比
我已为此编写了此代码,它显示了已填充进度视图的平滑动画,但未显示%进程完成。
这是我的代码:
#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UILabel *lblProgres;
@property (weak, nonatomic) IBOutlet UITextField *txtValue;
@property (weak, nonatomic) IBOutlet UIProgressView *progressBar;
- (IBAction)btnRestPressed:(id)sender;
- (IBAction)btnAction:(id)sender;
@end
@implementation ViewController
//-----------------------------------------------------------------------
#pragma mark - Memory Managment Method
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//-----------------------------------------------------------------------
#pragma mark - Action Method
- (IBAction)btnRestPressed:(id)sender {
[self.progressBar setProgress:0.0];
}
- (IBAction)btnAction:(id)sender {
float val1 = 1/[self.txtValue.text floatValue];
// [self.progressBar setProgress:0];
NSTimer *timer = [NSTimer timerWithTimeInterval:val1 target:self selector:@selector(startedProgress:)userInfo:nil repeats:YES];
[timer fire];
if(self.progressBar.progress != 1.0)
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
}
//-----------------------------------------------------------------------
#pragma mark - Custom Method
- (void) startedProgress:(NSTimer *)timer{
float val1 = 1/[self.txtValue.text floatValue];
[UIView animateWithDuration:1/val1 animations:^{
float i=0;;
if(self.progressBar.progress >= 0.0) {
// for(i = 1; i <= 100*10; i++) {
[self.progressBar setProgress:[self.progressBar progress] + 1 /val1 animated:YES] ;
NSLog(@"%f",self.progressBar.progress);
// }
if(self.progressBar.progress == 1.0) {
[timer invalidate];
}
}
} completion:^(BOOL finished) {
}];
}
//-----------------------------------------------------------------------
#pragma mark - Initalize Method
+ (ViewController *)viewController {
ViewController *view = [[ViewController alloc] initWithNibName:@"View" bundle:nil];
return view;
}
//-----------------------------------------------------------------------
#pragma mark - Lifecycle Method
- (void)viewDidLoad {
[super viewDidLoad];
}
@end
答案 0 :(得分:0)
添加
self.lblProgres.text = [NSString stringWithFormat:@"%f %",self.progressBar.progress*100];
在
[self.progressBar setProgress:[self.progressBar progress] + 1 /val1 animated:YES] ;