首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CAGradientLayer和scrollViewTexturedBackgroundColor

CAGradientLayer和scrollViewTexturedBackgroundColor
EN

Stack Overflow用户
提问于 2011-02-12 20:03:51
回答 2查看 2.7K关注 0票数 1

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

代码语言:javascript
复制
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似乎不支持将UIColors作为模式图像。

你知道如何为子视图实现一个简单的边框淡出效果吗?

谢谢

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-02-12 22:05:12

你可以使用一个小技巧:

代码语言:javascript
复制
//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];
票数 3
EN

Stack Overflow用户

发布于 2013-01-22 01:17:32

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

如果你需要的话,这里就是:

代码语言:javascript
复制
#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

但是你应该用你自己的颜色。并使用这个定义:

代码语言:javascript
复制
#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]
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4977947

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档