iOS - 關(guān)于百分比圓環(huán)的解讀

前言:

百分比圓環(huán)幾乎是所有項(xiàng)目蛾狗,中都會有的。針對于下面的Demo,我們采取雙圓的形式仪媒,進(jìn)行百分比圓環(huán)的案例分析沉桌,并且封裝后,以方便備用算吩。

案例展示

yuanhuan.gif

在這當(dāng)中留凭,有以下幾個難點(diǎn),需要我們清楚:

  • 1.畫圓弧利用的是貝塞爾曲線偎巢,我們必須清楚顯示圓環(huán)和輔助圓環(huán)的起點(diǎn)角度和終點(diǎn)角度蔼夜。
  • 2.顯示圓環(huán)和輔助圓環(huán)的起點(diǎn)相同,終點(diǎn)不同
  • 3.顯示圓環(huán)和輔助圓環(huán)重合(半徑相同压昼,位置相同)求冷,顯示圓環(huán)的終點(diǎn)小于等于輔助圓環(huán)的終點(diǎn)

必須理解一下三幅圖
第一幅圖


第二幅圖

通過上圖,我們可以這樣想,終點(diǎn)的弧度點(diǎn) - 起點(diǎn)弧度點(diǎn) = 整個圓弧的弧度 巢音,整個圓弧的弧度遵倦,我們可以把它看成0%~100%這樣的百分比,例如:在用20%乘以整個圓弧的弧度這個就說明 官撼,在整個弧度中占有20%是多少梧躺。求出來之后在加上起點(diǎn),就是顯示圓的終點(diǎn)傲绣。

第三幅圖


通過上幅圖掠哥,我們要知道起點(diǎn)和終點(diǎn)是怎么平行在一條線上。

代碼示例
ZYCircle.h

#import <UIKit/UIKit.h>
@interface ZYCircle : UIView
//起點(diǎn) 角度
@property(nonatomic) CGFloat startAngle;
//終點(diǎn) 角度
 @property(nonatomic)CGFloat endInnerAngle;
//線寬
@property(nonatomic)CGFloat lineWith;
//百分比數(shù)字
@property (nonatomic)CGFloat percentage;
//基準(zhǔn)圓環(huán)顏色
@property(nonatomic,strong)UIColor *unfillColor;
//顯示圓環(huán)顏色
@property(nonatomic,strong)UIColor *fillColor;
//中心數(shù)據(jù)顯示標(biāo)簽
@property (nonatomic ,strong)UILabel *centerLable;
- (instancetype) initWithFrame:(CGRect)frame;
@end

ZYCircle.m
#import "ZYCircle.h"
#define DEGREES_TO_RADIANS(degrees) ((M_PI * degrees)/ 180)
@interface ZYCircle ()

@property(nonatomic) CGPoint CGPoinCerter;
@property(nonatomic) CGFloat endAngle;
@property(nonatomic) BOOL clockwise;
@end
@implementation ZYCircle
{
    CGFloat _radius;
}
-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
if (self) {
    self.lineWith = 10.0;
    self.fillColor = [UIColor greenColor];
    self.unfillColor = [UIColor lightGrayColor];
    self.clockwise = YES;
    self.backgroundColor = [UIColor clearColor];
    self.percentage = 0;
    self.startAngle = 180;
    self.endAngle = 360;
}
return self;
}
#pragma mark setMethod
/**
 *  畫圖函數(shù)
 *
 *  @param rect rect description
 */
