Browse files

Initial commit

  • Loading branch information...
0 parents commit ad6bae5befca8071098e71d02940194b89a903c7 @shnhrrsn shnhrrsn committed Nov 13, 2009
Showing with 212 additions and 0 deletions.
  1. +68 −0 EGOGradientView.h
  2. +115 −0 EGOGradientView.m
  3. +29 −0 README.textile
68 EGOGradientView.h
@@ -0,0 +1,68 @@
+//
+// EGOGradientView.h
+// EGOGradientView
+//
+// Created by Shaun Harrison on 11/13/09.
+// Copyright (c) 2009 enormego
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+//
+
+#import <UIKit/UIKit.h>
+
+/*
+ * EGOGradientView works as a straight pass through to
+ * CAGradientLayer, with the exception of the "colors" property.
+ * The colors property allows you to send UIColor objects, as well as CGColorRefs,
+ * and will always return an NSArray of UIColor objects.
+ */
+
+@interface EGOGradientView : UIView {
+
+}
+
+// CAGradientLayer requires you to pass only CGColorRefs.
+// However EGOGradientView will covert the UIColors to CGColorRefs for you, and will pass back UIColors
+@property(copy) NSArray *colors;
+
+/* An optional array of NSNumber objects defining the location of each
+ * gradient stop as a value in the range [0,1]. The values must be
+ * monotonically increasing. If a nil array is given, the stops are
+ * assumed to spread uniformly across the [0,1] range. When rendered,
+ * the colors are mapped to the output colorspace before being
+ * interpolated. Defaults to nil. Animatable. */
+
+@property(copy) NSArray *locations;
+
+/* The start and end points of the gradient when drawn into the layer's
+ * coordinate space. The start point corresponds to the first gradient
+ * stop, the end point to the last gradient stop. Both points are
+ * defined in a unit coordinate space that is then mapped to the
+ * layer's bounds rectangle when drawn. (i.e. [0,0] is the bottom-left
+ * corner of the layer, [1,1] is the top-right corner.) The default values
+ * are [.5,0] and [.5,1] respectively. Both are animatable. */
+
+@property CGPoint startPoint, endPoint;
+
+/* The kind of gradient that will be drawn. Currently the only allowed
+ * value is `kCAGradientLayerAxial' (the default value.). */
+
+@property(copy) NSString *type;
+
+@end
115 EGOGradientView.m
@@ -0,0 +1,115 @@
+//
+// EGOGradientView.m
+// EGOGradientView
+//
+// Created by Shaun Harrison on 11/13/09.
+// Copyright (c) 2009 enormego
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+//
+
+#import "EGOGradientView.h"
+#import <QuartzCore/QuartzCore.h>
+
+@interface EGOGradientView (Private)
+@property(nonatomic,readonly,retain) CAGradientLayer* layer;
+@end
+
+
+@implementation EGOGradientView
+
+
+- (id)initWithFrame:(CGRect)frame {
+ if (self = [super initWithFrame:frame]) {
+ // Initialization code
+ }
+ return self;
+}
+
+- (void)setColors:(NSArray *)newColors {
+ NSMutableArray* normalizedColors = [[NSMutableArray alloc] initWithCapacity:newColors.count];
+
+ for(id color in newColors) {
+ if([color isKindOfClass:[UIColor class]]) {
+ [normalizedColors addObject:(id)[(UIColor*)color CGColor]];
+ } else {
+ [normalizedColors addObject:color];
+ }
+ }
+
+ NSLog(@"Setting: %@ | %@ | %@ | %@", normalizedColors, newColors, self, self.layer);
+ self.layer.colors = normalizedColors;
+ [normalizedColors release];
+}
+
+- (NSArray*)colors {
+ NSMutableArray* uiColors = [[NSMutableArray alloc] initWithCapacity:self.layer.colors.count];
+
+ for(id cgColor in self.layer.colors) {
+ [uiColors addObject:[UIColor colorWithCGColor:(CGColorRef)cgColor]];
+ }
+
+ NSArray* colors = [NSArray arrayWithArray:uiColors];
+ [uiColors release];
+
+ return colors;
+}
+
+- (void)setLocations:(NSArray *)locations {
+ self.layer.locations = locations;
+}
+
+- (NSArray*)locations {
+ return self.layer.locations;
+}
+
+- (void)setStartPoint:(CGPoint)point {
+ self.layer.startPoint = point;
+}
+
+- (CGPoint)startPoint {
+ return self.layer.startPoint;
+}
+
+- (void)setEndPoint:(CGPoint)point {
+ self.layer.endPoint = point;
+}
+
+- (CGPoint)endPoint {
+ return self.layer.endPoint;
+}
+
+- (void)setType:(NSString *)type {
+ self.layer.type = type;
+}
+
+- (NSString*)type {
+ return self.layer.type;
+}
+
++ (Class)layerClass {
+ return [CAGradientLayer class];
+}
+
+- (void)dealloc {
+ [super dealloc];
+}
+
+
+@end
29 README.textile
@@ -0,0 +1,29 @@
+h1. EGOGradientView
+Created by enormego
+
+EGOGradientView is a UIView that's layer backed by CAGradientLayer. This let's you have UIView's with gradient backgrounds, without much hassle.
+It works as a straight pass through to CAGradientLayer, with the exception of the "colors" property. The colors property allows you to send UIColor objects, as well as CGColorRefs, and will always return an NSArray of UIColor objects.
+
+h2. License
+
+EGOGradientView is available under the MIT license:
+
+_Copyright (c) 2009 enormego_
+
+_Permission is hereby granted, free of charge, to any person obtaining a copy_
+_of this software and associated documentation files (the "Software"), to deal_
+_in the Software without restriction, including without limitation the rights_
+_to use, copy, modify, merge, publish, distribute, sublicense, and/or sell_
+_copies of the Software, and to permit persons to whom the Software is_
+_furnished to do so, subject to the following conditions:_
+
+_The above copyright notice and this permission notice shall be included in_
+_all copies or substantial portions of the Software._
+
+_THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR_
+_IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,_
+_FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE_
+_AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER_
+_LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,_
+_OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN_
+_THE SOFTWARE._

0 comments on commit ad6bae5

Please sign in to comment.