且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

iOS 7动态模糊效果,如控制中心

更新时间:2023-02-03 10:09:42

以下是我找到的现成解决方案:



1。最意想不到的:使用UIToolBar

   - (id)initWithFrame:(CGRect)frame 
{
if((self = [super initWithFrame:frame]))
{
[self setup];
}
返回自我;
}

- (id)initWithCoder:(NSCoder *)编码器
{
if((self = [super initWithCoder:coder]))
{
[自我设置];
}
返回自我;
}

- (无效)设置
{
if(iOS7OrLater)
{
self.opaque = NO;
self.backgroundColor = [UIColor clearColor];

UIToolbar * toolbar = [[UIToolbar alloc] initWithFrame:self.bounds];
toolbar.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
toolbar.barTintColor = self.tintColor;
[self insertSubview:toolbar atIndex:0];
}
}

UIToolbar可以满足这种需求他唯一的内置模糊机制,这种机制是动态的,有什么好处。但糟糕的是,在某种程度上它会忽略颜色并使背景看起来不可救药......





更新:



为避免颜色中断,请勿使用barTintColor。如果你想要暗色调模糊,你也可以改变工具栏的样式(使用UIBarStyleBlack)。



2。 FXBlurView



与工具栏不同它更积极,但它的动态机制很少见,实际上它只能用于静态背景。 (动态=否)。




I'm trying to make a controller that will be similar to Control Center in iOS7. From WWDC session #226 I've learnt how to get blurred image with different effects

UIGraphicsBeginImageContextWithOptions(image.size, NULL, 0);

[view drawViewHierarchyInRect:rect];

UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

lightImage = [newImage applyLightEffect];

So, in other words, we just capture some image (make screenshot), perform blur effect and use this blurred image for our needs.

But if you open control center above some dynamic content you'll notice that control center's blurred background is changing as well as content does.

Does anybody know how to replicate this behavior?

The only way I see it is to capture content and make blur effect with some interval (e.g. half a second). But it looks redundantly.

Here are ready solutions that I've found:

1. The most unexpected: Use UIToolBar

- (id) initWithFrame:(CGRect)frame
{
    if ((self = [super initWithFrame:frame]))
    {
        [self setup];
    }
    return self;
}

- (id) initWithCoder:(NSCoder *)coder
{
    if ((self = [super initWithCoder:coder]))
    {
        [self setup];
    }
    return self;
}

- (void) setup
{
    if (iOS7OrLater)
    {
        self.opaque = NO;
        self.backgroundColor = [UIColor clearColor];

        UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:self.bounds];
        toolbar.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        toolbar.barTintColor = self.tintColor;
        [self insertSubview:toolbar atIndex:0];
    }
}

UIToolbar can be used for this needs, bacuse it has his only build-in blur mechanism, and this mechanism is dynamic, what is good. But the bad thing is that in some reason it ignores colors and makes background looks irredeemably...

Update:

To avoid color breaking, do not use barTintColor. You also may change style of toolbar if you want dark styled blur (use UIBarStyleBlack).

2. FXBlurView.

Unlike toolbar it more positive, but it's dynamic mechanism is rare yet and in fact it can be used only for static background. (dynamic = NO).