10

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

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

UIVisualEffectView位于自定义控件的后面。

在此处输入图像描述

(出于调试目的,没有在圆圈内渲染任何内容)

如您所见,视图模糊了圆圈之外的事物。

我不知道如何仅在视图内部进行模糊处理,并且预发布文档实际上是空的。我唯一的想法是创建许多 1x1 视图来覆盖圆圈,但这似乎不会真正起作用,即使它这样做也是一个缓慢而丑陋的解决方案。如何模糊视图内的内容,而不模糊其外部的任何内容?

4

1 回答 1

30

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

CircleControlView *circleView = yourCircleView();

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

更新

斯威夫特游乐场示例:

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

结果:

半透明圆圈

于 2014-08-19T20:53:36.363 回答