ios - CGContextFillPath、CGContextStrokePath的使用规则
PHP中文网
PHP中文网 2017-04-18 09:43:04
0
1
841

1.我想做到的效果是描线并且填充,如:

但是我代码完成后,结果是这样的:

就是说我的描线部分没有显示出来

2.我的代码部分:

/* 划线前预备工作 */ CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetLineCap(context, kCGLineCapRound); CGContextSetLineWidth(context, 1.f); CGContextSetAllowsAntialiasing(context, true); CGContextSetRGBStrokeColor(context, 16.0 / 255.0f, 131.0 / 255.0f, 198.0 / 255.0f, 1.0f); CGContextBeginPath(context); CGFloat height = rect.size.height; CGFloat width = rect.size.width; CGFloat averageWidth = (width - 80)/6.f; /* 画坐标轴 */ CGContextMoveToPoint(context, 1.f, 0); CGContextAddLineToPoint(context, 1.f, height-50); CGContextAddLineToPoint(context, width-1.f, height-50); /* 画坐标标注 */ for (int i = 0; i<7; i++) { CGContextMoveToPoint(context, 41.f+i*averageWidth, height-54); CGContextAddLineToPoint(context, 41.f+i*averageWidth, height-50); } CGContextStrokePath(context); CGContextBeginPath(context); CGContextSetLineWidth(context, 2.f); CGContextSetRGBStrokeColor(context, 16.0 / 255.0f, 131.0 / 255.0f, 198.0 / 255.0f, 1.0f); CGContextSetRGBFillColor(context, 74/255.0f, 179/255.f, 240/255.f, 0.5f); if (_datas != nil) { /* 极值 */ NSInteger discrepancy = _datas.maxmumValue - _datas.minmumValue; /* 每个像素代表的数值 */ CGFloat pxPerValue = (height-54.f-55.f)/ discrepancy; /* 记录每个点的纵坐标 */ NSMutableArray *coorArray = [[NSMutableArray alloc] initWithArray:_datas.verticalCoordinates]; /* 找到最小值对应的坐标点 */ NSInteger minIndex = _datas.minmumIndex; NSInteger minValue = _datas.minmumValue; [coorArray replaceObjectAtIndex:minIndex withObject:[NSNumber numberWithFloat:(height-104.f)]]; /* 描线、填充 */ for (int i = 0; i<7; i++) { if (i != minIndex) { /* 获取元素值 */ NSInteger vertical = [[_datas.verticalCoordinates objectAtIndex:i] integerValue]; /* 获取差值 */ NSInteger disValue = vertical - minValue; /*得到坐标值*/ CGFloat value = (height-104.f) - disValue * pxPerValue ; [coorArray replaceObjectAtIndex:i withObject:[NSNumber numberWithFloat:value]]; } /* 如果是第一个,将点移动到起始点 */ if (i == 0) { CGContextMoveToPoint(context, 41.f, height-50); CGContextAddLineToPoint(context, 41.f, [[coorArray objectAtIndex:0] floatValue]); }else{ CGContextAddLineToPoint(context, 41.f+i*averageWidth, [[coorArray objectAtIndex:i] floatValue]); if (i == 6){ CGContextAddLineToPoint(context, 41.f+6*averageWidth,height-50); CGContextAddLineToPoint(context, 41.f, height-50); } } } CGContextFillPath(context); CGContextStrokePath(context); }

主要的地方就是这:

/* 如果是第一个,将点移动到起始点 */ if (i == 0) { CGContextMoveToPoint(context, 41.f, height-50); CGContextAddLineToPoint(context, 41.f, [[coorArray objectAtIndex:0] floatValue]); }else{ CGContextAddLineToPoint(context, 41.f+i*averageWidth, [[coorArray objectAtIndex:i] floatValue]); if (i == 6){ CGContextAddLineToPoint(context, 41.f+6*averageWidth,height-50); CGContextAddLineToPoint(context, 41.f, height-50); } } CGContextFillPath(context); CGContextStrokePath(context);

不知道为什么不能同时完成划线、填充路径?

PHP中文网
PHP中文网

认证高级PHP讲师

reply all (1)
小葫芦

You put

CGContextFillPath(context); CGContextStrokePath(context);

changed to

CGContextDrawPath(context, kCGPathFillStroke);
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!