2011-04-22 35 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

相關問題