问题是......
我有一条路径,我创建了一个视图,我在其中绘制它。视图具有相同的路径维度。然后,我创建了第二个视图,其中我覆盖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中,我有覆盖sizeThatFit:方法!
我不知道为什么它不起作用!路径始终具有相同的维度。我会走一条路,然后在路径中占据他的维度。因此,例如,如果路径的boundingBox是[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.m中覆盖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];
但没有!这是非常紧张的......:'(
请我帮忙!
答案 0 :(得分:1)
您的路径具有一定的大小,因此为了向上或向下缩放该路径,您需要将变换应用于您正在绘制的CGContext。例如,您可以通过查看视图的大小并将其与路径大小进行比较来计算出比例,然后将该比例应用于上下文:
// 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