2012-11-23 2 views
0

저는 UIGraphicsGetCurrentContext()을 사용하여 UI 요소에 대한 그라디언트 배경을 만들었지 만 드로잉이 일어나는 곳을 오해 한 것 같습니다. 드로잉이 서브 뷰에서 이루어지기를 원했지만 대신 뷰 자체에서 발생합니다.서브 뷰에서 CGContextRef 가져 오기

나는이 문제가 내가 UIGraphicsGetCurrentContext()을 사용할 때,보기의 CGContextRef을 얻고 있다고 생각한다. 그리하여 그 곳에서 드로잉이 일어나고있다. 내가 뭘하고 싶은 것은 드로잉을 서브 뷰에서 처리하게하여 다른 관련된 서브 뷰와 함께 페이드 인하거나 페이드 아웃 할 수있게하는 것입니다. 이 작업을 수행 할 수 있습니까, 아니면 그냥 다른 레이어 UIView 하위 클래스를 만들 필요가 있습니까?

여기에 제가 사용하는 코드의 단순화가 있습니다. 내 목표는 topBar에서 배경 그라디언트를 페이드 인 및 페이드 아웃 할 수 있고 InterfaceControlsView보기를 그대로 두는 것입니다.

@implementation InterfaceControlsView 

- (id)initWithFrame:(CGRect)frame 
{ 
    topBar = [[UIView alloc] initWithFrame:CGRectMake(0.0, 20.0, self.frame.size.width, 45.0)]; 
/* etc. */ 
} 

- (void)drawRect:(CGRect)rect { 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGRect rect = topBar.frame; // topBar is a subview 

    CGContextSaveGState(context); 
    CGContextAddRect(context, rect); 
    CGContextClip(context); 
    CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, 0); 
    CGContextRestoreGState(context); 
    /* etc. */ 
} 
@end 

답변

2

하위보기에 대한 그래디언트 bg를 만들려면 하위 클래스를 만들고 그라디언트 레이어를 사용할 필요가 없습니다. 희망 하시려면

CALayer *layer = _button.layer; 
layer.borderWidth = 1.0f; 
layer.borderColor = [UIColor lightGrayColor].CGColor; 


CAGradientLayer *gLayer = [CAGradientLayer layer]; 
[gLayer setName:@"gradient"]; 

gLayer.frame = layer.bounds; 

gLayer.colors = [NSArray arrayWithObjects: 
         (id)[UIColor colorWithRed:26.0/255.0 green:94.0/255.0 blue:74.0/255.0 alpha:1.0].CGColor, 
         (id)[UIColor colorWithRed:23.0/255.0 green:59.0/255.0 blue:37.0/255.0 alpha:1.0].CGColor, 
         nil]; 
[layer addSublayer:gLayer]; 
+0

감사합니다. 내가 한 일보다 훨씬 쉬워. 매우 감사. – Andrew