【问题标题】:How can UIVisualEffectView be used inside of a circle?UIVisualEffectView 如何在圆圈内使用?
【发布时间】:2014-10-13 02:06:19
【问题描述】:

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

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

UIVisualEffectView 在自定义控件后面。

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

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

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

【问题讨论】:

    标签: ios ios8 mask translucency


    【解决方案1】:

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

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

    更新

    Swift 游乐场示例:

    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 vibrancy disabled 时才能让它工作。如果我打开活力,面具显然会被忽略。
    • @Benjohn 当视觉效果视图启用了活力时,我的圆形蒙版仍然可以正常工作。启用活力时,您的面具不起作用,这似乎很奇怪。我认为这一定是由其他原因引起的。
    • 嗨@E.A.Wilson 谢谢——你是对的,我用错了活力:-)
    • 这可以用不同的图像来完成吗?假设我有一个箭头图像,并且我希望 UIEffectsView 的效果仅应用于图像。我将如何将此代码应用于此?
    • 我不明白你在问什么,但听起来很不一样,应该是一个单独的问题。
    猜你喜欢
    • 2021-10-15
    • 1970-01-01
    • 1970-01-01
    • 2012-11-12
    • 1970-01-01
    • 1970-01-01
    • 2019-07-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多