ppaulojr
ppaulojr

Reputation: 3647

Multiple Subviews inside a view and Autoresize

I'm trying to create a component that represent a logo using the code below:

- (void) createSubviews
{
    CGRect path[] = {
        CGRectMake(62.734375,-21.675000,18.900000,18.900000),
        CGRectMake(29.784375,-31.725000,27.400000,27.300000),
        CGRectMake(2.534375,-81.775000,18.900000,18.900000),
        CGRectMake(4.384375,-57.225000,27.400000,27.300000),
        CGRectMake(2.784375,62.875000,18.900000,18.900000),
        CGRectMake(4.334375,29.925000,27.400000,27.300000),
        CGRectMake(62.734375,2.525000,18.900000,18.900000),
        CGRectMake(29.784375,4.475000,27.400000,27.300000),
        CGRectMake(-21.665625,-81.775000,18.900000,18.900000),
        CGRectMake(-31.765625,-57.225000,27.400000,27.300000),
        CGRectMake(-81.615625,-21.425000,18.900000,18.900000),
        CGRectMake(-57.215625,-31.775000,27.400000,27.300000),
        CGRectMake(-81.615625,2.775000,18.900000,18.900000),
        CGRectMake(-57.215625,4.425000,27.400000,27.300000),
        CGRectMake(-21.415625,62.875000,18.900000,18.900000),
        CGRectMake(-31.765625,29.925000,27.400000,27.300000)};

    for (int i = 0; i < 16; i++) {
        CGRect rect = CGRectApplyAffineTransform(path[i],
          CGAffineTransformMakeScale(self.frame.size.width / 213.0,
                                     self.frame.size.height / 213.0));
        EllipseView * v = [[EllipseView alloc] initWithFrame:
                CGRectOffset(rect, self.frame.size.width/2.0, self.frame.size.height/2)];
        v.tag = 90000 + i;
        v.tintColor = self.tintColor;
        [self addSubview:v];
    }
    self.initialLenght = self.frame.size.width;
}

-(void) layoutSubviews
{
    CGFloat ratio = self.frame.size.width / self.initialLenght;
    for (int i = 0; i < 16; i++) {
        EllipseView * v = (EllipseView*)[self viewWithTag:90000+i];
        v.transform = CGAffineTransformMakeScale(ratio, ratio);
    }
}

I'm having a hard time using the CGAffineTransform operations. Any hints on how to handle multiple subviews inside a view and keep them resizing as a group?

enter image description here

Upvotes: 2

Views: 55

Answers (1)

Rainer Schwarze
Rainer Schwarze

Reputation: 4745

According to the UIView reference:

transform Property

Specifies the transform applied to the receiver, relative to the center of its bounds.

Note the "center of its bounds". The transform just scales the "content" of the EllipseView. Since the views are not moved in layoutSubviews, they remain at their position and only the dots are scaled.

Do you have to use transforms?

You might just change the frame of the views like you do in createSubviews.

As you already have the coordinates, you could create a CAShapeLayer, build a UIBezierPath with ellipses according to your CGRect array, add that layer as a sublayer to the main view and then scale that.

Upvotes: 1

Related Questions