UIVisualEffectView如何在圆内使用? [英] How can UIVisualEffectView be used inside of a circle?
问题描述
我正在制作一个自定义控制圈.圆圈的一部分可能是透明的.如果它是半透明的而不是透明的,它将具有更好的视觉感和外观.
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.
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;
更新
快速游乐场示例:
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屋!