-(void)drawRect:(CGRect)rect{
    [self initData];
    [self drawMiddlecircle];
    [self drawOutCCircle];
}
-(void)initData
{
>
//中心標(biāo)簽設(shè)置
CGFloat center =MIN(self.bounds.size.height/2, self.bounds.size.width/2);
self.CGPoinCerter = CGPointMake(center, center);
self.centerLable = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 2*center, 2*center)];
self.centerLable.textAlignment=NSTextAlignmentCenter;
self.centerLable.backgroundColor=[UIColor clearColor];
self.centerLable.adjustsFontSizeToFitWidth = YES;
self.centerLable.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
self.contentMode = UIViewContentModeRedraw;
[self addSubview: self.centerLable];
//半徑計算
_radius = MIN(self.bounds.size.height/2-self.lineWith/2, self.bounds.size.width/2-self.lineWith/2);
self.centerLable.font = [UIFont systemFontOfSize:_radius/3];
self.centerLable.text = [NSString stringWithFormat:@"%.1lf%%",self.percentage*100];
//起點(diǎn)與終點(diǎn)坐標(biāo)
self.endInnerAngle = DEGREES_TO_RADIANS(self.endInnerAngle);
self.startAngle = DEGREES_TO_RADIANS(self.startAngle);
self.endAngle = self.percentage*(self.endInnerAngle - self.startAngle) + self.startAngle;
//0%標(biāo)簽
UILabel *zeroPercentLable = [[UILabel alloc]init];
zeroPercentLable.text = [NSString stringWithFormat:@"0%%"];
CGFloat zeroPercentLableX =_radius + _radius*cos(self.startAngle);
CGFloat zeroPercentLableY = _radius*sin(self.startAngle)+(self.startAngle>DEGREES_TO_RADIANS(180)?-_radius:_radius);
zeroPercentLable.frame = CGRectMake(zeroPercentLableX, zeroPercentLableY+self.lineWith, 2.1*_radius/7.5 + 15, 25);
zeroPercentLable.textAlignment = NSTextAlignmentCenter;
zeroPercentLable.font = [UIFont boldSystemFontOfSize:_radius/7.5 ];
zeroPercentLable.textColor = [UIColor orangeColor];
[self addSubview:zeroPercentLable];
//100%標(biāo)簽
UILabel *hundredPercentLable = [[UILabel alloc]init];
hundredPercentLable.text = [NSString stringWithFormat:@"100%%"];
CGFloat hundredPercentLableX =_radius + _radius*cos(self.endInnerAngle)-8;
CGFloat hundredPercentLableY = _radius*sin(self.endInnerAngle)+(self.endInnerAngle<DEGREES_TO_RADIANS(180)?-_radius:_radius);
hundredPercentLable.frame = CGRectMake(hundredPercentLableX, hundredPercentLableY+self.lineWith, 2.9*_radius/7.5 + 15, 25);
hundredPercentLable.textAlignment = NSTextAlignmentCenter;
hundredPercentLable.font = [UIFont boldSystemFontOfSize:_radius/7.5 ];
hundredPercentLable.textColor = [UIColor orangeColor];
[self addSubview:hundredPercentLable];
}
#pragma mark - 利用layer的 strokeEnd秃诵、strokeStart和lineWidth 屬性添加CA動畫
- (void)addAnimationOneOnLayer:(CAShapeLayer *)layer duration:(CFTimeInterval)duration
{
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
animation.fromValue = @(0);
animation.toValue = @(1);
animation.duration = duration;
[layer addAnimation:animation forKey:nil];
}
/**
 *  顯示圓環(huán)
 */
-(void )drawOutCCircle{
UIBezierPath *bPath = [UIBezierPath bezierPathWithArcCenter:self.CGPoinCerter radius:_radius startAngle:self.startAngle endAngle:self.endAngle clockwise:self.clockwise];
>
CAShapeLayer *layer =  [CAShapeLayer layer];
layer.lineWidth = self.lineWith;
layer.lineCap = kCALineCapRound;
layer.lineJoin = kCALineJoinRound;
layer.strokeColor = self.fillColor.CGColor;
layer.fillColor = nil;
layer.path = bPath.CGPath;
[self.layer addSublayer:layer];
>
[self addAnimationOneOnLayer:layer duration:1.0];
}
/**
 *  輔助圓環(huán)
 */
