回答
使用此为您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);
}
感谢您的代码,我需要修改高度和宽度,但它完美的作品。 –
不要谢谢我...谢谢PaintCode;) –
;)所以,实际上我不得不接受@ Cyrille的回答,但我不能:) –
内部阴影很难用CoreGraphics - 基本上,你需要否定你的路径,并在它下面画一个阴影,剪切到你原来的路径。
你可以看看PaintCode,它会显示你的代码。如果您不想购买它,它有15分钟的演示模式,这应该足以满足您的需求。
谢谢,我会尽快检查它。 –
这个应用程序非常好,我认为它会一直帮助我进一步绘制问题;) –
你可以试试这个:
#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;
}
我认为这会对您有所帮助。
这里是此代码的结果http://d.pr/i/q0rl。我创建了一个新的View(带有.h和.m文件),并使用上面的代码修改了.m文件。在此之后,我将视图添加到了我的ViewController中,并设置了之前创建的View类。 –
是的。我认为那是你需要的图表。你有什么问题吗? –
尝试下面的代码,其中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.-
第3行和第4行给出以下错误。 'UIView'没有可见的@interface声明选择器'setShadowColor:' –
iam对不起,请参阅我的编辑 –
- 1. 如何在透明矩形上绘制阴影?
- 2. 带圆角矩形和阴影的UIView:阴影出现在矩形上方
- 3. 绘制阴影
- 4. 在iOS中绘制矩形
- 5. iOS:CALayer阴影只有矩形?
- 6. CSS绘制形状与阴影
- 7. 透明形状的绘制阴影
- 8. EXC_BAD_ACCES绘制阴影
- 9. ios绘制右上角的矩形
- 10. 在UIImage上绘制边框和阴影
- 11. 在JPanel上绘制矩形
- 12. 在ImageVIew上绘制矩形
- 13. 在图形对象上绘制没有过滤器的阴影
- 14. 在Iphone上绘制超出C唇形区域的阴影
- 15. 在DrawingContext上绘图时在图像上绘制阴影
- 16. 在矩形上绘制圆形
- 17. 在圆形公式上绘制矩形
- 18. 用阴影色绘制UIlabel
- 19. 围绕NSImageView绘制阴影
- 20. 绘制阴影的UITableViewCell
- 21. 使用funcanimation绘制阴影
- 22. 绘制阴影难度
- 23. 绘制矩形上Mapbox GL
- 24. 在Mac上的JFrame上绘制矩形
- 25. 阴影设置(矩形:)当视图重绘
- 26. 绘制矩形
- 27. 在图框上绘制矩形 - 如何限制矩形区域?
- 28. 在矩形内绘制等距矩形
- 29. 绘制一个圆形阴影的方形元素与CSS
- 30. UIView最简单的(矩形)阴影
你想要的图像或查看? –
我不确定哪个适合我。我会在上面贴上标签,所以我可能会认为我认为。 –
通过CoreGraphics的代码绘制它。它更加灵活 - 您可以随意更改颜色,阴影半径和偏移量,而且不需要单独的1x和2x版本。 – Cyrille