0

このコードで長方形を作成しましたが、動作します:

- (void)drawRect:(CGRect)rect{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextAddRect(context, CGRectMake(60, 60, 100, 1));
    CGContextStrokePath(context);
}

しかし、今は影をつけたいので、これを試しました:

NSShadow* theShadow = [[NSShadow alloc] init];
[theShadow setShadowOffset:NSMakeSize(10.0, -10.0)];
[theShadow setShadowBlurRadius:4.0];

しかし、xcodeは私に教えてください NSMakeSize : Sending 'int' to parameter of incompatible type 'CGSize'

影について正しい形はどれですか? ありがとう!!

4

1 に答える 1

2

CGContextSetShadow(...)影が必要なオブジェクトを描画する関数の前に関数を呼び出す必要があります。完全なコードは次のとおりです。

- (void)drawRect:(CGRect)rect {
    // define constants
    const CGFloat shadowBlur = 5.0f;
    const CGSize shadowOffset = CGSizeMake(10.0f, 10.0f);

    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetRGBStrokeColor(context, 1, 0, 0, 1);

    // Setup shadow parameters. Everithyng you draw after this line will be with shadow
    // To turn shadow off invoke CGContextSetShadowWithColor(...) with NULL for CGColorRef parameter.
    CGContextSetShadow(context, shadowOffset, shadowBlur);

    CGRect rectForShadow = CGRectMake(rect.origin.x, rect.origin.y, rect.size.width - shadowOffset.width - shadowBlur, rect.size.height - shadowOffset.height - shadowBlur);
    CGContextAddRect(context, rectForShadow);
    CGContextStrokePath(context);
}

備考:

にいくつかのランダムな値を指定していることに気付きましたCGContextAddRect(context, CGRectMake(60, 60, 100, 1));。パラメータを介して受け取った長方形内にのみ描画する必要がありrectます。

于 2014-07-09T01:16:21.530 に答える