-(void)drawMiddlecircle{
UIBezierPath *cPath = [UIBezierPath bezierPathWithArcCenter:self.CGPoinCerter radius:_radius startAngle:self.startAngle endAngle:self.endInnerAngle clockwise:self.clockwise];
cPath.lineWidth=self.lineWith;
cPath.lineCapStyle = kCGLineCapRound;
cPath.lineJoinStyle = kCGLineJoinRound;
UIColor *color = self.unfillColor;
[color setStroke];
[cPath stroke];
}

用法
- (void)setCircle{

ZYCircle *circle = [[ZYCircle alloc] initWithFrame:CGRectMake(30, 20,SCREEN_WIDTH-60 , SCREEN_WIDTH-60)];
circle.percentage = 0.23;    //百分比數(shù)值
circle.lineWith = 30;       //線寬
circle.fillColor = [UIColor redColor];          //填充顏色
circle.unfillColor = [UIColor lightGrayColor];  //未填充顏色
circle.startAngle = 150;        //百分百的圓環(huán)起點(diǎn)
circle.endInnerAngle = 390;     //百分百的圓環(huán)終點(diǎn)
//  circle.centerLable.text = @"111"; //中心標(biāo)簽的顯示數(shù)值续搀,默認(rèn)為百分?jǐn)?shù)
      [self.displayView addSubview:circle];
 }
#pragma mark - 點(diǎn)擊方法
- (IBAction)百分比圓環(huán):(id)sender {
[self setCircle];
 }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市菠净,隨后出現(xiàn)的幾起案子禁舷,更是在濱河造成了極大的恐慌,老刑警劉巖毅往,帶你破解...
    沈念sama閱讀 218,858評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件牵咙,死亡現(xiàn)場離奇詭異,居然都是意外死亡攀唯,警方通過查閱死者的電腦和手機(jī)洁桌,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,372評論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來侯嘀,“玉大人另凌,你說我怎么就攤上這事谱轨。” “怎么了吠谢?”我有些...
    開封第一講書人閱讀 165,282評論 0 356
  • 文/不壞的土叔 我叫張陵土童,是天一觀的道長。 經(jīng)常有香客問我工坊,道長娜扇,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,842評論 1 295
  • 正文 為了忘掉前任栅组,我火速辦了婚禮,結(jié)果婚禮上枢析,老公的妹妹穿的比我還像新娘玉掸。我一直安慰自己,他們只是感情好醒叁,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,857評論 6 392
  • 文/花漫 我一把揭開白布司浪。 她就那樣靜靜地躺著,像睡著了一般把沼。 火紅的嫁衣襯著肌膚如雪啊易。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,679評論 1 305
  • 那天饮睬,我揣著相機(jī)與錄音租谈,去河邊找鬼。 笑死捆愁,一個胖子當(dāng)著我的面吹牛割去,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播昼丑,決...
    沈念sama閱讀 40,406評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼呻逆,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了菩帝?” 一聲冷哼從身側(cè)響起咖城,我...
    開封第一講書人閱讀 39,311評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎呼奢,沒想到半個月后宜雀,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,767評論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡控妻,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,945評論 3 336
  • 正文 我和宋清朗相戀三年州袒,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片弓候。...
    茶點(diǎn)故事閱讀 40,090評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡郎哭,死狀恐怖他匪,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情夸研,我是刑警寧澤邦蜜,帶...
    沈念sama閱讀 35,785評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站亥至,受9級特大地震影響悼沈,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜姐扮,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,420評論 3 331
  • 文/蒙蒙 一絮供、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧茶敏,春花似錦壤靶、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,988評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至恬惯,卻和暖如春向拆,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背酪耳。 一陣腳步聲響...
    開封第一講書人閱讀 33,101評論 1 271
  • 我被黑心中介騙來泰國打工浓恳, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人碗暗。 一個月前我還...
    沈念sama閱讀 48,298評論 3 372
  • 正文 我出身青樓奖蔓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親讹堤。 傳聞我的和親對象是個殘疾皇子吆鹤,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,033評論 2 355

推薦閱讀更多精彩內(nèi)容