UIImageView doesn't position properly on iPad

2019-08-30 06:51发布

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.

Here is my initialization.

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

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. :)

EDIT

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];
}

1条回答
Anthone
2楼-- · 2019-08-30 07:04

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 ;

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 ;

Good luck

查看更多
登录 后发表回答