2

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

我的上课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是有问题的矩形。

4

5 回答 5

5

这可以在 drawRect 中处理:

- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGRect aRect=[myPath bounds];
    CGContextSetLineWidth(context, 2);
    CGContextSetStrokeColorWithColor(context, [UIColor blueColor ].CGColor);
    CGContextStrokeRect(context, aRect);
}
于 2012-10-15T14:11:30.707 回答
2

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

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

添加#import <QuartzCore/QuartzCore.h>你的文件并使用view.layer.borderColorview.layer.borderWidth添加你想要的。

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

于 2011-10-11T18:44:02.833 回答
2

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

-(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

于 2011-10-11T19:52:40.353 回答
0

您可以在此处尝试类似此方法的简单方法。这使用了一个实际的 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);
}
于 2013-05-29T22:43:53.100 回答
0

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

let dp = UIBezierPath.init(rect: myRect)
dp.stroke()
于 2016-02-28T14:42:55.737 回答