如何在圆圈内使用UIVisualEffectView [英] How to use a UIVisualEffectView inside of a circle

查看:122
本文介绍了如何在圆圈内使用UIVisualEffectView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个自定义控制圈。圆的一部分可以是透明的。如果它是半透明的而不是透明的,它会更具视觉感并且看起来更好。

I'm making a custom control circle. Part of the circle may be transparent. It would make more visual sense and look better if it was translucent instead of transparent.

因为视图是矩形的,我只希望圆是半透明的,而不是其他的矩形,这是一个问题。

Because views are rectangular, and I only want the circle to be translucent, not the rest of the rectangle, this is a problem.

UIVisualEffectView 是自定义控件的后面。

The UIVisualEffectView is behind the custom control.

(没有任何在圆圈内呈现的东西用于调试目的)

(Without anything rendered inside of the circle for debugging purposes)

正如你所看到的,视图模糊了圆圈之外的东西。

As you can see, the view is blurring things outside of the circle.

我不知道如何在视图内部模糊,而预发布文档是几乎为空。我唯一的想法是创建许多1x1视图来覆盖圆圈,但这似乎不会真正起作用,即使它确实如此,这将是一个缓慢而丑陋的解决方案。 如何模糊视图中的内容,而不会模糊其外的任何内容?

I don't know how to blur inside only the view, and the prerelease documentation is practically empty. My only thought is to create many 1x1 views to cover the circle, but this seems like it would not really work, and even if it did it would be a slow and ugly solution. How can I blur the content inside the view, without blurring anything outside it?

推荐答案

设置圆形视图的图层蒙版到实心圆圈:

Set the circle view's layer mask to a filled circle:

CircleControlView *circleView = yourCircleView();

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



更新



Swift游乐场示例:

UPDATE

Swift playground example:

import UIKit
import XCPlayground
import QuartzCore

UIGraphicsBeginImageContext(CGSize(width: 100, height: 100))
let viewPath = UIBezierPath(ovalInRect:CGRect(x:1,y:1,width:98,height:98))
viewPath.lineWidth = 2
viewPath.stroke()
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()

let view = UIView(frame:CGRect(x:0,y:0,width:100,height:100))
XCPShowView("view", view)

let label = UILabel(frame:view.bounds)
label.text = "This is the text in the background behind the circle."
label.numberOfLines = 0
view.addSubview(label)

let effectView = UIVisualEffectView(effect:UIBlurEffect(style:.ExtraLight))
effectView.frame = view.bounds
view.addSubview(effectView)

let circleView = UIImageView(image:image)
effectView.addSubview(circleView)

let maskPath = UIBezierPath(ovalInRect:circleView.bounds)
let mask = CAShapeLayer()
mask.path = maskPath.CGPath
effectView.layer.mask = mask

结果:

这篇关于如何在圆圈内使用UIVisualEffectView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