简体   繁体   English

在drawRect:(CGrect)rect,objective中不显示阴影

[英]shadow does not show in drawRect:(CGrect)rect,objective

My goal is 1. add gradient for my view (done) 2. add drop shadow for the bottom edge of my view ( issue at here ) What I am doing is : 我的目标是1.为我的视图添加渐变(完成)2.为我的视图底部添加阴影(此处为问题)我正在做的是:

- (void)drawRect:(CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext();

    UIColor *whiteColor         =   [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0];
    UIColor *lightGrayColor     =   [UIColor colorWithRed:230.0/255.0 green:230.0/255.0 blue:230.0/255.0 alpha:1.0];

    CGRect paperRect            =   self.bounds;

    // Fill with gradient
    [self drawLinearGradient:context for:paperRect start:whiteColor.CGColor end:lightGrayColor.CGColor];

    CGContextSetStrokeColorWithColor(context, [UIColor lightGrayColor].CGColor);
    CGContextSetLineWidth(context, .9);
    CGContextStrokeRect(context, paperRect);

    // Add shadow
    CGContextSetShadowWithColor(context, CGSizeMake(0, self.frame.size.height), 9.0, [UIColor blueColor].CGColor);

}
-(void)drawLinearGradient:(CGContextRef)context for:(CGRect)rect start:(CGColorRef)startColor end:(CGColorRef)endColor {
    CGColorSpaceRef colorSpace  =   CGColorSpaceCreateDeviceRGB();
    CGFloat locations[]         =   { 0.0, 1.0 };

    NSArray *colors             =   [NSArray arrayWithObjects:(__bridge id)startColor, (__bridge id)endColor, nil];

    CGGradientRef gradient      =   CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef) colors, locations);

    CGPoint startPoint          =   CGPointMake(CGRectGetMidX(rect), CGRectGetMinY(rect));
    CGPoint endPoint            =   CGPointMake(CGRectGetMidX(rect), CGRectGetMaxY(rect));

    CGContextSaveGState(context);
    CGContextAddRect(context, rect);
    CGContextClip(context);
    CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, 0);
    CGContextRestoreGState(context);

    CGGradientRelease(gradient);
    CGColorSpaceRelease(colorSpace);

}

However, what I am getting is the picture below without shadow at all 但是,我得到的是下面完全没有阴影的图片

Am I on the right track but missing something in the middle ? 我是在正确的轨道上但中间缺少什么吗? Please help if you have any ideas.. 如果您有任何想法请帮助。 在此处输入图片说明

You need to set the shadow properties before drawing whatever it is that's supposed to have the shadow. 您需要先设置阴影属性, 然后再绘制应该具有阴影的任何内容。 Put your call to CGContextSetShadowWithColor before the call to CGContextStrokeRect . 把你的调用CGContextSetShadowWithColor调用之前CGContextStrokeRect

Also, the offset should probably not be as large as you're making it. 另外,偏移量可能不应该像您制作的那么大。 The shadow offset controls how much the shadow is offset from the pixels that were drawn with that shadow, so unless you want the shadow to actually start very far away from your rect you probably want an offset of just a pixel or so, like CGSizeMake(0.0, 1.0) . 阴影偏移量控制阴影与用该阴影绘制的像素之间的偏移量,因此,除非您希望阴影实际上从矩形开始很远,否则您可能只希望偏移一个像素左右,例如CGSizeMake(0.0, 1.0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 覆盖drawRect:(CGRect)rect在Objective-C(iOS)中吗? - Overriding drawRect:(CGRect)rect in Objective-C (iOS)? 如何启动函数 drawRect(rect: CGRect) ? 迅速 - How to start function drawRect(rect: CGRect) ? Swift 如何调用 -(void)drawRect:(CGRect)rect - How to call -(void)drawRect:(CGRect)rect - (void)drawRect:(CGRect)rect; 正在耗尽几乎所有的iPhone CPU - -(void) drawRect:(CGRect)rect; is using up nearly all of iPhone CPU 不使用-(void)drawRect:(CGRect)rect方法绘制线吗? - Draw lines without the -(void)drawRect:(CGRect)rect method? -(void)drawRect:(CGRect)rect,绘制一个迷宫并检测其边界 - - (void)drawRect:(CGRect)rect , draw a maze and detect its boundary 为什么drawRect:在没有调用[super drawrect:rect]的情况下工作? - Why does drawRect: work without calling [super drawrect:rect]? drawRect:自动检查是否有东西在CGRect传递给它的范围内吗? - Does drawRect: automatically check whether something is within the bounds of the CGRect passed to it? 具有透明内部矩形目标的 UIBezierPath 阴影 c - UIBezierPath Shadow with transparent internal rect Objective c 在iOS上,如果drawRect不使用传入的rect,[self setNeedsDisplayInRect:rect]传入rect的关系是否重要? - On iOS, if drawRect doesn't use the rect that was passed in, does it matter if [self setNeedsDisplayInRect:rect] passes in a rect?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM