在保持背景动态的同时向视图添加动画模糊

Adding an animatable blur to a view while keeping the background dynamic

我正在尝试实现自定义模糊视图,其中模糊半径可以设置动画,并且模糊视图可以堆叠混合模式 (CGBlendingMode or a CIFilter),同时保留任何 animations/actions背景。

我首先尝试了 UIVisualEffectView,但是如果不访问 private APIs 就无法对半径进行动画处理,这很可能会导致应用被拒绝。

拍摄快照和应用效果的问题是视图是静态的,背景中的任何移动都被模糊视图掩盖了。

我也查看了 FlexMonkey's Blurable,但我得到的结果与快照相似。

任何指导都会很有帮助。 干杯

编辑:我添加了一个 gif 来演示我正在尝试制作的东西。从左到右移动的红色视图模糊并放大了它下面的内容,同时显示了在它后面的 red/blue 方块上发生的动画。

混合模式非常棘手,我不确定是否可以将它们应用于实时 UIView (GPUImage might help here) but animating the blur radius (between 0 and UIVisualEffectView's full blur) is possible via :

您可以从 nil 效果动画到 UIBlurEffectStyleDark 这样的效果,因此如果我们添加动画然后暂停图层的动画,我们可以通过调整图层的 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(动画持续时间)之间调整模糊:

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

注意:这不适用于不支持 UIBlurEffect 动画的 iOS8。