3
我在Xcode 8和雨燕2.3
從this不錯的職位和my own question開始工作了一圈模糊的遮罩層,我能得到這個兩個不同的效果,但我不能「加入」了。添加上的UIView
第一個(模糊):
internal extension UIView {
/**
Add and display on current view a blur effect.
*/
internal func addBlurEffect(style style: UIBlurEffectStyle = .ExtraLight, atPosition position: Int = -1) -> UIView {
// Blur Effect
let blurEffectView = self.createBlurEffect(style: style)
if position >= 0 {
self.insertSubview(blurEffectView, atIndex: position)
} else {
self.addSubview(blurEffectView)
}
return blurEffectView
}
internal func createBlurEffect(style style: UIBlurEffectStyle = .ExtraLight) -> UIView {
let blurEffect = UIBlurEffect(style: style)
let blurEffectView = UIVisualEffectView(effect: blurEffect)
blurEffectView.frame = self.bounds
blurEffectView.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
return blurEffectView
}
}
第二個(徑向梯度):
public class BlurFilterMask: CAShapeLayer {
// MARK: - Public properties -
public var origin: CGPoint = CGPointZero
public var diameter: CGFloat = 0.0
public var gradientWidth: CGFloat = 200.0
public var gradientColorOpacity: CGFloat = 0.5
// MARK: - Layout -
override public func drawInContext(ctx: CGContext) {
let clearRegionRadius = self.diameter * 0.5
let blurRegionRadius = clearRegionRadius + self.gradientWidth
let baseColorSpace = CGColorSpaceCreateDeviceRGB()
let colours: [CGFloat] = [0.0, 0.0, 0.0, 0.0, // Clear region
0.0, 0.0, 0.0, self.gradientColorOpacity] // blur region color
let colourLocations: [CGFloat] = [0.0, 0.4]
let gradient = CGGradientCreateWithColorComponents (baseColorSpace, colours, colourLocations, 2)
CGContextDrawRadialGradient(ctx, gradient!, self.origin, clearRegionRadius, self.origin, blurRegionRadius, .DrawsAfterEndLocation)
}
}
任何提示,將不勝感激!
最近你不是問過類似的問題嗎? http://stackoverflow.com/questions/40256942/draw-hole-on-uiblureffect/40401855#40401855 –
類似的,我不能在孔上應用徑向漸變......我嘗試過所有可能的組合! –