简体   繁体   English

圆形 UIVisualEffectView

[英]Round UIVisualEffectView

I have a map.我有地图。 On the map, I'd like to draw small, blurred circle.在地图上,我想画一个模糊的小圆圈。 I've implemented something like this:我已经实现了这样的事情:

UIVisualEffect *visualEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
self.visualEffectView = [[UIVisualEffectView alloc] initWithEffect:visualEffect];
[self addSubview:self.visualEffectView];

and then in layoutSubviews :然后在layoutSubviews

[self.visualEffectView setFrame:CGRectMake(0.f, 0.f, 20.f, 20.f];

Now the problem is making this view round.现在的问题是让这个观点变得圆润。 I've tried:我试过了:

[self.visualEffectView.layer setCornerRadius:10.f];

However nothing happens.然而什么也没有发生。 Another try was with (basing on SOF question ):另一个尝试是(基于SOF 问题):

CAShapeLayer *mask = [CAShapeLayer layer];
mask.path = [UIBezierPath bezierPathWithOvalInRect:self.visualEffectView.bounds].CGPath;
self.visualEffectView.layer.mask = mask;

But in this case, the visualEffectView is round but doesn't blur :/.但在这种情况下, visualEffectView是圆形但不模糊:/。 Is there any way to make it working?有没有办法让它工作?

BTW: I've tried FXBlurView , however it works very slowly, I can't accept my app to load only map + blur for ~1 minute on iPhone 5.顺便说一句:我试过FXBlurView ,但是它工作得非常慢,我不能接受我的应用程序在 iPhone 5 上只加载地图 + 模糊约 1 分钟。

Tryout:试用:

self.visualEffectView.clipsToBounds = YES;

Put this after you set the cornerRadius.在设置cornerRadius后放置它。 This should be it.应该是这样。 You can leave out the BezierPath stuff.你可以省略 BezierPath 的东西。 Hope this helps :)希望这可以帮助 :)

EDIT :编辑

I just tried something similar in my own project.我只是在我自己的项目中尝试了类似的东西。 And a good way to keep the blur with round corners, is simply to put the visual effect view as a child of a new view with the same frame as your visual effect view.保持圆角模糊的一个好方法是将视觉效果视图作为与视觉效果视图具有相同框架的新视图的子视图。 You can now just set the corner radius of this new parent UIView object and set its clipsToBounds property to YES .您现在可以设置这个新父UIView对象的角半径并将其clipsToBounds属性设置为YES It then automatically gives its subviews the corner radius, as it clips to its bounds.然后它会自动为其子视图提供角半径,因为它会裁剪到其边界。

Give it a try, it works in my case.试一试,它适用于我的情况。

I figured this out if anyone wants to know how to do it, no code needed:如果有人想知道怎么做,我想出了这个,不需要代码:

  1. Create a view and set its background to "Clear Color" in the Attribute editor (it MUST be clear color not default.在属性编辑器中创建一个视图并将其背景设置为“清除颜色”(它必须是清除颜色而不是默认颜色。

  2. Drag another view on top of the first view, size it to the size you want your Visual Effect view to be, and in the Attribute editor set its background to "Clear Color", and check "Clip Subviews".将另一个视图拖到第一个视图的顶部,将其调整为您想要的视觉效果视图的大小,然后在属性编辑器中将其背景设置为“清除颜色”,然后选中“剪辑子视图”。

    Also on this view, go to the identity inspector and under "User Defined Runtime Attributes" add a new Key Path named "layer.cornerRadius", make it type "Number", and set its value to 9 or higher for a decent rounded edge.同样在这个视图上,转到身份检查器,在“用户定义的运行时属性”下添加一个名为“layer.cornerRadius”的新键路径,输入“数字”,并将其值设置为 9 或更高以获得合适的圆角边缘. (There's a bug in Xcode which will change the Key Path back to the default once you change the Type, if this happens, just be sure to go back and re-type in layer.cornerRadius). (Xcode 中有一个错误,一旦您更改了类型,就会将键路径更改回默认值,如果发生这种情况,请务必返回并在 layer.cornerRadius 中重新键入)。

  3. Drag a Visual Effects View with blur on top of the View in step 3.在步骤 3 中的视图顶部拖动一个带有模糊的视觉效果视图。

  4. Now run your program.现在运行你的程序。 You will have rounded edges, blur, and no artifacts.您将拥有圆润的边缘、模糊和无伪影。

Now, I created mine where it links using a segue, if you need this to work with a segue your segue has to be set to Modal and the presentation has to be set to OVER Full Screen (not just Full Screen).现在,我创建了我的使用 segue 链接的地方,如果您需要它与 segue 一起使用,您的 segue 必须设置为 Modal,并且演示文稿必须设置为 OVER 全屏(不仅仅是全屏)。

Here is a link to a project file that demonstrates it.这是演示它的项目文件的链接。 Note the hierarchy of the views in the second view controller: Project File on Dropbox注意第二个视图控制器中视图的层次结构: Dropbox 上的项目文件

EDIT: My picture disappeared so I readded it.编辑:我的照片消失了,所以我读了它。 例子

自从最初的问题以来,事情显然发生了变化,但我能够通过在 Visual Effect View 本身的“User Defined Runtime Attributes”中选择“Clip to Bounds”并设置“layer.cornerRadius”来实现这一点。

要绘制圆形,CornerRadius 在您的示例中必须等于 width/2,width = 30,然后 CornerRadius = 30/2 = 15;

This works for me这对我有用

@IBOutlet weak var blurView: UIVisualEffectView!

And in viewDidLoad()而在 viewDidLoad()

blurView.layer.cornerRadius = 10;
blurView.clipsToBounds  =  true

You can change the corner radius according to your preference.您可以根据自己的喜好更改拐角半径。

Well, so there is a possibility to apply rounded corners just like on normal UIView.嗯,所以有可能像在普通 UIView 上一样应用圆角。 However the UIVisualEffectView won't look good, because the area nearby curved 'sides' of the circle isn't blurred properly.但是UIVisualEffectView看起来不太好,因为圆的弯曲“侧面”附近的区域没有正确模糊。 Because it looks buggy and ugly I disadvise to round UIVisualEffectView .因为它看起来有缺陷和丑陋,我不建议舍UIVisualEffectView

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM