显示 CGRect 边框的简单方法?

发布于 2024-12-09 04:37:06 字数 1297 浏览 0 评论 0原文

我正在尝试找到一种方法来显示 iOS 程序中某些 CGRect 的边框,以进行调试。有没有一个相当简单的方法来做到这一点?我只需要查看程序在哪里创建这些矩形,这样我就可以追踪一些奇怪的触摸行为(或缺乏)。

我的类 init 方法:

// Initialize with points and a line number, then draw a rectangle 
// in the shape of the line
-(id)initWithPoint:(CGPoint)sP :(int)w :(int)h :(int)lN :(int)t {
    if ((self = [super init])) {
        startP = sP;
        lineNum = lN;
        width = w;
        height = h;
        int type = t;
        self.gameObjectType = kPathType;

        // Draw the path sprite
        path = [CCSprite spriteWithFile: @"line.png" rect:CGRectMake(0, 0, 5, height)];
        ccTexParams params = {GL_LINEAR,GL_LINEAR,GL_REPEAT,GL_REPEAT};
        [path.texture setTexParameters:&params];

        if(type == 1) {
            path.position = ccp(startP.x, startP.y);
        } else {
            path.rotation = 90;
            path.anchorPoint = ccp(0, 0);
            path.position = ccp(startP.x, startP.y-2);
        }

        [self addChild:path];

        // Draw the "bounding" box
        pathBox = CGRectMake(path.position.x - (path.contentSize.width/2), path.position.y - (path.contentSize.height/2), path.contentSize.width * 10, path.contentSize.height);
    }
    return self;
}

pathBox 是有问题的矩形。

I'm trying to find a way to display the borders of some CGRects in my iOS program for debugging purposes. Is there a fairly simple way to do this? I just need to see where the program is creating these rectangles, so I can track down some odd touch behaviors (or lack thereof).

My class init method:

// Initialize with points and a line number, then draw a rectangle 
// in the shape of the line
-(id)initWithPoint:(CGPoint)sP :(int)w :(int)h :(int)lN :(int)t {
    if ((self = [super init])) {
        startP = sP;
        lineNum = lN;
        width = w;
        height = h;
        int type = t;
        self.gameObjectType = kPathType;

        // Draw the path sprite
        path = [CCSprite spriteWithFile: @"line.png" rect:CGRectMake(0, 0, 5, height)];
        ccTexParams params = {GL_LINEAR,GL_LINEAR,GL_REPEAT,GL_REPEAT};
        [path.texture setTexParameters:¶ms];

        if(type == 1) {
            path.position = ccp(startP.x, startP.y);
        } else {
            path.rotation = 90;
            path.anchorPoint = ccp(0, 0);
            path.position = ccp(startP.x, startP.y-2);
        }

        [self addChild:path];

        // Draw the "bounding" box
        pathBox = CGRectMake(path.position.x - (path.contentSize.width/2), path.position.y - (path.contentSize.height/2), path.contentSize.width * 10, path.contentSize.height);
    }
    return self;
}

pathBox is the rect in question.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

傾旎 2024-12-16 04:37:06

这可以在drawRect中处理:

- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGRect aRect=[myPath bounds];
    CGContextSetLineWidth(context, 2);
    CGContextSetStrokeColorWithColor(context, [UIColor blueColor ].CGColor);
    CGContextStrokeRect(context, aRect);
}

This can be handled within drawRect:

- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGRect aRect=[myPath bounds];
    CGContextSetLineWidth(context, 2);
    CGContextSetStrokeColorWithColor(context, [UIColor blueColor ].CGColor);
    CGContextStrokeRect(context, aRect);
}
何以心动 2024-12-16 04:37:06

我将尝试假设这是一个 iOS 项目,因为这就是我所知道的。

如果这些矩形用于 UIView 或 CALayer,那么您可以为它们设置边框。

在文件中添加 #import 并使用 view.layer.borderColorview.layer.borderWidth 添加内容你想要的。

如果它只是一个图层,请删除其中的 view 部分。

I'm going to take a stab and assume this is an iOS project, since that's what I know.

If these rectangles are being used for UIView or a CALayer then you can set the border for them.

Add #import <QuartzCore/QuartzCore.h> in your file and use view.layer.borderColor, view.layer.borderWidth add what you want.

If it's just a layer remove the view part of it. 

剪不断理还乱 2024-12-16 04:37:06

我或多或少想出了如何做到这一点:只是在我的类中扩展了绘制方法,如下所示:

