进度封装

#import <UIKit/UIKit.h>

@interface CertificationProgressView : UIView

-(void)setnumber:(CGFloat)floNumber;

@end
#import "CertificationProgressView.h"

@interface CertificationProgressView ()
@property (nonatomic,strong) NSTimer *progressTimer;
@property (nonatomic,strong) UILabel *numberLabel;
@property (nonatomic,strong) UIProgressView *progress;
@property (nonatomic,strong) UIImageView *image;
@end

@implementation CertificationProgressView


- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.numberLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 80, 20)];
        self.numberLabel.backgroundColor = [UIColor redColor];
        self.numberLabel.textColor = [UIColor whiteColor];
        self.numberLabel.font = [UIFont systemFontOfSize:9.0f];
        self.numberLabel.text = @"资料完善度0%";
        self.numberLabel.layer.cornerRadius = 3;
        self.numberLabel.layer.masksToBounds = YES;
        self.numberLabel.textAlignment = NSTextAlignmentCenter;
        [self addSubview:self.numberLabel];
        
        self.image = [[UIImageView alloc]initWithFrame:CGRectMake(self.numberLabel.frame.origin.x + 3, self.numberLabel.frame.size.height, 9, 4)];
        self.image.image = [UIImage imageNamed:@"red__down"];
        [self addSubview:self.image];
        
        self.progress = [[UIProgressView alloc]initWithFrame:CGRectMake(0, 25, CGRectGetWidth([UIScreen mainScreen].bounds), 5)];
        self.progress.progressTintColor = [UIColor blueColor];
        self.progress.trackTintColor = [UIColor redColor];
        [self addSubview:self.progress];
        
        self.progressTimer = [NSTimer timerWithTimeInterval:0.01 target:self selector:@selector(testAction) userInfo:nil repeats:YES];
        [[NSRunLoop currentRunLoop] addTimer:_progressTimer forMode:NSRunLoopCommonModes];
    }
    return self;
}


- (void)testAction

{
    CALayer *layer = self.numberLabel.layer.presentationLayer;
    NSLog(@"%.2f",layer.frame.origin.x);
    
    self.numberLabel.text = [NSString stringWithFormat:@"资料完善度%.0f%%",layer.frame.origin.x/(CGRectGetWidth([UIScreen mainScreen].bounds) - layer.frame.size.width) * 100];
    self.progress.progress = layer.frame.origin.x/(CGRectGetWidth([UIScreen mainScreen].bounds) - layer.frame.size.width);
}


-(void)setnumber:(CGFloat)floNumber{
    CGFloat xx = (CGRectGetWidth([UIScreen mainScreen].bounds) - 80 ) * floNumber;
    [self.progressTimer setFireDate:[NSDate distantPast]];
    [UIView animateWithDuration:5.0f animations:^{
        self.numberLabel.frame = CGRectMake(xx, 0, 80, 20);
        CGFloat aa = xx / (CGRectGetWidth([UIScreen mainScreen].bounds) - 80) * 65;
        self.image.frame = CGRectMake(xx + aa , 20, 9, 4);
    }completion:^(BOOL finished) {
        //[self.progressTimer invalidate];
        [self.progressTimer setFireDate:[NSDate distantFuture]];
    }];
}

引用类,添加

#import "ViewController.h"
#import "CertificationProgressView.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIProgressView *ssss;

@property (nonatomic,strong) CertificationProgressView *certification;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.certification = [[CertificationProgressView alloc]initWithFrame:CGRectMake(0, 100, CGRectGetWidth([UIScreen mainScreen].bounds), 100)];
    [self.view addSubview:self.certification];
    
    // Do any additional setup after loading the view, typically from a nib.
}

- (IBAction)aasdf:(UIButton *)sender {
    [self.certification setnumber:random()%10/10.0];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

猜你喜欢

转载自blog.csdn.net/xiaobo0134/article/details/84768906
今日推荐