UIImageView在iPad上的位置不正确(始终为0、0) [英] UIImageView doesn't position properly on iPad (always on 0, 0)

查看:58
本文介绍了UIImageView在iPad上的位置不正确(始终为0、0)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天,我遇到了一个奇怪的问题.我有一个适用于iPhone和iPad的应用程序,它是一个通用应用程序,但是我制作了两个主要的视图控制器,一个用于iPhone,一个用于iPad.我先制作了iPhone版本...然后将一些代码复制到了iPad版本中.除了我的徽标UIImageView之外,其他所有东西似乎都正常运行.我有一个徽标,该徽标从屏幕外滑到导航栏的中心.在动画之前,我将UIImageView设置为不在屏幕上,因此它可以向下滑动.但是,即使我在uiimageview框架的x和y中放置了任何随机值,其值仍为0、0.这与我在iPhone上使用的代码相同,并且可以正常工作.

Today I faced a strange problem. I have an application which is the same for iPhone and iPad, it's an universal app, however I made two main view controllers, one for iPhone and one for iPad. I made the iPhone version first... then I copied some of the code to the iPad version. Everything seems to work fine except my logo UIImageView. I have a logo which slides down from off screen to the center of navigation bar. Before the animation I set up the UIImageView to be off screen, so then it could slide down. But, even if I put any random value in the x and y of uiimageview frame, it is still at 0, 0. It's the same code I have on iPhone and it's working ok.

这是我的初始化.

    UIImage *logoText = [UIImage imageNamed:@"logotext.png"];
    [logoTextView setUserInteractionEnabled:YES];
    logoTextView = [[UIImageView alloc] init];
    // I already tried [[UIImageView alloc] initWithFrame:CGRectMake......];
    logoTextView.frame = CGRectMake(389, -100, logoText.size.width, logoText.size.height);
    logoTextView.image = logoText;
    logoTextView.center = CGPointMake(389, -100);
    logoTextView.alpha = 1;
    [logoTextView addGestureRecognizer:resetGesture];
   // [logoTextView setBackgroundColor:[UIColor whiteColor]];
    [self.view insertSubview:logoTextView atIndex:10];

这实际上应该将UIImageView放置在中心位置,并且离屏幕100px.但是什么也没发生.仍然是0,0.我以各种方式尝试了一下,没有任何实际效果...

This should actually position the UIImageView in the center and 100px off the screen. But nothing happens. It is still 0, 0. I tried it all ways I know, nothing really works...

有人知道该怎么办吗?真的对我有帮助. :)

Does somebody know, what to do? It would really help me. :)

编辑

我刚刚找到了解决方案...我不知道为什么,但是这只能在新方法中起作用...很奇怪.

I have just found the solution... I don't know why but this only works if its in a new method... weird.

- (void)logoShow {

    UITapGestureRecognizer *resetGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(resetValues)];

    UIImage *logoText = [UIImage imageNamed:@"logotext@2x.png"];
    logoTextView = [[UIImageView alloc] initWithFrame:CGRectMake(389, -50, logoText.size.width/1.35, logoText.size.height/1.35)];
    logoTextView.center = CGPointMake(389, -50);
    logoTextView.image = logoText;
    logoTextView.alpha = 1;
    [logoTextView addGestureRecognizer:resetGesture];
    [self.view insertSubview:logoTextView atIndex:15];


    // LOGO TEXT ANIMATION
    [UIView beginAnimations:@"Logo Text Animation" context:nil];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
    [UIView setAnimationDelay:4.5];
    [UIView setAnimationDuration:0.5];
    logoTextView.alpha = 1;
    logoTextView.center = CGPointMake(389, 22);
    [UIView commitAnimations];
    [logoTextView setUserInteractionEnabled:YES];
}

推荐答案

我会将徽标视图设置为视图控制器的标题视图.在您的-initWithNib(或类似文件)中执行以下操作:

I would set the logo view as the title view of my view controller. In your -initWithNib (or similar) do this:

logoTextView.alpha = 0.0f ;
self.navigationItem.titleView = logoTextView ;

然后在您的-viewDidAppear:方法中执行以下操作:

Then in your -viewDidAppear: method do this:

CALayer * layer = logoTextView.layer ;
{
    CABasicAnimation * anim = [ CABasicAnimation animationWithKeyPath:@"transform" ] ;
    anim.fromValue = [ NSValue valueWithCATransfom3D:
                         CATransform3DMakeTranslate( 0, -100.0f, 0 ) ] ;
    [ layer addAnimation:anim forKey:nil ] ;
}
logoTextView.alpha = 1.0f ;

祝你好运

这篇关于UIImageView在iPad上的位置不正确(始终为0、0)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