iPhone的旋转视图不占用整个屏幕

时间:2013-01-15 08:24:01

标签: iphone view rotation

我使用CGAffineTransformMakeRotation旋转了一个视图。 (iPhone - allow landscape orientation on just one viewcontroller

如下所示,图像左右两侧有白色区域 我希望图像占据整个空间的黑色背景。(至少在一个维度,宽度或高度)

enter image description here

以下是完整代码

- (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;

1 个答案:

答案 0 :(得分: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);

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

相关问题