objective-c - 向圆形 View 添加阴影

标签 objective-c uiview

我试图在圆形 View 后面添加一个阴影,但我的尝试导致我只在 View 边框上添加了一个阴影——而且这个阴影也没有出现在 View 周围(只是顶部)。这是我的代码:

-(void)drawRect:(CGRect)dirtyRect{
    CGContextRef ctx=UIGraphicsGetCurrentContext();
    CGRect bounds=[self bounds];

    // Figure out the centre of the bounds rectangle
    CGPoint centre;
    centre.x=bounds.origin.x+0.5*bounds.size.width;
    centre.y=bounds.origin.y+0.5*bounds.size.height;

    // Clip context
    CGPathRef path = CGPathCreateWithEllipseInRect(bounds, NULL);
    CGContextAddPath(ctx, path);
    CGContextClip(ctx);

    // Add black outline
    path = CGPathCreateWithEllipseInRect(bounds, NULL);
    CGContextAddPath(ctx, path);
    [[UIColor blackColor] setStroke];
    CGContextSetLineWidth(ctx, 3.0);

    // Specify shadow
    CGSize offset=CGSizeMake(1,4);
    CGColorRef colour=[[UIColor darkGrayColor] CGColor];
    CGContextSetShadowWithColor(ctx, offset, 2, colour);

    // Draw image
    UIImage *littleImage=[UIImage imageNamed:@"image.png"];
    [littleImage drawInRect:bounds];

    CGContextStrokePath(ctx);

}

感谢阅读。

最佳答案

我认为你正在剪掉阴影。尝试使剪切路径矩形大一点或椭圆矩形小一点。

您可以通过关闭剪裁来测试这一点,看看是否出现阴影。

关于objective-c - 向圆形 View 添加阴影,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14271619/

相关文章:

ios - 在应用速率大于 2 倍时从 avplayer 播放生涩

ios - 如何在 swift 中按下按钮时从另一个 xib 文件加载另一个 xib 文件

java - 在 Java 和 iOS 中的位置

ios - 如何在 ARC iOS 应用程序中使用非 ARC 框架

ios - 保存 UIView 时不断出现空白图像...如何将 UIView 和 subview 保存到相机胶卷上?

iphone - iOS - UIScrollView 不更新其内容,一些 UIViews 在重新加载后消失

ios - UITableViewCell 详细文本标签布局不当

swift - 添加 subview 并使用自动布局将前缘与 super View 和环绕 View 对齐

ios - 上传图片到 Firebase

IOS : UITableView is showing data when i tap on it