被点内圆角矩形?(Is Point Inside Rounded Rectangle?)

2019-10-17 14:31发布

我有这样的代码来绘制圆角矩形:

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);
}

我怎么能计算出,如果给定的CGPoint是我的矩形内, 考虑到圆角?

Answer 1:

除了直接建立入的CGContextRef这样,你可以改为打造为UIBezierPath ,然后用-[UIBezierPath containsPoint:]

当你准备好实际呈现的路径进入的背景下,你可以通过这样做:

CGContextAddPath(ctx, [myBezierPath CGPath]);


文章来源: Is Point Inside Rounded Rectangle?