1

我使用 CGAffineTransformMakeRotation 旋转了一个视图。(iPhone - 只允许在一个视图控制器上横向显示

正如您在下面看到的,图像的左侧和右侧都有白色区域。
我希望图像占据黑色背景的整个空间。(至少在一维,宽度或高度)

在此处输入图像描述

以下是完整代码

- (void) viewDidLoad
{
    [super viewDidLoad];

    self.view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    self.view.backgroundColor = [UIColor blackColor];

    self.imageView.backgroundColor = [UIColor blackColor];
    self.imageView.opaque = NO;
    self.imageView.contentMode = UIViewContentModeScaleAspectFit;
    self.imageView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth |
        UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;

    [self.imageView setImageWithURL:[NSURL URLWithString:self.jsonAlbumImage.url_image
                                           relativeToURL: [NSURL URLWithString:@URL_BASE]]
                   placeholderImage: [GlobalHelper placeHolderImage]];

    [self.view addSubview: self.imageView];

}

- (void)didRotate:(NSNotification *)notification {
    UIDeviceOrientation orientation = [[notification object] orientation];

    if (orientation == UIDeviceOrientationLandscapeLeft) {
        [self.view setTransform:CGAffineTransformMakeRotation(M_PI / 2.0)];
    } else if (orientation == UIDeviceOrientationLandscapeRight) {
        [self.view setTransform:CGAffineTransformMakeRotation(M_PI / -2.0)];
    } else if (orientation == UIDeviceOrientationPortraitUpsideDown) {
        [self.view setTransform:CGAffineTransformMakeRotation(M_PI)];
    } else if (orientation == UIDeviceOrientationPortrait) {
        [self.view setTransform:CGAffineTransformMakeRotation(0.0)];
    }
}

- 编辑 -

最终对我有用的东西..不知道为什么修改有效..任何解释都会很棒!

  UIDeviceOrientation orientation = [[notification object] orientation];

    CGAffineTransform t;
    CGRect rect;
    if (orientation == UIDeviceOrientationLandscapeLeft) {
        t = CGAffineTransformMakeRotation(M_PI / 2.0);
        rect = CGRectMake(0,0,480,320);
    } else if (orientation == UIDeviceOrientationLandscapeRight) {
        t = CGAffineTransformMakeRotation(M_PI / -2.0);
        rect = CGRectMake(0,0,480,320);
    } else if (orientation == UIDeviceOrientationPortraitUpsideDown) {
        t = CGAffineTransformMakeRotation(M_PI);
        rect = CGRectMake(0,0,320,480);
    } else if (orientation == UIDeviceOrientationPortrait) {
        t = CGAffineTransformMakeRotation(0.0);
        rect = CGRectMake(0,0,320,480);
    }
    else
        return; // looks like there are other orientations than the specified 4

    [self.view setTransform:t];
    self.view.bounds = rect;
4

1 回答 1

1

- (void)didRotate:(NSNotification *)notification中,您需要重新布局视图,以便它们占据所有可用空间。你可以这样做:

- (void)didRotate:(NSNotification *)notification {
  UIDeviceOrientation orientation = [[notification object] orientation];

  CGAffineTransform t;
  if (orientation == UIDeviceOrientationLandscapeLeft) {
    t = CGAffineTransformMakeRotation(M_PI / 2.0);
  } else if (orientation == UIDeviceOrientationLandscapeRight) {
    t = CGAffineTransformMakeRotation(M_PI / -2.0);
  } else if (orientation == UIDeviceOrientationPortraitUpsideDown) {
    t = CGAffineTransformMakeRotation(M_PI);
  } else if (orientation == UIDeviceOrientationPortrait) {
    t = CGAffineTransformMakeRotation(0.0);
  }

  CGPoint screenCenter = CGPointMake([UIScreen mainScreen].bounds.width/2,[UIScreen mainScreen].bounds.height/2);
  self.view.center = CGPointApplyAffineTransform(screenCenter, t);
  self.view.bounds = CGRectApplyAffineTransform([UIScreen mainScreen].bounds, t);
  [self.view setTransform:t];

}

在哪里 :

CGRectApplyAffineTransform

将仿射变换应用于矩形。

       CGRect CGRectApplyAffineTransform (
           CGRect rect,
           CGAffineTransform t
         );

也尝试删除这一行:

self.view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;

如果您不打算使用自动旋转,则不需要它,我担心它可能会与您自己设置视图框架发生冲突。这只是一个假设,以解释您没有看到视图框架变化的事实。

编辑:

我很想知道这个转换的东西是做什么的

好吧,变换正在旋转。

旋转相对于用作枢轴的锚点;发生的情况是,如果锚点不在正在旋转的视图的中间,那么视图也会被平移(想象一下围绕顶点的旋转)。

因此,设置界限以使事情变得均匀是正确的;确实,我只是在建议以下几行:

  self.view.center = CGPointApplyAffineTransform(screenCenter, t);
  self.view.bounds = CGRectApplyAffineTransform([UIScreen mainScreen].bounds, t);

但可能对中心和边界应用相同的变换的想法并没有得到祝福。(这也是为什么我要求一些痕迹,看看发生了什么:-)

于 2013-01-15T09:01:37.297 回答