开发者

Gradient imageView iOS

开发者 https://www.devze.com 2023-03-16 03:10 出处:网络
I am customizing my UITableView and I figured out how to set the selected color of each cell. In my cellForRowAtIndexPath method, I have the following code:

I am customizing my UITableView and I figured out how to set the selected color of each cell. In my cellForRowAtIndexPath method, I have the following code:

    UIView *bgColorView = [[UIView alloc] init];
    [bgColorView setBackgroundColor:[UIColor orangeColor]];
    [cell setSelectedBackgroundVi开发者_高级运维ew:bgColorView];
    [bgColorView release];

But it is a solid orange. I want to make it more slick looking, and have it be a slight gradient from light orange to darker orange. How can I do this?


You'd use Core Graphics (a.k.a. Quartz) to draw a gradient in your view's -drawRect: method:

CGContextRef ctx = UIGraphicsGetCurrentContext();
CGFloat colors[8] = {1.0, 0.75, 0.30, 0.5, 0.7, 0.2, 1.0, 0.8};
CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();
CGGradientRef gradient = CGGradientCreateWithColorComponents(space, colors, NULL, 2);
CGContextDrawLinearGradient(ctx, gradient, top, bottom, NULL);

You can limit the area that the gradient fills by creating a path in the current context (ctx) and the clipping to it using CGContextClip(ctx);. top and bottom are CGPoints that define the beginning and end of the gradient.


You will have to override the view's drawRect method to draw the gradient. It can be kind of a PIA, but you can also check out this open source component which may work:

http://cocoacontrols.com/platforms/ios/controls/gradient-view


@Caleb's answer is right on; I do this for a variety of things.

What no one has mentioned is that the view for which you need to implement drawRect: is a custom UITableViewCell.


simple google search will find your answer ....

http://bluesplat-tech.blogspot.com/2009/03/gradient-shading-uiview.html


If you want a complex or subtle gradient, you can set a background image that is partial alpha and set the cell background color to change appearance.

0

精彩评论

暂无评论...
验证码 换一张
取 消