Iphone 在iOS上绘制阴影矩形

Iphone 在iOS上绘制阴影矩形,iphone,ios,drawing,Iphone,Ios,Drawing,我试图用iOS中的库绘制如下图像;但我不能 我认为画画很容易,但我没能做到。 完成绘制后,我将在其上放置一个标签。使用CoreGraphics很难实现内部阴影——基本上,您需要对路径求反,并在其下方绘制一个阴影,并剪裁到原始路径 你可以看看,它会告诉你代码。如果您不想购买它,它有15分钟的演示模式,这应该足以满足您的需要。您可以尝试以下方法: #import <QuartzCore/QuartzCore.h> 这允许您在视图上具有圆角。如果计算半径以匹配视图,则应获得所需的外观

我试图用iOS中的库绘制如下图像;但我不能

我认为画画很容易,但我没能做到。
完成绘制后,我将在其上放置一个标签。

使用CoreGraphics很难实现内部阴影——基本上,您需要对路径求反,并在其下方绘制一个阴影,并剪裁到原始路径

你可以看看,它会告诉你代码。如果您不想购买它,它有15分钟的演示模式,这应该足以满足您的需要。

您可以尝试以下方法:

#import <QuartzCore/QuartzCore.h>
这允许您在视图上具有圆角。如果计算半径以匹配视图,则应获得所需的外观

    - (id)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self) {
         self.backgroundColor = [UIColor grayColor];
        }
        return self;
    }
 - (void)drawRect:(CGRect)rect
{ 
           CGContextRef context =UIGraphicsGetCurrentContext();
            CGContextSetRGBStrokeColor(context, 1.0, 1.0, 1.0, 1.0);
            // And draw with a blue fill color
            CGContextSetRGBFillColor(context, 0.0, 0.0, 1.0, 1.0);
            // Draw them with a 2.0 stroke width so they are a bit more visible.
            CGContextSetLineWidth(context, 2.0);


            CGContextAddRect(context, self.bounds);

            CGContextStrokePath(context);


            // Close the path
            CGContextClosePath(context);
            // Fill & stroke the path
            CGContextDrawPath(context, kCGPathFillStroke);
            self.layer.cornerRadius = self.bounds.size.width/12;
            self.layer.masksToBounds = TRUE;

}

我认为这将对您有所帮助。

尝试下面的代码,其中
myView
是您要设置阴影的
UIView

myView.layer.cornerRadius   =   5.0f;
myView.layer.masksToBounds  =   YES;
[myView.layer setShadowColor:[[UIColor blackColor] colorWithAlphaComponent: 0.5]];
[myView.layer setShadowOffset:CGSizeMake(0, -1)];

希望这有帮助。-

将此作为您的
drawRect
方法:

- (void)drawRect:(CGRect)rect

    //// General Declarations
    CGContextRef context = UIGraphicsGetCurrentContext();

    //// Shadow Declarations
    UIColor* shadow = [UIColor blackColor];
    CGSize shadowOffset = CGSizeMake(1, 1);
    CGFloat shadowBlurRadius = 2;

    //// Frames
    CGRect frame = rect;

    //// Abstracted Graphic Attributes
    CGRect shadowBoxRect = CGRectMake(CGRectGetMinX(frame) + 0, CGRectGetMinY(frame) + 0, 40, 40);
    CGFloat shadowBoxCornerRadius = 4;


    //// ShadowBox Drawing
    UIBezierPath* shadowBoxPath = [UIBezierPath bezierPathWithRoundedRect: shadowBoxRect cornerRadius: shadowBoxCornerRadius];
    [[UIColor lightGrayColor] setFill];
    [shadowBoxPath fill];

    ////// ShadowBox Inner Shadow
    CGRect shadowBoxBorderRect = CGRectInset([shadowBoxPath bounds], -shadowBlurRadius, -shadowBlurRadius);
    shadowBoxBorderRect = CGRectOffset(shadowBoxBorderRect, -shadowOffset.width, -shadowOffset.height);
    shadowBoxBorderRect = CGRectInset(CGRectUnion(shadowBoxBorderRect, [shadowBoxPath bounds]), -1, -1);

    UIBezierPath* shadowBoxNegativePath = [UIBezierPath bezierPathWithRect: shadowBoxBorderRect];
    [shadowBoxNegativePath appendPath: shadowBoxPath];
    shadowBoxNegativePath.usesEvenOddFillRule = YES;

    CGContextSaveGState(context);
    {
        CGFloat xOffset = shadowOffset.width + round(shadowBoxBorderRect.size.width);
        CGFloat yOffset = shadowOffset.height;
        CGContextSetShadowWithColor(context,
            CGSizeMake(xOffset + copysign(0.1, xOffset), yOffset + copysign(0.1, yOffset)),
            shadowBlurRadius,
            shadow.CGColor);

        [shadowBoxPath addClip];
        CGAffineTransform transform = CGAffineTransformMakeTranslation(-round(shadowBoxBorderRect.size.width), 0);
        [shadowBoxNegativePath applyTransform: transform];
        [[UIColor grayColor] setFill];
        [shadowBoxNegativePath fill];
    }
    CGContextRestoreGState(context);

}

