【发布时间】:2023-06-22 13:05:01
【问题描述】:
我有一个 UIVisualEffectView 和一个我想要查看的形状 (CGPath)。
我听说屏蔽 UIVisualEffectView 可以做到这一点。
所以我所做的(PocketSVG 是一个 API,可以帮助您将 SVG 文件转换为 CGPath)。代码:
let blur: UIBlurEffect = UIBlurEffect(style: .Light)
let ev: UIVisualEffectView = UIVisualEffectView(effect: blur)
ev.frame = self.frame
ev.translatesAutoresizingMaskIntoConstraints = false
self.addSubview(ev)
ev.rightAnchor.constraintEqualToAnchor(self.rightAnchor).active = true
ev.bottomAnchor.constraintEqualToAnchor(self.bottomAnchor).active = true
ev.leftAnchor.constraintEqualToAnchor(self.leftAnchor).active = true
ev.heightAnchor.constraintEqualToAnchor(self.heightAnchor, multiplier: 1.5).active = true
let myPath: CGPathRef = PocketSVG.pathFromSVGFileNamed("CategoriesBar").takeUnretainedValue()
var transform: CGAffineTransform = CGAffineTransformMakeScale(self.frame.size.width / 754.0, self.frame.size.height / 220.0)
let transformedPath: CGPathRef = CGPathCreateMutableCopyByTransformingPath(myPath, &transform)!
let maskLayer = CAShapeLayer()
maskLayer.path = transformedPath
maskLayer.fillRule = kCAFillRuleEvenOdd
let maskView = UIView(frame: self.frame)
maskView.backgroundColor = UIColor.blackColor()
maskView.layer.mask = maskLayer
ev.maskView = maskView
它在iOS 9 及以下版本上运行良好,但在iOS 10 和Xcode 8 上不起作用(没有模糊,只有透明视图)。
如果有人想尝试,这是我的SVG 文件的链接:
https://www.dropbox.com/s/mjql2bzf37vpl2r/CategoriesBar.svg?dl=0
知道如何让它在iOS 10 上也能正常工作吗?
谢谢!
【问题讨论】:
-
您将视觉效果视图的框架设置为
self.bounds,但将遮罩视图的框架设置为self.frame。看看是不是这个原因。UIVisualEffectView已知在屏蔽时有错误行为。 -
@LeoNatan 我尝试将两者都设置为
self.bounds和self.frame,但它仍然不起作用:(.... -
@LeoNatan 还有其他想法吗?
-
抱歉,现在不行。
-
您从哪里调用此代码,例如。
viewDidLoad还是viewWillAppear?如果是这样,view.frame和view.bounds将包含无效值,例如 (0, 0, 1000, 1000),因为视图尚未布局。在调试器中检查它。
标签: ios iphone swift cocoa-touch ios10