亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

IOS 開發(fā)中畫扇形圖實(shí)例詳解

 更新時(shí)間:2017年04月27日 11:49:21   投稿:lqh  
這篇文章主要介紹了IOS 開發(fā)中畫扇形圖實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下

IOS 開發(fā)中畫扇形圖實(shí)例詳解

昨天在做項(xiàng)目中,遇到一個(gè)需要顯示扇形圖的功能,網(wǎng)上搜了一下,發(fā)現(xiàn)code4app里面也沒有找到我想要的那種類似的效果,沒辦法了,只能自己學(xué)習(xí)一下如何畫了。

首先我們需要了解一個(gè)uiview的方法

-(void)drawRect:(CGRect)rect

我們知道了這個(gè)方法,就可以在自定義UIView的子類的- (void)drawRect:(CGRect)rect里面繪圖了,關(guān)于drawrect的調(diào)用周期,網(wǎng)上也是一找一大堆,等下我會(huì)整理一下,轉(zhuǎn)載一篇供你們參考。

廢話少說,下面直接開始代碼

首先我們自定義一個(gè)繼承字uiview的子類,我這里就起名字叫pieview了

首先我們?cè)囋囅犬嬕粋€(gè)圓

#import "pieview.h"
//直徑,其實(shí)radius是半徑的意思吧,哈哈 算了先用著,demo都寫好了就不改了,你們知道就行了
#define radius 50

@implementation pieview

-(void)drawRect:(CGRect)rect
{
    CGContextRef ctx = UIGraphicsGetCurrentContext();//獲取圖形上下文
    CGPoint cent=CGPointMake((self.frame.size.width/2)-radius/2, (self.frame.size.height/2)-radius/2);//設(shè)置圖形開始畫的坐標(biāo)原點(diǎn),根據(jù)實(shí)際需要設(shè)置,我這是隨便寫的
    CGContextAddEllipseInRect(ctx, CGRectMake(cent.x, cent.y, 100, 100));這個(gè)是核心函數(shù),在這里設(shè)置圖形的開始從哪里畫,畫的寬度和高度是多少。如果寬高不一樣 可就是橢圓了啊
     [[UIColor greenColor] set];//設(shè)置顏色
    CGContextFillPath(ctx);//實(shí)心的
    //CGContextStrokePath(ctx);空心的
}

@end

然后我們創(chuàng)建一個(gè)控制器 pieViewController 引用我們的pieview,展示一下效果

#import "pieViewController.h"
//#import "myview.h"
//#import "JYpieview.h"
#import "pieview.h"
@interface pieViewController ()

@end

@implementation pieViewController

