gpt4 book ai didi

ios - drawRect :(CGrect)rect, 目标中不显示阴影

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:43:54 25 4
gpt4 key购买 nike

我的目标是1.为我的 View 添加渐变(完成)2. 为我的 View 的底部边缘添加阴影(在这里发布)我正在做的是:

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

}

但是,我得到的是下面完全没有阴影的图片

我是否在正确的轨道上,但中间遗漏了什么?如果您有任何想法,请帮助.. enter image description here

最佳答案

您需要在绘制任何应该有阴影的东西之前设置阴影属性。在调用 CGContextStrokeRect 之前调用 CGContextSetShadowWithColor

此外,偏移量可能不会像您所做的那样大。阴影偏移量控制阴影与使用该阴影绘制的像素的偏移量,因此除非您希望阴影实际上从您的矩形开始非常远,否则您可能只需要一个像素左右的偏移量,如 CGSizeMake(0.0, 1.0).

关于ios - drawRect :(CGrect)rect, 目标中不显示阴影,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15820205/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com