iOS uses Uibezierpath to implement Progressview

Source: Internet
Author: User

The implementation results are as follows:

The interface is implemented with UITableView and Tabelviewcell, and the Red view is drawn with Uibezierpath. Note The red part of the upper left corner, the lower left corner is right angle yo!!!! Not much to say < here is really willing to use Uibezierpath to achieve!!!?? The code is as follows:

Controller code:

////VIEWCONTROLLER.M//Progressviewdemo////Created by think Peng on 2017/4/20.//Copyright 2017 think Peng. All rights reserved.//#import"ViewController.h"#import"ProgressTableViewCell.h"@interface Viewcontroller ()<uitableviewdelegate, uitableviewdatasource>@property (nonatomic, strong) UITableView*TableView, @property (nonatomic, strong) Cashapelayer*layer; @end @implementation Viewcontroller- (void) viewdidload {[Super viewdidload]; Self.navigationItem.title=@"Progressdemo"; [Self setui];}#pragmaMark-Setup Interface-(void) Setui {Self.tableview= [[UITableView alloc]initwithframe:cgrectmake (0,0, Self.view.frame.size.width, Self.view.frame.size.height) style:uitableviewstylegrouped]; Self.tableview.Delegate=Self ; Self.tableView.dataSource=Self ; Self.tableView.backgroundColor=[Uicolor Clearcolor]; //Register Cell[Self.tableview Registerclass:[progresstableviewcellclass] Forcellreuseidentifier:@"Cell"]; Self.tableView.tableFooterView=[[UIView alloc]init]; [Self.view AddSubview:self.tableView];}#pragmamark-uitableviewdatasource-(Nsinteger) Numberofsectionsintableview: (UITableView *) TableView {return 1;}-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) Section {return 5;}-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath {Progresstableviewcell*cell = [TableView dequeuereusablecellwithidentifier:@"Cell"Forindexpath:indexpath]; returncell;}#pragmamark-uitableviewdelegate-(CGFloat) TableView: (UITableView *) TableView heightforheaderinsection: (nsinteger) Section {return 0.001f;;}-(CGFloat) TableView: (UITableView *) TableView heightforfooterinsection: (nsinteger) Section {return 0.0001f;}-(CGFloat) TableView: (UITableView *) TableView Heightforrowatindexpath: (Nsindexpath *) Indexpath {return  -;} @end

Tabelviewcell Code:

////PROGRESSTABLEVIEWCELL.M//Progressviewdemo////Created by think Peng on 2017/4/21.//Copyright 2017 think Peng. All rights reserved.//#import"ProgressTableViewCell.h"#import"Masonry.h"#import"ProgressView.h"@interface Progresstableviewcell () @property (nonatomic, strong) UILabel*Titlelabel, @property (nonatomic, strong) Progressview*Progressview, @property (nonatomic, strong) UILabel*Numberlabel, @end @implementation Progresstableviewcell- (void) awakefromnib {[Super awakefromnib]; //Initialization Code}-(Instancetype) Initwithstyle: (Uitableviewcellstyle) style Reuseidentifier: (NSString *) Reuseidentifier {if(self =[Super Initwithstyle:style Reuseidentifier:reuseidentifier])        {[Self createsubviews];    [Self layOut]; }    returnSelf ;}- (void) createsubviews {Self.titlelabel=[[UILabel alloc]init]; Self.titleLabel.font= [Uifont systemfontofsize: -]; Self.titleLabel.text=@"Xidan Grand Yue City"; Self.titleLabel.textAlignment=Nstextalignmentleft;    [Self.contentview AddSubview:self.titleLabel]; Self.progressview=[[Progressview alloc]init]; Self.progressView.backgroundColor=[Uicolor Whitecolor]; Self.progressView.progress= Arc4random_uniform ( -) + +;    [Self.contentview AddSubview:self.progressView]; Self.numberlabel=[[UILabel alloc]init]; Self.numberLabel.font= [Uifont systemfontofsize: -]; Self.numberLabel.text=@"¥2000"; Self.numberLabel.textAlignment=Nstextalignmentright; [Self.contentview AddSubview:self.numberLabel];}- (void) LayOut {[Self.titlelabel mas_makeconstraints:^ (Masconstraintmaker *Make ) {Make.left.mas_equalTo (Self.contentview). Offset (Ten); Make.centerY.mas_equalTo (Self.contentview);//Make.width.greaterThanOrEqualTo (@ ());Make.width.mas_equalTo (Self.contentView.frame.size.width *0.3);    }]; [Self.progressview mas_makeconstraints:^ (Masconstraintmaker *Make ) {Make.left.mas_equalTo (self.titleLabel.mas_right). Offset (Ten); Make.height.mas_equalTo ( -);        Make.centerY.mas_equalTo (Self.titleLabel.mas_centerY); Make.width.mas_equalTo (Self.contentView.frame.size.width*0.4);    }]; [Self.numberlabel mas_makeconstraints:^ (Masconstraintmaker *Make ) {Make.left.mas_equalTo (self.progressView.mas_right). Offset (Ten);        Make.centerY.mas_equalTo (Self.contentview); Make.right.mas_equalTo (Self.contentview). Offset (-Ten); }];} @end

Progressview Code:

////progressview.m//Progressviewdemo////Created by think Peng on 2017/4/20.//Copyright 2017 think Peng. All rights reserved.//#import"ProgressView.h"@interface Progressview () @end @implementation Progressview-(void) DrawRect: (cgrect) rect{//Create Bethel Path        /*cgfloat width = self.progress/rect.size.width * rect.size.width;          Displayed width = The total width of the value/setting returned by the server * full value; Displayed width = Full value * Scale value Scale value = Server returned width/full value*/cgfloat Width= Rect.size.width * Self.progress/Rect.size.width; //displayed width = The value returned by the server * The total width/full value of the set;Uibezierpath *path = [Uibezierpath bezierpathwithroundedrect:cgrectmake (0,0, width, rect.size.height) byroundingcorners:uirectcornertopright|uirectcornerbottomright Cornerradii:cgsizemake (Rect.size.height, rect.size.height)];    [[Uicolor Redcolor] setfill]; [path fill];}- (void) Setprogress: (cgfloat) progress{_progress=progress; //redraw, the system first creates the context associated with the view and then calls the DrawRect[self setneedsdisplay];} @end

is not super simple, but I did not think at first, or own too much food. ... Fight IT!!!

iOS uses Uibezierpath to implement Progressview

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.