ios – UIView drawRect:绘制倒像素,制作一个洞,一个窗口,负空间

ios – UIView drawRect:绘制倒像素,制作一个洞,一个窗口,负空间,第1张

概述使用下面的代码,我绘制一个圆角矩形.它绘制了一个漂亮的浅灰色填充圆角矩形(大小为“self”).我实际上想绘制这个像素的倒数,即:不是一个实心的圆角矩形,而是一个实心的浅灰色矩形中这个圆形矩形形状的窗口或孔. 我需要使用反向剪辑方法吗?或者我需要使用bezier路径?借口如果这是非常基本的,虽然找不到信息. 谢谢阅读! - (void)drawRect:(CGRect)rect{ // 使用下面的代码,我绘制一个圆角矩形.它绘制了一个漂亮的浅灰色填充圆角矩形(大小为“self”).我实际上想绘制这个像素的倒数,即:不是一个实心的圆角矩形,而是一个实心的浅灰色矩形中这个圆形矩形形状的窗口或孔.

我需要使用反向剪辑方法吗?或者我需要使用bezIEr路径?借口如果这是非常基本的,虽然找不到信息.

谢谢阅读!

- (voID)drawRect:(CGRect)rect{    // get the context    CGContextRef context = UIGraphicsGetCurrentContext    CGContextSaveGState(context);        //draw the rounded rectangle    CGContextSetstrokecolorWithcolor(context,[[UIcolor blackcolor] CGcolor]);    CGContextSetRGBFillcolor(context,0.8,1.0);    CGContextSetlinewidth(context,_linewidth);    CGRect rrect = CGRectMake(CGRectGetMinX(rect),CGRectGetMinY(rect),CGRectGetWIDth(rect),CGRectGetHeight(rect));    CGfloat radius = _cornerRadius;    CGfloat minx = CGRectGetMinX(rrect),mIDx = CGRectGetMIDX(rrect),maxx = CGRectGetMaxX(rrect);    CGfloat miny = CGRectGetMinY(rrect),mIDy = CGRectGetMIDY(rrect),maxy = CGRectGetMaxY(rrect);    CGContextMovetoPoint(context,minx,mIDy);    // Add an arc through 2 to 3    CGContextAddArctopoint(context,miny,mIDx,radius);    // Add an arc through 4 to 5    CGContextAddArctopoint(context,maxx,mIDy,radius);    // Add an arc through 6 to 7    CGContextAddArctopoint(context,maxy,radius);    // Add an arc through 8 to 9    CGContextAddArctopoint(context,radius);    // Close the path    CGContextClosePath(context);    // Fill the path    CGContextDrawPath(context,kCGPathFill);    CGContextRestoreGState(context);}
解决方法 在上下文中添加多个子路径,并使用模式kCGPathEOFill进行绘制. The Quartz 2D Programming Guide explains in more detail.

// Outer subpath: the whole rectCGContextAddRect(context,rrect);// Inner subpath: the area insIDe the whole rect    CGContextMovetoPoint(context,mIDy);...// Close the inner subpathCGContextClosePath(context);// Fill the pathCGContextDrawPath(context,kCGPathEOFill);
总结

以上是内存溢出为你收集整理的ios – UIView drawRect:绘制倒像素,制作一个洞,一个窗口,负空间全部内容,希望文章能够帮你解决ios – UIView drawRect:绘制倒像素,制作一个洞,一个窗口,负空间所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1005021.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-22
下一篇2022-05-22

发表评论

登录后才能评论

评论列表(0条)

    保存