转换视图,保持子视图大小

时间:2014-05-28 21:10:00

标签: ios quartz-2d cgaffinetransform

我有一个视图,用户可以通过UIGestureRecognizer 缩放旋转。该视图包含两个视图:

 superview
  - content
  - button

content应缩放并旋转,button应保持其大小和位置。
我正在CGAffineTransformUIGestureRecognizer的帮助下缩放和旋转视图。


我怎样才能做到这一点?

我试过了:

  • 缩放/旋转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];
}

只是基本的缩放和旋转,没什么特别的。

1 个答案:

答案 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];
}