2012-09-03 149 views
4

我想在iOS中使用库绘制类似下面的图像;但我不能。在iOS上绘制阴影矩形

enter image description here

我认为这是很容易得出,但我无法实现的。 我完成绘制后,我会在它上面贴上标签。

+0

你想要的图像或查看? –

+0

我不确定哪个适合我。我会在上面贴上标签,所以我可能会认为我认为。 –

+0

通过CoreGraphics的代码绘制它。它更加灵活 - 您可以随意更改颜色,阴影半径和偏移量,而且不需要单独的1x和2x版本。 – Cyrille

回答

3

使用此为您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); 

} 
+0

感谢您的代码,我需要修改高度和宽度,但它完美的作品。 –

+1

不要谢谢我...谢谢PaintCode;) –

+0

;)所以,实际上我不得不接受@ Cyrille的回答,但我不能:) –

1

内部阴影很难用CoreGraphics - 基本上,你需要否定你的路径,并在它下面画一个阴影,剪切到你原来的路径。

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

+0

谢谢,我会尽快检查它。 –

+0

这个应用程序非常好,我认为它会一直帮助我进一步绘制问题;) –

1

你可以试试这个:

#import <QuartzCore/QuartzCore.h> 

,并在你的代码,使后您的视图设置这些:

self.layer.cornerRadius = x; 

self.layer.masksToBounds = TRUE; 

这可以让你有你的观点圆角。如果您计算半径以匹配您的视图,则应该获得所需的外观。

- (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; 

} 

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

+0

这里是此代码的结果http://d.pr/i/q0rl。我创建了一个新的View(带有.h和.m文件),并使用上面的代码修改了.m文件。在此之后,我将视图添加到了我的ViewController中,并设置了之前创建的View类。 –

+0

是的。我认为那是你需要的图表。你有什么问题吗? –

0

尝试下面的代码,其中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)]; 

希望这helps.-

+0

第3行和第4行给出以下错误。 'UIView'没有可见的@interface声明选择器'setShadowColor:' –

+0

iam对不起,请参阅我的编辑 –