gpt4 book ai didi

ios - 带有 CGPath 的 UIView 和另一个包含第一个 UIView 的 UIView。如何使第一个 View 适合第二个 View ?

转载 作者:行者123 更新时间:2023-11-29 04:56:33 29 4
gpt4 key购买 nike

问题是...

我有一条路径,我创建了一个在其中绘制它的 View 。 View 与路径具有相同的维度。然后,我创建第二个 View ,在其中重写 sizeThatFit: 方法,以便缩放第一个 View ,直到第二个 View 的所有空间已满(这是我对 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: 方法!

我不知道为什么它不起作用!路径始终具有相同的维度。我会选择一条路径并将其绘制在 View 中,其中路径占据他的维度。因此,例如,如果路径的边界框是 [10,10] 并且 View 是 [100,100],我希望该路径变得与 View 尺寸一样大。我该怎么办??

我希望问题足够清楚。抱歉我的英语:)

这是 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 方法,第一个 View 已安装!问题是路径也没有安装!它始终具有相同的尺寸:(

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

但是什么也没有!压力好大……:'(

请问我需要帮助!

最佳答案

您的路径具有一定的大小,因此为了放大或缩小该路径,您需要对正在绘制的 CGContext 应用变换。例如,您可以通过查看 View 的大小并将其与路径的大小进行比较来计算比例,然后将该比例应用于上下文:

// 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

关于ios - 带有 CGPath 的 UIView 和另一个包含第一个 UIView 的 UIView。如何使第一个 View 适合第二个 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7862002/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com