我尝试使用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似乎不支持将UIColors作为模式图像。
你知道如何为子视图实现一个简单的边框淡出效果吗?
谢谢
发布于 2011-02-12 22:05:12
你可以使用一个小技巧:
//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];发布于 2013-01-22 01:17:32
我在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]https://stackoverflow.com/questions/4977947
复制相似问题