Make gradient on iPhone/iPad
Categories: Apple, iPad, iPhone, Mac OS X on May.30, 2010
It’s easy! As of iPhone SDK 3.0, custom gradients can be implemented very easily, without subclassing or images, by using the new CAGradientLayer
add framework
1 | #import < QuartzCore/QuartzCore.h> |
so, example:
1 2 3 4 5 6 7 8 9 10 11 12 | UIView *myTabView = [[UIView alloc] initWithFrame:frame]; CAGradientLayer *gradient = [CAGradientLayer layer]; gradient.frame = myTabView.bounds; gradient.colors = [NSArray arrayWithObjects: (id)[[UIColor colorWithRed:0.1 green:0.1 blue:0.1 alpha:0.8 ] CGColor] , (id)[[UIColor colorWithRed:0.1 green:0.1 blue:0.1 alpha:0.8 ] CGColor] , (id)[[UIColor colorWithRed:0.1 green:0.1 blue:0.1 alpha:0.8 ] CGColor] , (id)[[UIColor blackColor] CGColor] , (id)[[UIColor blackColor] CGColor], nil]; [myTabView.layer insertSublayer:gradient atIndex:0]; [[self.tabBarController tabBar] insertSubview:myTabView atIndex:0]; [myTabView release]; |
Result:

or, nice example with use Core Graphics to draw the gradient
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | - (void)drawRect:(CGRect)rect { CGContextRef currentContext = UIGraphicsGetCurrentContext(); CGGradientRef glossGradient; CGColorSpaceRef rgbColorspace; size_t num_locations = 2; CGFloat locations[2] = { 0.0, 1.0 }; CGFloat components[8] = { 1.0, 1.0, 1.0, 0.35, // Start color 1.0, 1.0, 1.0, 0.06 }; // End color rgbColorspace = CGColorSpaceCreateDeviceRGB(); glossGradient = CGGradientCreateWithColorComponents(rgbColorspace, components, locations, num_locations); CGRect currentBounds = self.bounds; CGPoint topCenter = CGPointMake(CGRectGetMidX(currentBounds), 0.0f); CGPoint midCenter = CGPointMake(CGRectGetMidX(currentBounds), CGRectGetMidY(currentBounds)); CGContextDrawLinearGradient(currentContext, glossGradient, topCenter, midCenter, 0); CGGradientRelease(glossGradient); CGColorSpaceRelease(rgbColorspace); } |
Similar posts:


Оставить отзыв