Iphone CAGradientLayer和scrollViewTexturedBackgroundColor

Iphone CAGradientLayer和scrollViewTexturedBackgroundColor,iphone,objective-c,uikit,core-graphics,Iphone,Objective C,Uikit,Core Graphics,我正在尝试使用CAGradientLayer在背景纹理视图中淡出前景控件。 简而言之,我有一个bg颜色为scrollViewTexturedBackgroundColor的视图。我在上面有一个视图,它的内容我想在边框处淡入背景视图的颜色 CAGradientLayer* gradient = [CAGradientLayer layer]; gradient.frame = self->scrollableCanvas.bounds; gradient.colors = [NSArray

我正在尝试使用CAGradientLayer在背景纹理视图中淡出前景控件。 简而言之,我有一个bg颜色为scrollViewTexturedBackgroundColor的视图。我在上面有一个视图,它的内容我想在边框处淡入背景视图的颜色

CAGradientLayer* gradient = [CAGradientLayer layer];
gradient.frame = self->scrollableCanvas.bounds;
gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor scrollViewTexturedBackgroundColor] CGColor], (id)[[UIColor scrollViewTexturedBackgroundColor] CGColor], nil];
[gradient setStartPoint:CGPointMake(0.90, 1)];
[gradient setEndPoint:CGPointMake(1, 1)];    
[[self->scrollableCanvas layer] addSublayer:gradient];
[[self->scrollableCanvas layer] setMasksToBounds:YES];
不幸的是,CAGradientLayer似乎不支持UIColor作为图案图像

有没有办法为子视图实现简单的边框淡入淡出效果


谢谢

您可以使用一个小技巧:

//create normal UIImageView
UIImageView* iv = [[[UIImageView alloc] initWithImage: [UIImage imageNamed :@"bg_png.png"]] autorelease];
[superview addSubview: iv];

//draw gradient on top
UIView *view = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)] autorelease];
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = view.bounds;
gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor colorWithWhite: 1.0 alpha: 0.0] CGColor], (id)[[UIColor colorWithWhite: 1.0 alpha: 1.0] CGColor], nil];
[view.layer insertSublayer:gradient atIndex:0];
[superview addSubview: view];

我在UILabel上创建了一个类别,使用Max的答案在标签的末尾添加渐变

如果您需要,请点击这里:

#import "UILabel+GradientEnding.h"

@implementation UILabel (GradientEnding)

- (void)addGradientEnding
{
    //draw gradient on top
    UIView *gradientAlphaView = [[UIView alloc] initWithFrame:CGRectMake(self.frame.size.width - 80.0, 0, 80.0, self.frame.size.height)];
    UIColor *startColor = RGBA(0xf7f7f7, 0);
    UIColor *endColor = RGBA(0xf7f7f7, 1);
    CAGradientLayer *gradient = [CAGradientLayer layer];
    gradient.frame = gradientAlphaView.bounds;
    gradient.colors = [NSArray arrayWithObjects:(id)[startColor CGColor], (id)[endColor CGColor], nil];
    gradient.startPoint = CGPointMake(0, 0.5);
    gradient.endPoint = CGPointMake(1.0, 0.5);
    [gradientAlphaView.layer insertSublayer:gradient atIndex:0];
    [self addSubview:gradientAlphaView];
}

@end
但是你应该使用你自己的颜色。并使用此定义:

#define RGBA(rgbValue, opacity) [UIColor \
colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 \
green:((float)((rgbValue & 0xFF00) >> 8))/255.0 \
blue:((float)(rgbValue & 0xFF))/255.0 alpha:opacity]