- (void)viewDidLoad {
  [super viewDidLoad];
  pieview *view=[[pieview alloc]init];
  view.frame=CGRectMake(4, 150, 150, 300);
  [self.view addSubview:view];

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

好了看一下效果吧
這里寫圖片描述
好了,下面讓我們開始扇形圖的制作吧

#import "pieview.h"
//直徑
#define radius 50
#define PI 3.14159265358979323846

@implementation pieview
//計(jì)算度轉(zhuǎn)弧度
static inline float radians(double degrees) {
  return degrees * PI / 180;
}
-(void)drawRect:(CGRect)rect
{
  CGPoint cent=CGPointMake((self.frame.size.width/2)-radius/2, (self.frame.size.height/2)-radius/2);
  CGContextRef ctx = UIGraphicsGetCurrentContext();
  CGContextClearRect(ctx, rect);

  float angle_start = radians(0.0);
  float angle_end = radians(120.0);
  CGContextMoveToPoint(ctx, cent.x, cent.y);
  CGContextSetFillColor(ctx, CGColorGetComponents( [[UIColor greenColor] CGColor]));
  CGContextAddArc(ctx, cent.x, cent.y, radius, angle_start, angle_end, 0);
  CGContextFillPath(ctx);

  angle_start = angle_end;
  angle_end = radians(360.0);
  CGContextMoveToPoint(ctx, cent.x, cent.y);
  CGContextSetFillColor(ctx, CGColorGetComponents( [[UIColor blueColor] CGColor]));
  CGContextAddArc(ctx, cent.x, cent.y, radius, angle_start, angle_end, 0);
  CGContextFillPath(ctx);
}
@end

在運(yùn)行一下,我們看下效果
這里寫圖片描述
可使有沒有覺得上面的代碼很多重復(fù)的?對(duì)的,我們可以封裝一個(gè)方法 那么重構(gòu)后的代碼我就一次性的貼上去了

#import "pieview.h"
//直徑
#define radius 50
#define PI 3.14159265358979323846

@implementation pieview
//計(jì)算度轉(zhuǎn)弧度
static inline float radians(double degrees) {
  return degrees * PI / 180;
}
static inline void drawArc(CGContextRef ctx, CGPoint point, float angle_start, float angle_end, UIColor* color) {
  CGContextMoveToPoint(ctx, point.x, point.y);
  CGContextSetFillColor(ctx, CGColorGetComponents( [color CGColor]));
  CGContextAddArc(ctx, point.x, point.y, radius, angle_start, angle_end, 0);
  //CGContextClosePath(ctx);
  CGContextFillPath(ctx);
}
-(void)drawRect:(CGRect)rect
{
  CGPoint cent=CGPointMake((self.frame.size.width/2)-radius/2, (self.frame.size.height/2)-radius/2);
  CGContextRef ctx = UIGraphicsGetCurrentContext();
  CGContextClearRect(ctx, rect);

  float angle_start = radians(0.0);
  float angle_end = radians(121.0);
  drawArc(ctx, cent, angle_start, angle_end, [UIColor yellowColor]);

  angle_start = angle_end;
  angle_end = radians(228.0);
  drawArc(ctx, cent, angle_start, angle_end, [UIColor greenColor]);

  angle_start = angle_end;
  angle_end = radians(260);
  drawArc(ctx, cent, angle_start, angle_end, [UIColor orangeColor]);

  angle_start = angle_end;
  angle_end = radians(360);
  drawArc(ctx, cent, angle_start, angle_end, [UIColor purpleColor]);

}
@end

看下運(yùn)行效果圖
這里寫圖片描述

如果我們中途數(shù)據(jù)變了 想要改一下圖形怎么辦呢?

那么我們就需要用到這個(gè)

  //通知自定義的view重新繪制圖形
//  [self setNeedsDisplay];

這時(shí)候我們就要pieview向外界提供一個(gè)接口屬性,這是我做的模擬5面之后改變圓形的直徑大小

.h文件

#import <UIKit/UIKit.h>

@interface pieview : UIView
//直徑
@property(nonatomic,assign)float radius;
@end

.m文件

#import "pieview.h"
#define PI 3.14159265358979323846

@implementation pieview
//計(jì)算度轉(zhuǎn)弧度
static inline float radians(double degrees) {
  return degrees * PI / 180;
}
static inline void drawArc(CGContextRef ctx, CGPoint point, float angle_start, float angle_end, UIColor* color,float radius) {
  CGContextMoveToPoint(ctx, point.x, point.y);
  CGContextSetFillColor(ctx, CGColorGetComponents( [color CGColor]));
  CGContextAddArc(ctx, point.x, point.y, radius, angle_start, angle_end, 0);
  //CGContextClosePath(ctx);
  CGContextFillPath(ctx);
}
-(void)drawRect:(CGRect)rect
{
  CGPoint cent=CGPointMake((self.frame.size.width/2)-self.radius/2, (self.frame.size.height/2)-self.radius/2);
  CGContextRef ctx = UIGraphicsGetCurrentContext();
  CGContextClearRect(ctx, rect);

  float angle_start = radians(0.0);
  float angle_end = radians(121.0);
  drawArc(ctx, cent, angle_start, angle_end, [UIColor yellowColor],self.radius);

  angle_start = angle_end;
  angle_end = radians(228.0);
  drawArc(ctx, cent, angle_start, angle_end, [UIColor greenColor],self.radius);

  angle_start = angle_end;
  angle_end = radians(260);
  drawArc(ctx, cent, angle_start, angle_end, [UIColor orangeColor],self.radius);

  angle_start = angle_end;
  angle_end = radians(360);
  drawArc(ctx, cent, angle_start, angle_end, [UIColor purpleColor],self.radius);

}
-(void)setRadius:(float)radius
{
  _radius=radius;
  [self setNeedsDisplay];
}
@end

pieViewController.m文件

@implementation pieViewController

- (void)viewDidLoad {
  [super viewDidLoad];
  pieview *view=[[pieview alloc]init];
  view.radius=50;
  view.frame=CGRectMake(4, 150, 150, 300);
  [self.view addSubview:view];
  //view.backgroundColor=[UIColor clearColor];
  //模擬5秒后執(zhí)行這個(gè)段代碼
  dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    view.radius=20;
  });
}
- (void)didReceiveMemoryWarning {
  [super didReceiveMemoryWarning];
  // Dispose of any resources that can be recreated.
}
@end

