最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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

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

首先我們需要了解一個uiview的方法

-(void)drawRect:(CGRect)rect

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

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

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

首先我們試試先畫一個圓

#import "pieview.h"
//直徑,其實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)原點,根據(jù)實際需要設(shè)置,我這是隨便寫的
    CGContextAddEllipseInRect(ctx, CGRectMake(cent.x, cent.y, 100, 100));這個是核心函數(shù),在這里設(shè)置圖形的開始從哪里畫,畫的寬度和高度是多少。如果寬高不一樣 可就是橢圓了啊
     [[UIColor greenColor] set];//設(shè)置顏色
    CGContextFillPath(ctx);//實心的
    //CGContextStrokePath(ctx);空心的
}

@end

然后我們創(chuàng)建一個控制器 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
//計算度轉(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

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

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

@implementation pieview
//計算度轉(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

看下運行效果圖
這里寫圖片描述

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

那么我們就需要用到這個

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

這時候我們就要pieview向外界提供一個接口屬性,這是我做的模擬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
//計算度轉(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í)行這個段代碼
  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秒之后
這里寫圖片描述

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

相關(guān)文章

  • iOS開發(fā)技巧之WeakSelf宏的進(jìn)化詳解

    iOS開發(fā)技巧之WeakSelf宏的進(jìn)化詳解

    在程序中我們經(jīng)常用到Block,但寫weak self 時會比較繁瑣,下面這篇文章主要給大家介紹了關(guān)于iOS開發(fā)技巧之WeakSelf宏的進(jìn)化的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們一起來看看吧
    2018-05-05
  • 詳解ios中scrollView上使用masonry

    詳解ios中scrollView上使用masonry

    本篇文章主要給大家詳細(xì)分析了ios開發(fā)中scrollView上使用masonry的詳細(xì)知識內(nèi)容,需要的朋友參考下吧。
    2018-02-02
  • iOS 圖片裁剪的實現(xiàn)方法

    iOS 圖片裁剪的實現(xiàn)方法

    本篇文章主要介紹了iOS 圖片裁剪的實現(xiàn)方法,主要介紹了兩種圖片剪裁的方法,具有一定的參考價值,有興趣的可以了解一下。
    2017-03-03
  • IOS中UIImageView方法實現(xiàn)簡單動畫

    IOS中UIImageView方法實現(xiàn)簡單動畫

    這篇文章主要介紹了IOS中UIImageView方法實現(xiàn)簡單動畫的相關(guān)資料,需要的朋友可以參考下
    2017-05-05
  • yii框架分類樹擴(kuò)展示例

    yii框架分類樹擴(kuò)展示例

    這篇文章主要介紹了yii框架分類樹擴(kuò)展示例,提供兩種方式的分類樹格式,表格和下拉框形式的樹形結(jié)構(gòu),需要的朋友可以參考下
    2014-04-04
  • iOS仿微信相機拍照、視頻錄制功能

    iOS仿微信相機拍照、視頻錄制功能

    這篇文章主要為大家詳細(xì)介紹了iOS仿微信相機拍照、視頻錄制功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-05-05
  • IOS開發(fā)筆記整理49之詳解定位CLLocation

    IOS開發(fā)筆記整理49之詳解定位CLLocation

    在項目功能中有一個定位CLLocation的需求,遇到了一些知識難點,經(jīng)過各位大俠的幫助,問題解決,特此分享供大家學(xué)習(xí),希望大家共同學(xué)習(xí)進(jìn)步
    2015-11-11
  • IOS倒計時設(shè)置UIButton標(biāo)題title的抖動問題

    IOS倒計時設(shè)置UIButton標(biāo)題title的抖動問題

    這篇文章主要介紹了IOS倒計時設(shè)置UIButton標(biāo)題title的抖動問題,在發(fā)送驗證碼后,button狀態(tài)需要變?yōu)閐isable,每隔一秒顯示倒計時時間,下面通過本文給大家介紹設(shè)置方法,一起看看吧
    2016-12-12
  • 淺談Unity中IOS Build Settings選項的作用

    淺談Unity中IOS Build Settings選項的作用

    下面小編就為大家分享一篇淺談Unity中IOS Build Settings選項的作用,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-01-01
  • iOS中的3種定時器匯總介紹

    iOS中的3種定時器匯總介紹

    這篇文章主要介紹了iOS中的3種定時器匯總介紹的相關(guān)資料,需要的朋友可以參考下
    2023-05-05

最新評論

广州市| 莱阳市| 兴文县| 石首市| 康保县| 宽甸| 上犹县| 宁强县| 衡山县| 泰顺县| 武陟县| 承德市| 定结县| 当阳市| 绍兴县| 成安县| 资中县| 松阳县| 甘孜| 浏阳市| 晋城| 大邑县| 鄂托克前旗| 阿城市| 肃宁县| 常德市| 长武县| 金华市| 宁明县| 兴海县| 宜城市| 古蔺县| 旬阳县| 麻阳| 安图县| 贵溪市| 佛坪县| 尚志市| 上杭县| 永川市| 兴义市|