xcode 在 UIView 周围创建发光

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5291546/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 20:28:50  来源:igfitidea点击:

Create a glow around a UIView

iphoneobjective-cxcodeios4quartz-graphics

提问by max_

I would like to draw a glow-ish border around a UIView which is roughly 5px from the actual UIView itself.

我想在 UIView 周围绘制一个发光的边框,它距离实际 UIView 本身大约 5px。

Please could you tell me how I could achieve this?

请你能告诉我我是如何做到这一点的吗?

回答by e.James

Probably the easiest way is to create a shadow, but use a light color instead of a dark one. Shadow details can be found here: How do I draw a shadow under a UIView?and here.

可能最简单的方法是创建阴影,但使用浅色而不是深色。阴影细节可以在这里找到:如何在 UIView 下绘制阴影?这里

Something like this should get the ball rolling:

像这样的事情应该让球滚动:

- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSaveGState(context);
    CGContextSetShadowWithColor(context, CGSizeMake(0, 0), 10,
                                [UIColor whiteColor].CGColor);
    [super drawRect:rect];
    CGContextRestoreGState(context);
}

Update:I just tried this out. You will have to use this code on the superview of the glowing view for it to work properly.

更新:我刚刚试过这个。您必须在发光视图的超级视图上使用此代码才能正常工作。

回答by Gregorius T

The first thing I would try is to embed the UIView within a UIView which has the glow image. If the glow effect is just an image, then you create a UIView containing the glow image that is 10 px taller and wider than the UIView being surrounded. This will allow for 5 px of extension on all 4 sides. You can do all this quickly and easily using Interface Builder.

我会尝试的第一件事是将 UIView 嵌入到具有发光图像的 UIView 中。如果发光效果只是一个图像,那么您创建一个 UIView 包含比被包围的 UIView 高和宽 10 像素的发光图像。这将允许在所有 4 个边上扩展 5 像素。您可以使用 Interface Builder 快速轻松地完成所有这些工作。

If you want the glow effect to look really cool, considering creating a collection of glow images that when viewed as a sequence will show sort of a moving glow effect. You can then use this collection of images in the UIView and turn on animation. All UIView controls have animation support built in.

如果您希望辉光效果看起来非常酷,可以考虑创建一组辉光图像,当这些图像作为一个序列查看时,会显示出某种移动的辉光效果。然后,您可以在 UIView 中使用此图像集合并打开动画。所有 UIView 控件都内置了动画支持。

Hope this helped. Good Luck.

希望这有帮助。祝你好运。