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

发布于 2024-12-11 18:55:39 字数 2737 浏览 0 评论 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: 方法!

我不知道为什么它不起作用!路径始终具有相同的维度。我会选择一条路径并将其绘制在视图中,其中路径占据他的维度。因此,例如,如果路径的边界框是 [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];

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

请我需要帮助!

the problem is...

I have a path and i create a view in which i draw it. View has the same dimension of path. Then, i create a second view in which i override sizeThatFit: method so first view is scaled until all space of second View is full ( that's my idea about what sizeThatFit: method does! I don't know if it's correct ). That's the code:

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

In SecondView i have overriden sizeThatFit: method!

I don't know why it doesn't work! Path has always same dimension. I would take a path and draw it in a view in which path takes his dimension. So, for example, if a path's boundingBox is [10,10] and a view is [100,100], i would that path becomes as big as view dimension. How can i do ??

I hope the problem it's clear enough. Sorry for my english :)

This is 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);
}

EDIT:

If i do

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

and in SecondView.m i override sizeThatFit method, first view is fitted!!! The problem is that path isn't fitted too!!! It has always the same dimension :(

EDIT2

I tried in this way too:

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

But nothing! It's very stressful.... :'(

Pls i need help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

永不分离 2024-12-18 18:55:39

您的路径具有一定的大小,因此为了放大或缩小该路径,您需要对正在绘制的 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

Your path has a certain size, so in order to scale that path up or down, you'll need to apply a transform to the CGContext where you're drawing. For example, you could figure out the scale by looking at your view's size and comparing it to your path's size, then applying that scale to the context:

// 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文