2011-04-22 32 views
0

我有一个自定义的UIView类,我在其上添加了一个UIScrollView。我想要做的是添加一个渐变到UIView的图层,以便在滚动视图上方可见。目前我这样做:添加渐变作为视图顶层的最佳方式是什么?

[self.layer insertSublayer:self.leftShadowLayer atIndex:[self.layer.sublayers count]]; 
[self.layer insertSublayer:self.rightShadowLayer atIndex:[self.layer.sublayers count]]; 

但这似乎有点hackish和由于某种原因容易出现问题。是否还有其他首选添加子图层的方式是视图中的顶层,而不管添加到视图图层中的是什么?

THX

回答

0

绘制使用CGGradientCreateWithColorComponents()CGGradientCreateWithColors梯度或()。 文档在于AT-

CGGradientRef guide

+0

呃,我的问题是关于如何绘制它如何使它设置为顶层的梯度? – HenryH 2011-04-25 02:50:10

0

见示例代码绘制的CALayer,其中U可以添加到一个UIView的层属性。

-(void)drawInContext:(CGContextRef)theContext 

{  

// self.opacity = .5;
CGMutablePathRef thePath = CGPathCreateMutable();

//CGPathMoveToPoint(thePath,NULL,100.0f,200.f); 

CGPoint aOnePoint=CGPointMake(cgpoint.x,tipPoint.y); 
CGPoint aTwoPoint=CGPointMake(cgpoint.x+5,460); 
CGPoint aThreePoint=CGPointMake(320,100); 
CGPoint aFourPoint=CGPointMake(320,tipPoint.y-25); 
CGPoint points[]={aOnePoint,aTwoPoint,aFourPoint}; 
CGPathAddLines(thePath, NULL,points,3); 



CGContextBeginPath(theContext); 
CGContextAddPath(theContext, thePath); 
//CAGradientLayer here...using the CGCreateGradient methods. 


CGContextSetLineWidth(theContext,2.0f); 

CGSize theShadowSize = CGSizeMake(4.0f,4.0f); 

CGContextSetShadowWithColor(theContext, theShadowSize,3,[UIColor darkGrayColor].CGColor); 
CGContextSetFillColorWithColor(theContext,[UIColor redColor].CGColor); 
CGContextFillPath(theContext); 

CFRelease(thePath); 

}

这种方式,您可以绘制自己的层,并把它添加到.layer视图的属性。 请注意,我们必须覆盖- (void)drawInContext:(CGContextRef)theContext以拥有自定义图层。可能需要调用

希望帮助..

+0

在索引0处插入图层以使其成为最顶层。 – 2011-04-25 04:26:54

+0

索引0是底层而不是顶部 – HenryH 2011-04-25 19:21:14

+0

是addSublayer:(CALayer *)图层方法在你的情况下不起作用? – 2011-04-26 05:24:49

相关问题