Ios – Transform all subviews

cocoa-touchiosiphoneobjective c

I have a UIView in a scrollview that contains about 100 subviews. The subviews all look the same and are instances of the same class. I have a transform that I want to apply to each subview. But the transform needs to change everytime zoomScale changes.

What's the best way to apply the transform to all the views? Currently Im implementing layoutSubviews, getting the array of subviews, and setting each subview's transform to the new transform.

Is there an optimal way to do this?

Update:

The views I want to transform are pins on a map. When I zoom in with scrollView they need to be scaled down (i.e. so they look the same size to the user) and remain locked to the same position).

- (void)layoutSubviews
{
    CGAffineTransform transform = CGAffineTransformMakeScale(1.0/self.zoomValue, 1.0/self.zoomValue);

    for(UIView *view in self.subviews){
        view.transform = transform;
    }    
}

- (void)scrollViewDidZoom:(UIScrollView *)scrollView
{
    self.mapMarkerContainer.zoomValue = scrollView.zoomScale;
    [self.mapMarkerContainer setNeedsLayout];
}

Best Answer

Since you're applying the same transformation to all subviews, CALayer's sublayerTransform property seems a perfect fit.

- (void)layoutSubviews
{
    CGAffineTransform transform = CGAffineTransformMakeScale(1.0/self.zoomValue, 1.0/self.zoomValue);

    self.layer.sublayerTransform = CATransform3DMakeAffineTransform(transform);
}