Question

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!

Was it helpful?

Solution

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top