ios - UIImageView 旋转但未正确重新定位

标签 ios uiimageview rotation landscape portrait

我正在构建一个以横向和纵向模式显示图像的应用程序。旋转效果很好。图像在横向模式下也完美定位。然而,它在纵向中保留了它的横向坐标,结果导致它放错了位置。请在下面找到我的代码。你能让我知道我错过了什么吗?还有一种方法可以严格地从 Xib 文件实现这一点吗?

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[[self navigationController] setNavigationBarHidden:YES animated:NO];

UIImage *startImage = [UIImage imageNamed:@"title.png"];
UIImageView *startImageView = [[UIImageView alloc] initWithImage:startImage];

if (curOrientation == UIInterfaceOrientationPortrait || curOrientation == UIInterfaceOrientationPortraitUpsideDown) {

    [startImageView setFrame:CGRectMake(-128, 0, 1024, 1024)];

}else{

    [startImageView setFrame:CGRectMake(0, -128, 1024, 1024)];

}

[self.view addSubview:startImageView];


}

最佳答案

目前您仅在首次加载 View 时调用此代码。你实际上需要调用它

  • 每当 View 出现在屏幕上时(以防设备在屏幕外时旋转)
  • 每当设备旋转时

但是您应该将 View 创建代码保留在 viewDidLoad 中,因为您只想创建它一次。

创建一个属性来保留指向 View 的指针,以便您可以从代码中的所有这些地方引用它......

@property (nonatomic, weak) UIImageView* startImageView;

在 viewDidLoad 中创建它(但不要担心几何图形,因为您可以在 viewWillAppear 中执行此操作):

- (void)viewDidLoad
{
    [super viewDidLoad];
        // Do any additional setup after loading the view.
    [[self navigationController] setNavigationBarHidden:YES animated:NO];
    UIImage *startImage = [UIImage imageNamed:@"title.png"];
    UIImageView *startImageView = [[UIImageView alloc] initWithImage:startImage];
    self.startImageView = startImageView;
    [self.view addSubview:startImageView];
}

做一个通用的定位方法:

- (void) orientStartImageView
    {
        UIInterfaceOrientation curOrientation = [UIApplication sharedApplication].statusBarOrientation;
        if (curOrientation == UIInterfaceOrientationPortrait || curOrientation == UIInterfaceOrientationPortraitUpsideDown) {
            [self.startImageView setFrame:CGRectMake(-128, 0, 1200, 1200)];
        }else{
            [self.startImageView setFrame:CGRectMake(0, -128, 1200, 1200)];
        }
    }

从 viewWillAppear 调用它(每次 View 出现在屏幕上时触发):

- (void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self orientStartImageView];
}

从 viewWillLayoutSubviews 调用它(每次 View 在屏幕上和设备旋转时触发):

- (void)viewWillLayoutSubviews
{
    [super viewWillLayoutSubviews];
    [self orientStartImageView];
}

顺便说一下,我不确定你的框架是否正确 - 在纵向模式下你将左边缘移到屏幕外,在横向模式下你将上边缘移到屏幕外。那是你要的吗?很可能您可以在 Interface Builder 中实现您想要的,但从您的代码中不清楚那是什么 - 也许您可以张贴图片。还要检查您是否已禁用自动布局(Interface Builder 的文件检查器中的复选框)以简化问题。

更新

您可以在 Xib 中无需代码执行此操作:将 imageView 置于其 superView 的中心,将其大小设置为您的最终大小(例如 1200x1200),禁用自动布局,取消选择所有 Spring 和支柱,适本地设置您的 View 模式(例如centerscaleToFill)

enter image description here

enter image description here

enter image description here

关于ios - UIImageView 旋转但未正确重新定位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14926488/

相关文章:

java - 来自 4x4 变换矩阵 (libgdx) 的意外旋转角度

iphone - 通过 UIView 更改 MaskedLayer 的大小

ios - 使 UIWebview 显示桌面而不是移动页面

ios - Cloudkit 仪表板意外的服务器错误

ios - UIScrollView 在 ios6 中不滚动

html - 如何使用正则表达式从 html 字符串中获取图像 url

iphone - 通过触摸传递到下面的 UIViews

swift - CAShapeLayer。旋转框架坐标更新后,但路径坐标没有更新

java - 在 Android 上旋转图像而不缩小

iphone - 拥有多个具有不同触摸的移动 UIImage View