2

CGRectデバッグ目的で、iOS プログラムで一部の の境界線を表示する方法を見つけようとしています。これを行うかなり簡単な方法はありますか?プログラムがこれらの長方形を作成している場所を確認する必要があるだけなので、奇妙なタッチ動作 (またはその欠如) を追跡できます。

私のクラス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

この方法のような簡単なことをここで試すことができます。これは、UIView や CLayer とは対照的に、実際の CGRect 構造を使用します。

-(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 に答える