[英]CAGradientLayer and scrollViewTexturedBackgroundColor
我正在嘗試使用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作為圖案圖像。
有什么想法可以為子視圖實現簡單的邊框淡入淡出效果嗎?
謝謝
您可以使用一些技巧:
//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]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.