-(void) draw {
    glColor4f(0, 1.0, 0, 1.0);
    glLineWidth(2.0f);
    [super draw];

    CGRect pathBox = CGRectMake(path.position.x - (path.contentSize.width/2), path.position.y - (path.contentSize.height/2), path.contentSize.width * 10, path.contentSize.height);
    CGPoint verts[4] = {
      ccp(pathBox.origin.x, pathBox.origin.y),
      ccp(pathBox.origin.x + pathBox.size.width, pathBox.origin.y),
      ccp(pathBox.origin.x + pathBox.size.width, pathBox.origin.y + pathBox.size.height),
      ccp(pathBox.origin.x, pathBox.origin.y + pathBox.size.height)
    };

    ccDrawPoly(verts, 4, YES);
}

感谢 Cocos2D 站点上的 Blue Ether 的提醒:
http://www.cocos2d-iphone.org/forum /topic/21718?replies=5#post-120691

I figured out more-or-less how to do it: just extended the draw method in my class like so:

-(void) draw {
    glColor4f(0, 1.0, 0, 1.0);
    glLineWidth(2.0f);
    [super draw];

    CGRect pathBox = CGRectMake(path.position.x - (path.contentSize.width/2), path.position.y - (path.contentSize.height/2), path.contentSize.width * 10, path.contentSize.height);
    CGPoint verts[4] = {
      ccp(pathBox.origin.x, pathBox.origin.y),
      ccp(pathBox.origin.x + pathBox.size.width, pathBox.origin.y),
      ccp(pathBox.origin.x + pathBox.size.width, pathBox.origin.y + pathBox.size.height),
      ccp(pathBox.origin.x, pathBox.origin.y + pathBox.size.height)
    };

    ccDrawPoly(verts, 4, YES);
}

Thanks to Blue Ether over at the Cocos2D site for the heads-up:
http://www.cocos2d-iphone.org/forum/topic/21718?replies=5#post-120691

乞讨 2024-12-16 04:37:06

您可以尝试一些简单的方法,例如这里的方法。它使用实际的 CGRect 结构,而不是 UIView 和 CLayer。

-(void) drawBorderForRect:(CGRect)rect usingColor:(UIColor*)uiColor{

    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetStrokeColorWithColor(context, uiColor.CGColor);
    CGContextSetLineWidth(context, 1.0f);

    CGFloat x = rect.origin.x;
    CGFloat y = rect.origin.y;
    CGFloat width = abs(rect.size.width);
    CGFloat height = abs(rect.size.height);

    // ---
    CGContextMoveToPoint(context, x, y);
    CGContextAddLineToPoint(context, x + width, y);

    // |
    CGContextMoveToPoint(context, x + width, y);
    CGContextAddLineToPoint(context, x  + width, y - height);

    // ---
    CGContextMoveToPoint(context, x  + width, y - height);
    CGContextAddLineToPoint(context, x - width, y - height);

    // |
    CGContextMoveToPoint(context, x, y - height);
    CGContextAddLineToPoint(context, x, y);

    CGContextStrokePath(context);
}

You can try something simple like this method here. This uses an actual CGRect structure as oppose to a UIView and CLayer.

-(void) drawBorderForRect:(CGRect)rect usingColor:(UIColor*)uiColor{

    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetStrokeColorWithColor(context, uiColor.CGColor);
    CGContextSetLineWidth(context, 1.0f);

    CGFloat x = rect.origin.x;
    CGFloat y = rect.origin.y;
    CGFloat width = abs(rect.size.width);
    CGFloat height = abs(rect.size.height);

    // ---
    CGContextMoveToPoint(context, x, y);
    CGContextAddLineToPoint(context, x + width, y);

    // |
    CGContextMoveToPoint(context, x + width, y);
    CGContextAddLineToPoint(context, x  + width, y - height);

    // ---
    CGContextMoveToPoint(context, x  + width, y - height);
    CGContextAddLineToPoint(context, x - width, y - height);

    // |
    CGContextMoveToPoint(context, x, y - height);
    CGContextAddLineToPoint(context, x, y);

    CGContextStrokePath(context);
}
我为君王 2024-12-16 04:37:06

很快,这很容易。您可以从 CGRect 构造 BezierPath:

let dp = UIBezierPath.init(rect: myRect)
dp.stroke()

In swift, it's easy. You can construct a BezierPath from a CGRect:

let dp = UIBezierPath.init(rect: myRect)
dp.stroke()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文