本文介绍了点内圆角矩形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  void ContextAddRoundedRect(CGContextRef ctx,CGRect矩形,CGFloat半径){
CGFloat minX = CGRectGetMinX(rect);
CGFloat maxX = CGRectGetMaxX(rect);
CGFloat minY = CGRectGetMinY(rect);
CGFloat maxY = CGRectGetMaxY(rect);

CGContextMoveToPoint(ctx,minX + radius,minY);
CGContextAddArcToPoint(ctx,maxX,minY,maxX,minY + radius,radius);
CGContextAddArcToPoint(ctx,maxX,maxY,maxX - radius,maxY,radius);
CGContextAddArcToPoint(ctx,minX,maxY,minX,maxY - radius,radius);
CGContextAddArcToPoint(ctx,minX,minY,minX + radius,minY,radius);
}

如何判断给定的CGPoint是否在我的矩形内考虑圆角?

解决方案

不是直接将它构建到 CGContextRef 就像这样,你可以将它构建为,然后使用 - [UIBezierPath containsPoint:] p>

当您准备好实际将路径呈现到上下文中时,您可以通过以下方式进行操作:

  CGContextAddPath(ctx,[myBezierPath CGPath]); 


I have this code to draw rounded rectangles:

void ContextAddRoundedRect(CGContextRef ctx, CGRect rect, CGFloat radius) {
    CGFloat minX = CGRectGetMinX(rect);
    CGFloat maxX = CGRectGetMaxX(rect);
    CGFloat minY = CGRectGetMinY(rect);
    CGFloat maxY = CGRectGetMaxY(rect);

    CGContextMoveToPoint(ctx, minX + radius, minY);
    CGContextAddArcToPoint(ctx, maxX, minY, maxX, minY + radius, radius);
    CGContextAddArcToPoint(ctx, maxX, maxY, maxX - radius, maxY, radius);
    CGContextAddArcToPoint(ctx, minX, maxY, minX, maxY - radius, radius);
    CGContextAddArcToPoint(ctx, minX, minY, minX + radius, minY, radius);
}

how can I figure out if a given CGPoint is inside my rect, considering the rounded corners?

解决方案

Instead of building it directly into the CGContextRef like this, you can build it instead as a UIBezierPath, and then use -[UIBezierPath containsPoint:].

When you're ready to actually render your path into the context, you can do so via:

CGContextAddPath(ctx, [myBezierPath CGPath]);

这篇关于点内圆角矩形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 12:48