I have an UIVisualEffectView
and a shape (CGPath
) that I want to view's shape to be.
I've heard that masking the UIVisualEffectView
will do it.
So that what I did (PocketSVG
is an API that helps you convert SVG
file to CGPath
). code:
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
It works great on iOS 9
and below but it doesn't work on iOS 10
and Xcode 8
(there is no blur, just transparent view).
Here's a link for my SVG
file if someone wants to try:
https://www.dropbox.com/s/mjql2bzf37vpl2r/CategoriesBar.svg?dl=0
Any idea how can I make it work on iOS 10
as well?
Thanks!