我不确定哪个适合我。我会在上面贴一个标签,这样我就可以看到我想的视图了。用CoreGraphics的代码画出来。它更灵活--你可以随意改变颜色、阴影半径、偏移量,而且你不需要单独的1x和2x版本。PaintCode demo能帮我吗?我需要一个教程,因为我对CoreGraphics知之甚少;)第3行和第4行给出以下错误。“UIView”没有可见的@interface声明选择器“setShadowColor:”这是此代码的结果。我创建了一个新视图(带有.h和.m文件),并用上面的代码修改了.m文件。之后,我向ViewController中插入了一个视图,并设置了我之前创建的视图类。是。我想这是你需要的图表。你有什么问题吗?这个应用非常好,我想它会帮助我解决更多的绘图问题;)感谢这段代码,我需要修改高度和宽度,但它工作得很好。;)所以,实际上我不得不接受@Cyrille的回答,但我不能:)
- (void)drawRect:(CGRect)rect

    //// General Declarations
    CGContextRef context = UIGraphicsGetCurrentContext();

    //// Shadow Declarations
    UIColor* shadow = [UIColor blackColor];
    CGSize shadowOffset = CGSizeMake(1, 1);
    CGFloat shadowBlurRadius = 2;

    //// Frames
    CGRect frame = rect;

    //// Abstracted Graphic Attributes
    CGRect shadowBoxRect = CGRectMake(CGRectGetMinX(frame) + 0, CGRectGetMinY(frame) + 0, 40, 40);
    CGFloat shadowBoxCornerRadius = 4;


    //// ShadowBox Drawing
    UIBezierPath* shadowBoxPath = [UIBezierPath bezierPathWithRoundedRect: shadowBoxRect cornerRadius: shadowBoxCornerRadius];
    [[UIColor lightGrayColor] setFill];
    [shadowBoxPath fill];

    ////// ShadowBox Inner Shadow
    CGRect shadowBoxBorderRect = CGRectInset([shadowBoxPath bounds], -shadowBlurRadius, -shadowBlurRadius);
    shadowBoxBorderRect = CGRectOffset(shadowBoxBorderRect, -shadowOffset.width, -shadowOffset.height);
    shadowBoxBorderRect = CGRectInset(CGRectUnion(shadowBoxBorderRect, [shadowBoxPath bounds]), -1, -1);

    UIBezierPath* shadowBoxNegativePath = [UIBezierPath bezierPathWithRect: shadowBoxBorderRect];
    [shadowBoxNegativePath appendPath: shadowBoxPath];
    shadowBoxNegativePath.usesEvenOddFillRule = YES;

    CGContextSaveGState(context);
    {
        CGFloat xOffset = shadowOffset.width + round(shadowBoxBorderRect.size.width);
        CGFloat yOffset = shadowOffset.height;
        CGContextSetShadowWithColor(context,
            CGSizeMake(xOffset + copysign(0.1, xOffset), yOffset + copysign(0.1, yOffset)),
            shadowBlurRadius,
            shadow.CGColor);

        [shadowBoxPath addClip];
        CGAffineTransform transform = CGAffineTransformMakeTranslation(-round(shadowBoxBorderRect.size.width), 0);
        [shadowBoxNegativePath applyTransform: transform];
        [[UIColor grayColor] setFill];
        [shadowBoxNegativePath fill];
    }
    CGContextRestoreGState(context);

}