旋转全屏 UIImageView

发布于 2024-12-22 10:25:20 字数 1634 浏览 2 评论 0原文

我有两个图像:

Help-Portrait.png (320 x 480)
Help-Landscape.png (480 x 320)

当用户单击任何视图上的帮助按钮时,需要向他们显示正确的图像,并且该图像也应该随着设备的旋转而旋转。我尝试将 imageView 添加到窗口和导航控制器视图中。

由于某种原因,我对此有疑问。 有人能阐明我做错了什么吗?

UIImage *image = nil;
CGRect frame;
if (UIInterfaceOrientationIsPortrait([[UIApplication sharedApplication] statusBarOrientation])) {
    image = [UIImage imageNamed:@"Help-Portrait.png"];
    frame = CGRectMake(0, 0, 320, 480);
} else {
    image = [UIImage imageNamed:@"Help-Landscape.png"];
    frame = CGRectMake(0, 0, 480, 320);
}
if (!helpImageView) {
    helpImageView = [[UIImageView alloc] initWithFrame:frame];
    helpImageView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    helpImageView.image = image;
}
[[UIApplication sharedApplication] setStatusBarHidden:YES];

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(helpImageTapped:)];
helpImageView.userInteractionEnabled = YES;
[helpImageView addGestureRecognizer:tap];
[self.view addSubview:helpImageView];
[tap release];

willRotateToInterfaceOrientation:

if(helpImageView) {
    [(id)[UIApplication sharedApplication] setStatusBarHidden:NO animated:YES];
    if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation)) {
        helpImageView.image = [UIImage imageNamed:@"Help-Portrait.png"];
    } else {
        helpImageView.image = [UIImage imageNamed:@"Help-Landscape.png"];
    }
}

当您旋转设备时,图像和框架不会改变,最终会在屏幕左侧显示三分之二的肖像图像。

我想要的是它能够显示方向正确的图像,即正确的向上方向。我也想要图像旋转的动画,但这是一个附带问题

I have two images:

Help-Portrait.png (320 x 480)
Help-Landscape.png (480 x 320)

When a user clicks the help button on any view, they need to be presented with the correct image, which should also rotate when the device does. I have tried adding the imageView to both the window, and the navigation controller view.

For some reason I am having issues with this.
Could anyone shed light on what I am doing wrong?

UIImage *image = nil;
CGRect frame;
if (UIInterfaceOrientationIsPortrait([[UIApplication sharedApplication] statusBarOrientation])) {
    image = [UIImage imageNamed:@"Help-Portrait.png"];
    frame = CGRectMake(0, 0, 320, 480);
} else {
    image = [UIImage imageNamed:@"Help-Landscape.png"];
    frame = CGRectMake(0, 0, 480, 320);
}
if (!helpImageView) {
    helpImageView = [[UIImageView alloc] initWithFrame:frame];
    helpImageView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    helpImageView.image = image;
}
[[UIApplication sharedApplication] setStatusBarHidden:YES];

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(helpImageTapped:)];
helpImageView.userInteractionEnabled = YES;
[helpImageView addGestureRecognizer:tap];
[self.view addSubview:helpImageView];
[tap release];

willRotateToInterfaceOrientation:

if(helpImageView) {
    [(id)[UIApplication sharedApplication] setStatusBarHidden:NO animated:YES];
    if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation)) {
        helpImageView.image = [UIImage imageNamed:@"Help-Portrait.png"];
    } else {
        helpImageView.image = [UIImage imageNamed:@"Help-Landscape.png"];
    }
}

When you rotate the device the image and the frame don't change, and you end up with two thirds of the portrait image displayed on the left part of the screen.

What I want is it for it to show the correct image for the orientation, the right way up. Also I would like animation for the image rotation, but thats a side issue

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

辞旧 2024-12-29 10:25:20

您需要调整按钮图像的位置位于 ViewController 的 shouldAutorotateToInterfaceOrientation 方法(为您链接的文档)。

做类似的事情:

- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation
{
    UIImage *image = NULL;

    if (UIInterfaceOrientationIsPortrait(interfaceOrientation))
    {
        image = [UIImage imageNamed:@"Help-Portrait.png"];
    } else {
        image = [UIImage imageNamed:@"Help-Landscape.png"];
    }
    [yourButton setImage: image forState: UIControlStateNormal]
    return YES;
}

The place where you need to adjust your button image is in your ViewController's shouldAutorotateToInterfaceOrientation method (documentation linked for you).

Do something like:

- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation
{
    UIImage *image = NULL;

    if (UIInterfaceOrientationIsPortrait(interfaceOrientation))
    {
        image = [UIImage imageNamed:@"Help-Portrait.png"];
    } else {
        image = [UIImage imageNamed:@"Help-Landscape.png"];
    }
    [yourButton setImage: image forState: UIControlStateNormal]
    return YES;
}
蓝天 2024-12-29 10:25:20

Michael Dautermann 的答案看起来几乎包含了所有答案,但我反对使用shouldAutorotateToInterfaceOrientation。此方法的设计目的只是为了确定是否应该发生旋转,仅用于确定是否应该发生旋转。

您应该使用 didRotateFromInterfaceOrientation: willAnimateRotationToInterfaceOrientation:duration 来代替。

didRotateFromInterfaceOrientation: - interfaceOrientation 已在您的 UIViewController 上设置,因此您可以获得当前方向。在这种情况下,旋转动画已经完成。

willAnimateRotationToInterfaceOrientation:duration - 此方法的好处是执行时间。您处于旋转动画内部,因此在旋转动画完成后更改 UI 时不会出现不太漂亮的效果。

Michael Dautermann's answer looks to have almost all the answer, but I'm opposed to using shouldAutorotateToInterfaceOrientation. This method is designed only to determine if a rotation should or should not occur, nothing else.

You should use either didRotateFromInterfaceOrientation: willAnimateRotationToInterfaceOrientation:duration instead.

didRotateFromInterfaceOrientation: - interfaceOrientation is already set on your UIViewController so you can get the current orientation. In this case the rotation animation is already complete.

willAnimateRotationToInterfaceOrientation:duration - The benefit of this method is execution time. You are inside the rotation animation so you won't have the less than pretty effects which happens when you change UI either after the rotation animation completes.

九厘米的零° 2024-12-29 10:25:20

使用这段代码让它工作:

- (void)showHelpImage {

    NSString *imageName = @"Help_Portrait.png";
    CGRect imageFrame = CGRectMake(0, 0, 320, 480);

    helpImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]];
    helpImageView.frame = imageFrame;
    [self.view addSubview:helpImageView];
    [self updateHelpImageForOrientation:self.interfaceOrientation];

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(helpImageTapped:)];
    helpImageView.userInteractionEnabled = YES;
    [helpImageView addGestureRecognizer:tap];
    [self.view addSubview:helpImageView];
    [tap release];
}

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    [self updateHelpImageForOrientation:toInterfaceOrientation];
}

- (void)updateHelpImageForOrientation:(UIInterfaceOrientation)orientation {
    NSString *imageName = nil;
    CGRect imageFrame = helpImageView.frame;
    if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown) {
        imageName = @"Help_Portrait.png";
        imageFrame = CGRectMake( 0, 0, 320, 480);
    } else if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight) {
        imageName = @"Help_Landscape.png";
        imageFrame = CGRectMake( 0, 0, 480, 320);
    }
    helpImageView.image = [UIImage imageNamed:imageName];
    helpImageView.frame = imageFrame;
}

得到这个想法:
http:// www.dobervich.com/2010/10/22/fade-out-default-ipad-app-image-with-proper-orientation/

Got it working, with this code:

- (void)showHelpImage {

    NSString *imageName = @"Help_Portrait.png";
    CGRect imageFrame = CGRectMake(0, 0, 320, 480);

    helpImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]];
    helpImageView.frame = imageFrame;
    [self.view addSubview:helpImageView];
    [self updateHelpImageForOrientation:self.interfaceOrientation];

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(helpImageTapped:)];
    helpImageView.userInteractionEnabled = YES;
    [helpImageView addGestureRecognizer:tap];
    [self.view addSubview:helpImageView];
    [tap release];
}

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    [self updateHelpImageForOrientation:toInterfaceOrientation];
}

- (void)updateHelpImageForOrientation:(UIInterfaceOrientation)orientation {
    NSString *imageName = nil;
    CGRect imageFrame = helpImageView.frame;
    if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown) {
        imageName = @"Help_Portrait.png";
        imageFrame = CGRectMake( 0, 0, 320, 480);
    } else if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight) {
        imageName = @"Help_Landscape.png";
        imageFrame = CGRectMake( 0, 0, 480, 320);
    }
    helpImageView.image = [UIImage imageNamed:imageName];
    helpImageView.frame = imageFrame;
}

Got the idea from:
http://www.dobervich.com/2010/10/22/fade-out-default-ipad-app-image-with-proper-orientation/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文