Recently I found the need to render gradients in an iOS app that I've been working on. Now, rendering gradients in a UIView is straight-forward enough. Simply override - (void) drawRect:(CGRect)rect and draw within it using CoreGraphics. This got me thinking. There must be lots of programmers out there who would wish to do the same thing. What if we could come up with something reusable? Without much ado, I give you UIGradientView - A subclass of UIView that supports rendering of gradients.
Here's a breakdown of the concepts used in rendering gradients with UIGradientView.
What is a Gradient Stop?
Let's say we want to vertically transition from White to Red and then from Red to Black. We start with White at the top and transition to red halfway through. Then we go from red at the half position, all the way to black at the bottom. Your mind's eye should have a picture like the one below.A Gradient Stop has an offset and colour. The offset is the percentage of the position at which the colour is the strongest. For example, in our multiple colour gradient example above, we would need three Gradient stops.
- GradientStop with offset 0 and colour White.
- GradientStop with offset 50 and colour Red.
- GradientStop with offset 100 and colour Black.
What is a Gradient Overlay?
LinearGradientOverlay
typedef enum {
LinearGradientDirection_Vertical,
LinearGradientDirection_Horizontal,
LinearGradientDirection_TopLeftToBottomRight,
LinearGradientDirection_TopRightToBottomLeft,
LinearGradientDirection_Max
} LinearGradientDirection;
RadialGradientOverlay
typedef enum {
// The rendered radial gradient will completely fill
// the UIGRadientView
RadialGradientOverlayOptions_FillOuter = -1,
// The rendered radial gradient will fit the inside
// of the UIGradientView.
RadialGradientOverlayOptions_FillInner = -2,
RadialGradientOverlayOptions_Max = -3
} RadialGradientOverlayOptions;
UIGradientView
Presets
The project comes with a default set of gradient presets that can be loaded via the UIGradientView(Presets) category class. If you don't wish to use any of the presets, simply exclude the following files from your project:- UIGradientView+Presets.h
- UIGradientView+Presets.m
- Gradient-Presets.plist.
Feel free to checkout or fork the project from github. Feedback / suggestions are very much welcome!










.jpg)
.jpg)
