您无法控制UIVisualEffectView 的模糊半径。您可以通过对要模糊的视图进行快照并将 CoreImage 的“CIGaussianBlur”过滤器应用于该快照然后在您要模糊的视图正上方的UIImageView 中显示模糊图像来实现您想要的。使用CIGaussianBlur,您可以将模糊半径应用于任意长度。
您可以在UIView 上使用扩展名以使其更方便:
extension UIView
{
func snapshotView(scale scale: CGFloat = 0.0, isOpaque: Bool = true) -> UIImage
{
UIGraphicsBeginImageContextWithOptions(self.bounds.size, opaque, scale)
self.drawViewHierarchyInRect(self.bounds, afterScreenUpdates: true)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
func blur(blurRadius blurRadius: CGFloat) -> UIImage?
{
guard let blur = CIFilter(name: "CIGaussianBlur") else { return nil }
let image = self.snapshotView(scale: 1.0, isOpaque: true)
blur.setValue(CIImage(image: image), forKey: kCIInputImageKey)
blur.setValue(blurRadius, forKey: kCIInputRadiusKey)
let ciContext = CIContext(options: nil)
let result = blur.valueForKey(kCIOutputImageKey) as! CIImage!
let boundingRect = CGRect(x: 0,
y: 0,
width: frame.width,
height: frame.height)
let cgImage = ciContext.createCGImage(result, fromRect: boundingRect)
return UIImage(CGImage: cgImage)
}
}
然后你可以添加一个半透明的叠加层来判断模糊的深浅,例如:
let overlay = UIView()
overlay.frame = view.bounds
overlay.backgroundColor = UIColor(white: 0.0, alpha: 0.30)
viewIWantToBlur.addSubview(overlay)
let image = viewIWantToBlur.blur(blurRadius: 2.0)
imageView.image = image
有关此方法的更多信息和更强大的工作示例,您可以查看here. 您应该知道这是性能密集型的。