user339946
user339946

Reputation: 6119

Inverting a transform scale

I'm having an issue with trying to invert a transform scale so to speak. Here is my code:

    - (void)updateTransforms {

    CGAffineTransform holeTransform = CGAffineTransformIdentity;
    holeTransform = CGAffineTransformTranslate(holeTransform, _holeOffset.x, _holeOffset.y);
    holeTransform = CGAffineTransformRotate(holeTransform, _holeRotation);
    //Problem code:
    holeTransform = CGAffineTransformScale(holeTransform, _holeScale, _holeScale);

    self.transform = holeTransform;
    self.imageToUse.transform = CGAffineTransformInvert(holeTransform); }

This is basically a method that lets me rotate, pan, and scale my object. The desired affect I want is the opposite of each action. So if you move left, the image moves right, rotate left, it rotates right, scale up, image scales down at the same rate. This code is working perfect so far for panning and rotating.

The problem I believe lies within inverting the scale, because scale doesn't actually go negative, it simply increases or decreases from 1.0. How can I modify my code to get scaling working right? Thanks.

Upvotes: 0

Views: 1607

Answers (2)

rob mayoff
rob mayoff

Reputation: 385998

You didn't mention what actually happens incorrectly with your scaling transform. But since I helped you get the rotation and panning working, I will take a stab at this.

As picciano pointed out in his answer, to invert a scale factor you take its reciprocal, not its negative. You apparently think you need the negative of the scale factor. Based on that I speculate that you're initializing it to 0 (which is its own negative) instead of 1 (which is its own reciprocal) and getting errors like this:

Feb  9 14:17:41 Robs-iPad-2 imagehole[3138] <Error>: CGAffineTransformInvert: singular matrix.

Here's what you need to do to get your pinch gesture to scale the hole properly:

  1. You need to initialize _holeScale to 1. You can do this in viewDidLoad or viewWillAppear. If you have a reset button or similar control, you need to do it there too.

  2. You need to multiply _holeScale by the gesture recognizer's scale, and reset the recognizer's scale to 1, not 0:

    - (IBAction)pinchGesture:(UIPinchGestureRecognizer *)sender {
        _holeScale *= sender.scale;
        sender.scale = 1;
        [self updateTransforms];
    }
    

You can download my test project to see a working example.

Upvotes: 2

picciano
picciano

Reputation: 22711

This should do it: 2.0f becomes 0.5f, 5.0f becomes 0.2f, etc. You probably need to create a new transform object, rather than trying to do the transform invert.

holeTransform = CGAffineTransformScale(holeTransform, 1.0f/_holeScale, 1.0f/_holeScale);

Upvotes: 2

Related Questions