IOS 開發自訂條形ProgressView的執行個體_IOS

來源:互聯網
上載者:User

IOS 自訂進度條 ProgressView,好的進度條,讓人賞心悅目,在等待的時候不是那麼煩躁,也算是增加使用者體驗吧!

進度條在iOS開發中很常見的,我在項目開發中也寫過好多進度條,有好多種類的,條形,圓形等,今天給大家總結一種條形的開發進度條。

簡單思路:

 1.自訂進度條先繼承UIView 建立一個CustomBarProgressView
 2.在.H檔案中外漏的方法《開始的方法》《初始化的方法》
 3.在.M檔案中 利用定時器改變位置 實現進度條

#效果圖

#部分代碼

-(instancetype)initWithFrame:(CGRect)frame withStartNum:(CGFloat)startNum withEndNum:(CGFloat)endNum withSignNum:(CGFloat)signNum withTime:(CGFloat)time{  if (self = [super initWithFrame:frame]) {    self.startNum = startNum;    self.endNum = endNum;    self.signNum = signNum;    if(time == 0){      self.time = 0.1;    }else{      self.time = time;    }    [self setUpSubViews];  }  return self;}- (void)setUpSubViews{  UIView *backView = [[UIView alloc] init];  backView.backgroundColor =BoomViewColor;  backView.layer.cornerRadius = CornerRadius;  backView.layer.masksToBounds = YES;  [self addSubview:backView];  self.backView = backView;  UIView *fontView = [[UIView alloc] init];  fontView.backgroundColor = UpViewColor;  fontView.layer.cornerRadius = CornerRadius;  fontView.layer.masksToBounds = YES;  [self addSubview:fontView];  self.fontView = fontView;}-(void)progressViewStart{  if (self.timer == nil) {    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{      self.timer = [NSTimer scheduledTimerWithTimeInterval:self.time target:self selector:@selector(changeProgressViewFrame:) userInfo:nil repeats:YES];      [[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];    });  }}-(void)changeProgressViewFrame:(NSTimer *)timer{  //位置計算  CGFloat signProgress = (self.signNum - self.startNum) / (self.endNum - self.startNum);  NSLog(@"==>>>%f",self.progress);  if (self.progress >= signProgress){    [self.timer invalidate];    self.timer = nil;    return;  }  self.progress += 0.01;  [self setNeedsLayout];}-(void)layoutSubviews{  [super layoutSubviews];  NSLog(@"==>>>%f",self.progress);  self.backView.frame = self.bounds;  self.fontView.frame = self.bounds;  self.fontView.width = self.width * self.progress;}

PS:可以自己增加 進度條文字等修改大小 樣式

別小看任何人,越不起眼的人。往往會做些讓人想不到的事。。。

感謝閱讀,希望能協助到大家,謝謝大家對本站的支援!

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.