Leandros
Leandros

Reputation: 16825

transform view, keep subview size

I have a view, which the user can scale and rotate through UIGestureRecognizer. This view contains two views:

 superview
  - content
  - button

The content should be scaled and rotated, and the button should keep it's size and position.
I'am scaling and rotating the views with help of CGAffineTransform and UIGestureRecognizer.


How can I achieve this?

I've tried:

Edit:

The code which handles the rotating and scaling:

if ([recognizer respondsToSelector:@selector(rotation)]) {
    CGFloat rotation = 0.0f - (self.lastRotation - [(UIRotationGestureRecognizer *) recognizer rotation]);

    CGAffineTransform currentTransform = self.content.transform;
    CGAffineTransform newTransform = CGAffineTransformRotate(currentTransform, rotation);
    self.content.transform = newTransform;

    self.lastRotation = [(UIRotationGestureRecognizer *) recognizer rotation];

} else if ([recognizer respondsToSelector:@selector(scale)]) {
    CGFloat scale = 1.0f - (self.lastScale - [(UIPinchGestureRecognizer *) recognizer scale]);

    CGAffineTransform currentTransform = self.content.transform;
    CGAffineTransform newTransform = CGAffineTransformScale(currentTransform, scale, scale);
    self.content.transform = newTransform;

    self.lastScale = [(UIPinchGestureRecognizer *) recognizer scale];
}

Just basic scaling and rotating, nothing fancy.

Upvotes: 1

Views: 1140

Answers (1)

Leandros
Leandros

Reputation: 16825

The actual solution:

if ([recognizer respondsToSelector:@selector(rotation)]) {
    CGFloat rotation = 0.0f - (self.lastRotation - [(UIRotationGestureRecognizer *) recognizer rotation]);

    CGAffineTransform currentTransform = self.transform;
    CGAffineTransform newTransform = CGAffineTransformRotate(currentTransform, rotation);
    self.transform = newTransform;

    self.lastRotation = [(UIRotationGestureRecognizer *) recognizer rotation];

} else if ([recognizer respondsToSelector:@selector(scale)]) {
    CGFloat scale = 1.0f - (self.lastScale - [(UIPinchGestureRecognizer *) recognizer scale]);

    CGRect bounds = self.bounds;
    bounds.size.width *= scale;
    bounds.size.height *= scale;
    self.bounds = bounds;

    self.lastScale = [(UIPinchGestureRecognizer *) recognizer scale];
}

Upvotes: 1

Related Questions