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 回答