Is Point Inside Rounded Rectangle?

流过昼夜 提交于 2019-12-13 02:58:45

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/14298714/is-point-inside-rounded-rectangle

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!