在iOS中拖动时如何修改UIVisualEffectView的模糊性? [英] How can blurriness of UIVisualEffectView be modified while dragging in iOS?

查看:137
本文介绍了在iOS中拖动时如何修改UIVisualEffectView的模糊性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我正在使用UIVisualEffectView将模糊应用于图像.

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

我有一个UIScrollView.当我下拉滚动视图时,在"scrollViewDidScroll"方法中,我正在更改UIVisualEffectView的Alpha.当前的行为是,当我在视图中四处拖动时,模糊半径会平滑变化.

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.

当然,问题是我不应该这样做.获取有关更改UIVisualEffectView的alpha值的警告.

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?

推荐答案

有一种方法:)

您已经注意到,您可以将nil效果动画化为UIBlurEffectStyleDark之类的效果,因此,如果我们添加动画然后暂停该层的动画,我们可以通过调整该层的timeOffset

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;
}

在0.0和1.0(动画持续时间)之间调整模糊:

Adjust blur between 0.0 and 1.0 (animation duration):

- (void) adjustBlur:(CGFloat)blurIntensity {
    self.blurredEffectView.layer.timeOffset = blurIntensity;
}

  • 注意:这不适用于不支持UIBlurEffect动画的iOS 8.
    • Note: this won't work on iOS 8 where UIBlurEffect animations aren't supported.
    • 这篇关于在iOS中拖动时如何修改UIVisualEffectView的模糊性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