2011-08-23 6 views
4

drawRect에서 간단한 UIView가 있습니다. UIImageView를 서브 뷰로 추가 한 다음 해당 서브 뷰 위에 선을 그려 넣으려고합니다. 그러나 라인은 이미지 뷰 뒤에 그려집니다.iOS : 드로잉 라인이 서브 뷰 뒤에 나타납니다.

드로잉 컨텍스트를 하위 뷰로 만들 수 있도록하려면 어떻게해야합니까?

CGContextRef context = UIGraphicsGetCurrentContext(); 
CGContextSetLineWidth(context, 10.0); 
CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor); 
CGContextBeginPath(context); 
CGContextMoveToPoint(context, 0, 0); 
CGContextAddCurveToPoint(context,125,150,175,150,200,100); 
CGContextAddCurveToPoint(context,225,50,275,75,300,200); 
CGContextStrokePath(context); 

답변

7

다른 사용자 정의보기를 만들면 그 선을 그릴 수 있습니다. 그것을 ImageView와 같은 프레임으로 서브 뷰로 추가하고 bringSubviewToFront를 호출하여 이미지 뷰가 앞에 있는지 확인하십시오. 사용자 정의보기의 일부 속성을 opaque = NO과 같이 설정하고 배경색을 지우도록 설정해야 할 수도 있습니다 ([UIColor colorWithWhite:0.0 alpha:0.0]).

그런데 drawRect에 하위 뷰를 추가하지 마십시오. drawRect는 뷰 속성이나 계층을 변경하지 않고 그려야합니다. ImageView와 사용자 정의 선 그리기보기를 어딘가에 init에 추가해야합니다. 또한, 내 경우에는 제스처를 처리 아무것도하지만, 나던 작품을 그립니다

@implementation MyView 

-(id)initWithFrame:(CGRect)frame { 
    if ((self = [super initWithFrame:frame])) { 
     imageView = ... // however you create your image view 
     [self addSubview:imageView]; 
     lineView = [[MyLineView alloc] initWithFrame:imageView.frame]; 
     [self addSubview:lineView]; 
     [self bringSubviewToFront:lineView]; 
    } 
    return self; 
} 

... 

@end 

@implementation MyLineView 

-(void)drawRect:(CGRect)rect { 
    // your drawing code 
    // remember the coordinate system now has (0, 0) at the top left corner of the 
    // image view, so adjust your drawing code accordingly 
} 

@end 
+0

서브 뷰 그리기 라인에 책임이 할당됩니다과 같이 –

3

뷰는 앞뒤 순서로 그립니다. 하위보기 내에 표시 할 항목이 있으면 하위보기에서 그 그림을 그려야합니다.