Dan Morrow
Dan Morrow

Reputation: 4481

How can blurriness of UIVisualEffectView be modified while dragging in iOS?

Currently, I'm using a UIVisualEffectView to apply a blur to an image.

I have a UIScrollView. As I pull down on the scrollView, in my "scrollViewDidScroll" method, I'm changing the alpha of the UIVisualEffectView. The current behavior of this is that the blur radius changes smoothly as I drag around in the view.

The problem is, of course, I'm not supposed to be doing this. Getting warnings about changing the alpha value of a UIVisualEffectView.

I've seen people say to do a smooth transition of blurring using an animation, like this here: How to fade a UIVisualEffectView and/or UIBlurEffect in and out?

However, I haven't seen anything that allows me to do this during, say a pan-gesture or something. I mean, if I set up an animation with a timed amount, all good. But doing this during a drag?

Upvotes: 7

Views: 848

Answers (2)

Andrei Herford
Andrei Herford

Reputation: 18729

While the solution by @DanMorrow works, I came across a strange problem: When the ViewController that uses this solution presents another model ViewController, the app becomes unresponsive.

This problem went away, as soon I removed the line self.blurredEffectView.layer.speed = 0;. It's strange, that setting the layer speed of some subview influences a model ViewController presendet by this ViewController. Howerver, in my case this was clearly the source of the problem.

As @mattsven pointed out in his comment there is another solution using an UIViewPropertyAnimator which can be found in this answer in this answer . This solutions seems to be less hacky and it does not lead to the problem described above.

Upvotes: 0

Warpling
Warpling

Reputation: 2105

There is a way :)

As you've noticed, you can animate from a nil effect to an effect like UIBlurEffectStyleDark so if we add an animation and then pause the layer's animations we can control the progress of the effect by adjusting the layer's timeOffset!

- (void) setupBlur {
    // Setup the blur to start with no effect
    self.blurredEffectView = [[UIVisualEffectView alloc] initWithEffect:nil];

    // Add animation to desired blur effect
    [UIView animateWithDuration:1.0 animations:^{
        [self.blurredEffectView setEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleDark]];
    }];

    // Pause layer animations
    self.blurredEffectView.layer.speed = 0;
}

Adjust blur between 0.0 and 1.0 (animation duration):

- (void) adjustBlur:(CGFloat)blurIntensity {
    self.blurredEffectView.layer.timeOffset = blurIntensity;
}
  • Note: this won't work on iOS 8 where UIBlurEffect animations aren't supported.

Upvotes: 9

Related Questions