gpt4 book ai didi

ios - UIVisualEffectView 如何在圆圈内使用?

转载 作者:技术小花猫 更新时间:2023-10-29 10:56:10 25 4
gpt4 key购买 nike

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

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

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

enter image description here

(为了调试目的没有在圆圈内渲染任何东西)

如您所见, View 模糊了圆圈外的东西。

我不知道如何只在 View 内部模糊,预发布文档是 practically empty .我唯一的想法是创建许多 1x1 View 来覆盖圆圈,但这似乎不会真正起作用,即使它起作用也会是一个缓慢而丑陋的解决方案。 如何模糊 View 内的内容,而不模糊 View 外的任何内容?

最佳答案

将圆 View 的图层蒙版设置为实心圆:

CircleControlView *circleView = yourCircleView();

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

更新

Swift Playground 示例:

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

结果:

translucent circle

关于ios - UIVisualEffectView 如何在圆圈内使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25392465/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com