user964366
user964366

Reputation:

Scaling several views same time

I have a UIView called character. Inside that view the user can add several UIImageView as subViews. I created gesture methods for Pinch, Move and Rotate. The gesture method were added to the character view.

For moving I'm using the gesture translationInView property and it works fine! My problem is with scalling.

Scaling individual UIImageViews is ok, no issues, but how to scale all of them??

I tried to scale the character view, it works fine scaling proportionally all the subviews but I don't want to change the character view, because I use it as a canvas to place the UIImageView to compose my character. If I change the character with a CGAffineTransformScale, all the new ImageViews that I add, got the same scale.

I tried this but it doesn't work:

   if ([gesture state] == UIGestureRecognizerStateBegan || [gesture state] == UIGestureRecognizerStateChanged) {
        CGFloat customScale = gesture.scale;
        for (UIImageView *singleView in [self.devoCharacter subviews]) {
            singleView.frame = CGRectMake(singleView.frame.origin.x / customScale, singleView.frame.origin.y /customScale, singleView.frame.size.width /customScale , singleView.frame.size.height /customScale);
            [gesture setScale:1];
        }
}

I wanted to reduce the size of each view and their relative distances in this way, but the whole thing expands from the coordinates origin.

Then I tried this:

if ([gesture state] == UIGestureRecognizerStateBegan || [gesture state] == UIGestureRecognizerStateChanged) {
        CGFloat customScale = gesture.scale;
        for (UIImageView *singleView in [self.devoCharacter subviews]) {
            singleView.transform = CGAffineTransformScale(singleView.transform, customScale, customScale);
            [gesture setScale:1];
        }
}

But this scales each view independently and does not maintain their relative distances. Is there anyway to just scale each view, proportionally? and maintaining their relative distances as well?

Upvotes: 1

Views: 90

Answers (1)

user964366
user964366

Reputation:

Ok I got how to do it. Scale every single view and then scale proportionally their centers. This solved my problem:

        for (Part *singleView in [self.canvas subviews]) {
            singleView.transform = CGAffineTransformScale(singleView.transform, customScale, customScale);
            singleView.center = CGPointMake(singleView.center.x * customScale, singleView.center.y * customScale);
            [gesture setScale:1];
        }

Upvotes: 1

Related Questions