iOS   发布时间:2022-03-30  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – UIView drawRect:绘制倒像素,制作一个洞,一个窗口,负空间大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
使用下面的代码,我绘制一个圆角矩形.它绘制了一个漂亮的浅灰色填充圆角矩形(大小为“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 rect
CGContextAddRect(context,rrect);

// Inner subpath: the area inside the whole rect    
CGContextMoveToPoint(context,midy);
...
// Close the inner subpath
CGContextClosePath(context);

// Fill the path
CGContextDrawPath(context,kCGPathEOFill);

大佬总结

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

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

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。