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.

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.

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

In fact this effect is achieved by an extra image - a separate window with an imageview is shown underneath a uialertview. That window makes it so you can't select or touch any other views. If you want that image it can be found right here


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.


Need Your Help

Prevent Redis from saving to disc

redis bulkinsert

I'm building a Redis db which consumes nearly all of my machines memory. If Redis startes to save to disc while heavy inserting is going on, the memory consumption is more or less doubled (as descr...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.