2011-10-11 59 views
2

我试图找到一种方法来显示我的iOS程序中某些CGRect的边框以进行调试。有没有一个相当简单的方法来做到这一点?我只需要看看程序创建这些矩形的位置,这样我就可以追踪到一些奇怪的触摸行为(或者缺乏)。显示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是有问题的矩形。

+0

这不是一个Objective-C的问题 – Nektarios

回答

2

我想通了,更多或更少如何做到这一点:刚刚扩展,像这样在我班上的绘制方法:

-(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的网站抬头: http://www.cocos2d-iphone.org/forum/topic/21718?replies=5#post-120691

2

我将采取刺,并认为这是一个iOS项目,因为这就是我所知道的。

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

在您的文件中添加#import <QuartzCore/QuartzCore.h>并使用view.layer.borderColor,view.layer.borderWidth添加你想要的。

如果只是一层删除了view的一部分。

+0

对不起,我应该已经指定,它是使用了cocos2d的iOS –

5

这可以内的drawRect处理:

- (void)drawRect:(CGRect)rect 
{ 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGRect aRect=[myPath bounds]; 
    CGContextSetLineWidth(context, 2); 
    CGContextSetStrokeColorWithColor(context, [UIColor blueColor ].CGColor); 
    CGContextStrokeRect(context, aRect); 
} 
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); 
} 
0

在swift中,这很容易。您可以从一个的CGRect构造BezierPath:

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