效果
這里寫圖片描述

5秒之后
這里寫圖片描述

感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!

相關(guān)文章

  • iOS中表情鍵盤的完整實(shí)現(xiàn)方法詳解

    iOS中表情鍵盤的完整實(shí)現(xiàn)方法詳解

    這篇文章主要給大家介紹了關(guān)于iOS中表情鍵盤的完整實(shí)現(xiàn)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-06-06
  • iOS中的ipa重簽名(逆向必備)

    iOS中的ipa重簽名(逆向必備)

    這篇文章給大家介紹了ios中的ipa重簽名知識(shí)以及錯(cuò)誤原因及解決俄方案,需要的朋友參考下吧
    2018-01-01
  • ios使用AVFoundation讀取二維碼的方法

    ios使用AVFoundation讀取二維碼的方法

    這篇文章主要介紹了ios使用AVFoundation讀取二維碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-12-12
  • Navigation bar的注意事項(xiàng)詳解

    Navigation bar的注意事項(xiàng)詳解

    本文主要介紹了Navigation bar的注意事項(xiàng)。具有一定的參考價(jià)值,下面跟著小編一起來看下吧
    2017-01-01
  • iPhone X官方文檔的適配學(xué)習(xí)詳解

    iPhone X官方文檔的適配學(xué)習(xí)詳解

    本篇文章主要介紹了iPhone X官方文檔的適配學(xué)習(xí)詳解,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-09-09
  • iOS開發(fā)之widget實(shí)現(xiàn)詳解

    iOS開發(fā)之widget實(shí)現(xiàn)詳解

    這篇文章主要為大家詳細(xì)介紹了iOS開發(fā)之widget實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-09-09
  • 使用UITextField限制只可輸入中,英文,數(shù)字的方法

    使用UITextField限制只可輸入中,英文,數(shù)字的方法

    在我們?nèi)粘i_發(fā)中經(jīng)常遇到一些情況,要UITextField只能輸入某一種特定的字符.比如大寫A-Z或者小寫a-z,或者漢字.或者數(shù)字.那么該如何實(shí)現(xiàn)呢,下面通過這篇文章來看看吧。
    2016-09-09
  • 2016 cocoapods的安裝和使用方法以及版本升級(jí)遇到的問題

    2016 cocoapods的安裝和使用方法以及版本升級(jí)遇到的問題

    CocoaPods是一個(gè)負(fù)責(zé)管理iOS項(xiàng)目中第三方開源庫的工具,通過CocoaPods,我們可以將第三方的依賴庫統(tǒng)一管理起來,配置和更新只需要通過簡單的幾行命令即可完成,需要的朋友可以參考下
    2016-09-09
  • iOS圖片實(shí)現(xiàn)可拉伸不變形的處理操作

    iOS圖片實(shí)現(xiàn)可拉伸不變形的處理操作

    這篇文章主要為大家詳細(xì)介紹了iOS圖片實(shí)現(xiàn)可拉伸不變形的處理操作,通過UIImage對(duì)象調(diào)用該方法,并且傳入要拉伸的圖片的名字作為參數(shù),實(shí)現(xiàn)返回一個(gè)可拉伸不變形的圖片,感興趣的小伙伴們可以參考一下
    2016-05-05
  • 掃描二維碼控件的封裝iOS實(shí)現(xiàn)

    掃描二維碼控件的封裝iOS實(shí)現(xiàn)

    這篇文章主要為大家詳細(xì)介紹了iOS實(shí)現(xiàn)掃描二維碼控件的封裝,具有一定的實(shí)用性和參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-08-08

最新評(píng)論