ios - 显示 CGRect 边框的简单方法?

标签 ios cocos2d-iphone cgrect

我试图找到一种方法来在我的 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 是有问题的 rect。

最佳答案

这可以在 drawRect 中处理:

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

关于ios - 显示 CGRect 边框的简单方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7730417/

相关文章:

iphone - 如何将图像创建为当前场景的较小比例?

iphone - 创建炸弹 - Cocos2d/Box2d

c - 将一个矩形拆分成大小相等的矩形?

ios - 添加 UIImage 或 UILabel 到 navigationItems

ios - Core Audio的渲染回调不会更改输出音频

ios - 下载数据后 TableView 未更新

iphone - 具有透明背景的 OpenGL ES 2.0 GLKit

ios - 如何在cocos2d中针对特定场景实现手势

iphone - 如何移动 Interface Builder 放置的文本字段和按钮

swift - UILabel 获取框架的宽度或高度