回答
使用此爲您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