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

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了

首先我們試試先畫一個(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開發(fā)技巧之WeakSelf宏的進(jìn)化詳解

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

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

    詳解ios中scrollView上使用masonry

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

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

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

    IOS中UIImageView方法實(shí)現(xiàn)簡單動(dòng)畫

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

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

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

    iOS仿微信相機(jī)拍照、視頻錄制功能

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

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

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

    IOS倒計(jì)時(shí)設(shè)置UIButton標(biāo)題title的抖動(dòng)問題

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

    淺談Unity中IOS Build Settings選項(xiàng)的作用

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

    iOS中的3種定時(shí)器匯總介紹

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

最新評(píng)論

南华县| 大足县| 漳平市| 榆中县| 喀喇| 徐州市| 晋州市| 陆河县| 海城市| 大同县| 从化市| 施秉县| 上思县| 西宁市| 浑源县| 泗水县| 乌鲁木齐县| 泗阳县| 常德市| 枣庄市| 潢川县| 石阡县| 眉山市| 兰考县| 利津县| 铜陵市| 平谷区| 富川| 东乌| 玛曲县| 三穗县| 临沭县| 峨边| 鹿邑县| 道真| 资兴市| 报价| 宜阳县| 揭东县| 如东县| 宜章县|