我有一个视图,用户可以通过UIGestureRecognizer
缩放和旋转。该视图包含两个视图:
superview
- content
- button
content
应缩放并旋转,button
应保持其大小和位置。
我正在CGAffineTransform
和UIGestureRecognizer
的帮助下缩放和旋转视图。
我怎样才能做到这一点?
我试过了:
superview
并重置button
转换和位置。没有成功。content
并尝试将content.frame.size
设置为superview
。没有成功。 (虽然我认为这必须以某种方式工作)。修改
处理旋转和缩放的代码:
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];
}
只是基本的缩放和旋转,没什么特别的。
答案 0 :(得分:1)
实际解决方案:
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];
}