2011-10-22 42 views
0

問題是...UIView與CGPath和另一個包含第一個UIView。如何適合第一個觀點在第二個?

我有一個路徑,我創建一個視圖,在其中繪製它。視圖具有相同的路徑維度。然後,我創建了第二個視圖,其中我重寫了sizeThatFit:方法,因此第一個視圖被縮放,直到第二個視圖的所有空間都已滿(這是我對什麼是sizeThatFit:方法的看法!我不知道它是否正確)。這是代碼:

CGRect rect = CGPathGetBoundingBox(objPath); 
CGRect areaPath = CGRectMake(x, y, rect.size.width, rect.size.height); 

FirstView* first = [[FirstView alloc] initWithFrame:areaPath andObjPath:objPath]; 
SecondView* second = [[SecondView alloc] initWithFrame:CGRectMake(x, y, 200, 200)]; 
[second addSubview:first]; 

在SecondView中,我有overriden sizeThatFit:方法!

我不知道它爲什麼不起作用!路徑始終具有相同的維度。我會採取一條路徑,並在一個視圖中繪製它,在該視圖中路徑佔據了他的維度。所以,例如,如果一個路徑的邊界框是[10,10],視圖是[100,100],那麼我的路徑會變得和視圖維度一樣大。我能怎麼做 ??

我希望問題很明顯。對不起,我的英語:)

這是FirstView.m:

@synthesize objPath; 


- (id)initWithFrame:(CGRect)frame andObjPath:(CGMutablePathRef)path { 

self = [super initWithFrame:frame]; 
if (self) { 
    // Initialization code. 
    objPath = CGPathCreateMutable(); 
    CGRect rect = CGPathGetBoundingBox(path);  
    CGAffineTransform trans = CGAffineTransformMake(10, 0, 0, 10, self.bounds.size.width, self.bounds.size.height); 
    CGPathAddPath(objPath, &trans, path); 
    CGRect pathContainer = CGPathGetBoundingBox(path); 
    CGPathAddRect(objPath, &trans, pathContainer); 
    CGPathCloseSubpath(objPath); 
} 
return self; 
} 

- (void)drawRect:(CGRect)rect { 
// Drawing code. 
CGContextRef current = UIGraphicsGetCurrentContext(); 

CGContextAddPath(current, objPath); 

CGContextSetLineWidth(current, 0.6); 
CGContextSetRGBStrokeColor(current, 0xff/255.0, 0x40/255.0, 0x40/255.0, 1); 
CGContextSetRGBFillColor(current, 0xff/255.0, 0xc1/255.0, 0xc1/255.0, 1); 

CGContextDrawPath(current, kCGPathFillStroke); 
} 

編輯:

,如果我做

FirstView* first = [[FirstView alloc] initWithFrame:areaPath andObjPath:objPath]; 
SecondView* second = [[SecondView alloc] initWithFrame:CGRectMake(x, y, 200, 200)]; 
[second addSubview:first]; 
[first sizeToFit]; 

和SecondView.mi覆蓋sizeThatFit方法,第一視圖是適合的!問題是路徑不適合!它總是相同的尺寸:(

EDIT2

我這樣嘗試過:

FirstView* first = [[FirstView alloc] initWithFrame:areaPath andObjPath: objPath]; 
[first setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight]; 
[first setContentMode:UIViewContentModeScaleAspectFit]; 

SecondView* second = [[SecondView alloc] initWithFrame:CGRectMake(x, y, 200, 200)]; 
second.autoresizesSubviews = YES; 
[second setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight]; 
[second setContentMode:UIViewContentModeCenter]; 

[second addSubview:first]; 

但沒有什麼這是非常緊張的....:'(

請幫助我!

回答

1

您的路徑有一定的大小,所以爲了擴大或縮小這條路徑,您需要對CGCo應用轉換ntext你正在繪製的地方。例如,你可以通過查看您的視圖的大小,並將其與你的路徑的大小,然後將這種規模上下文找出規模:

// get current context 

CGRect pathBoundingBox = CGPathGetBoundingBox(objPath); 
CGFloat scale = MIN(self.bounds.size.width/pathBoundingBox.size.width, 
        self.bounds.size.height/pathBoundingBox.side.height); 

CGContextScaleCTM(current, scale, scale); 

// draw path 
+0

我以另一種方式解決,但,這是一個可能的解決方案 – kinghomer

相關問題