Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do vignette effect on iOS?

When UIAlertViews pop up, there is a vignette effect in the background. That is, the edges are darker and the center is lighter.

vignette iOS

I was wondering if this vignette effect was built into Cocoa Touch. I would like to show the vignette behind one of my custom views.

like image 283
JoJo Avatar asked Nov 30 '11 03:11

JoJo


2 Answers

It is built into UIKit (as UIAlertView is part of UIKit), but it's not public.

It shouldn't be too hard to create the same effect, though. It's just a radial gradient, which you can draw in code or Photoshop.

UPDATE: If you must know, the background is a class called _UIAlertNormalizingOverlayWindow with the following class hierarchy:

_UIAlertNormalizingOverlayWindow
_UIAlertOverlayWindow
UIWindow
like image 141
Can Berk Güder Avatar answered Sep 16 '22 16:09

Can Berk Güder


Create a class called VignetteEffect as a subclass of UIView

Add this code to your -drawRect: method:

- (void)drawRect:(CGRect)rect
{

    CGContextRef context = UIGraphicsGetCurrentContext();
    CGColorSpaceRef colSp = CGColorSpaceCreateDeviceRGB();

    CGGradientRef gradient = CGGradientCreateWithColors(colSp, (__bridge CFArrayRef)[NSArray arrayWithObjects:(id)[[UIColor colorWithRed:0 green:0 blue:0 alpha:0] CGColor], (id)[[UIColor colorWithRed:0 green:0 blue:0 alpha:0.5] CGColor], nil], 0);

    CGContextDrawRadialGradient(context, gradient, self.center, 0, self.center, self.frame.size.height+self.frame.size.height/4, 0);

    CGColorSpaceRelease(colSp);
    CGGradientRelease(gradient);

}

Tweak the values to your liking.

Add it to any view you have. Et voila. A nice vignette effect.

like image 38
the_critic Avatar answered Sep 20 '22 16:09

the_critic