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:
- to scale/rotate the
superview
and reset thebutton
transform and position. No success. - to scale/rotate the
content
and try to set thecontent.frame.size
to thesuperview
. No success. (Although I think this has to work somehow). - and some obscure bad things, which are not worth to mention.
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.
content.frame.size
? Note that thecontent.frame
is the smallest rectangle that encloses the view, so it's tricky to use on a view that's been rotated. You can use thecenter
andbounds
properties to move and scale the view, and use thetransform
to rotate the view around its center. – user3386109content
thebounds
doesn't change. I set just thesize
, because thecontent
will have differentx
andy
coordinates, than thesuperview
. Thesuperview
can be moved around in it's parent view. The scaling and resizing itself is done by usingCGAffineTransform
. – Leandroscontent
a single view with no subviews, or is it multiple views and/or subviews? – user3386109content
is just a singleUIImageView
. – Leandrosbounds.size
to scale the view, usetransform
only for rotation, and use thecenter
property to position the view properly in thesuperview
. – user3386109