0

问题是...

我有一条路径,我创建了一个在其中绘制它的视图。视图具有相同的路径维度。然后,我创建了第二个视图,在其中我覆盖了 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.mi 覆盖 sizeThatFit 方法中,安装第一个视图!!!问题是路径也不合适!!!它始终具有相同的尺寸:(

编辑2

我也尝试过这种方式:

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];

但是什么都没有!压力很大.... :'(

请问我需要帮助!

4

1 回答 1

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
于 2012-01-12T04:19:58.113 回答