0

I'm trying to create an effect whereby a photo is blurry at the top, but not at the bottom, and the blurriness 'fades off' gradually. I achieved this with the code below, which worked fine in iOS9, but does not in iOS10.

I'm aware of a known bug, as described in this question, that prevents a layer having a mask and a blur on the same layer.

The difference between my question and the one linked, is I'm not interested in using a CAShapeLayer as my mask, but rather a CAGradientLayer. I've tried fiddling with adding views/masks/layers in different orders, but am not having much luck.

    var visualEffectView = UIVisualEffectView(effect: UIBlurEffect(style: .light))
    visualEffectView.frame = CGRect(x: 0.0, y:0.0, width: photo.bounds.width, height: photo.bounds.height)
    photo.addSubview(visualEffectView)

    let maskStartColour = UIColor(red: 0.0, green: 0.0, blue: 0.0, alpha: 1.0)
    let maskEndColour = UIColor(red: 0.0, green: 0.0, blue: 0.0, alpha: 0.0)

    let gradient = CAGradientLayer()
    gradient.frame = CGRect(x: 0, y: 0, width: visualEffectView.bounds.width, height: visualEffectView.bounds.height)
    let colors: [AnyObject] = [maskStartColour.cgColor, maskEndColour.cgColor]
    gradient.colors = colors
    gradient.startPoint = CGPoint(x: 0.0, y: 0.0)
    gradient.endPoint = CGPoint(x: 0.0, y: 1.0)


    visualEffectView.layer.mask = gradient
halfer
  • 19,824
  • 17
  • 99
  • 186
Ali Parr
  • 4,737
  • 3
  • 31
  • 35
  • 1
    Tried it http://imgur.com/KIvxmSf xcode 10 & 11, it worked fine - try adding the effect view to the UIVC.view instead of the photo view? Thats the only difference in my implementation (other than using IB) – solenoid Jul 25 '17 at 21:08
  • Hey - thanks for giving it a go! Does it look the same on iOS 10? The reason I ask is I understand the known bug is fixed in iOS 11. I get broken behaviour with your code on iOS 10 - Xcode 8.3.3. – Ali Parr Jul 25 '17 at 22:35
  • 1
    Damn! Tricked by xcode-beta. Thats pretty lame, I must admit. I would have run in to that when I switched to iOS10 for sure. (tried it in old xcode, yeah no go) – solenoid Jul 25 '17 at 23:08
  • Yeah. I'm tempted to just accept that this problem is best ignored until iOS 11 rolls around. I'm going to construct my view in a way that means the effect, although broken, won't ruin the user experience on iOS 10. I'll leave the question open just in case someone can think of some trickery to get around it. – Ali Parr Jul 25 '17 at 23:18

0 Answers0