Search code examples
iosfacebookanimationuiviewdispatch

My dispatch animation doesn't works like i want to? iOS


I want to make an animation like the one that Facebook use to have in previous versions of their app, in which the title appear in the center and after 1 or 2 seconds it rise a few pixels so theres some space for login or something, the problem is that the animation is not working like I want, i leave my code so you can see whats wrong.... THANKS!

- (void)viewDidLoad
{

    self.view.backgroundColor = [UIColor colorWithRed:51/255.0f green:204/255.0f blue:102/255.0f alpha:1.0];
    self.LogoImageView.center = self.view.center;

    if ([[UIScreen mainScreen] bounds].size.height == 568)
    {
        //IPHONE 4 i

        self.LogoImageView.frame = CGRectMake(33, 156, 255, 255);

    }else
    {
        //IPHONE 3.5 i

         self.LogoImageView.frame = CGRectMake(33, 112, 255, 255);
    }

    double delayInSeconds = 2.0;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        [UIView animateWithDuration:1.0 delay:.35 usingSpringWithDamping:.10 initialSpringVelocity:.50 options:UIViewAnimationOptionCurveEaseInOut animations:^{

            if ([[UIScreen mainScreen] bounds].size.height == 568)
            {
                //iphone 5 image

                self.LogoImageView.frame = CGRectMake(33, 48, 200 , 200);

            }else
            {
                self.LogoImageView.frame = CGRectMake(33, 48, 200, 200);
            }




        } completion:nil];
    });






    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

Solution

  • Try calling [super viewDidLoad] before your animation. I suspect the LogoImageView to not being initialized yet at the time you do the animation.

    I don't know if you've noticed too but the frame you set in the animation seems to be the same for both screen sizes

    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
    
        self.view.backgroundColor = [UIColor colorWithRed:51/255.0f green:204/255.0f blue:102/255.0f alpha:1.0];
    
    
        // You can remove this line since you're already setting
        // the origin position right after.
        //
        // self.LogoImageView.center = self.view.center;
    
        BOOL isFourInchesScreen = [[UIScreen mainScreen] bounds].size.height == 568;
        if (isFourInchesScreen) {
    
            // IPHONE 4 i
            //
            self.LogoImageView.frame = CGRectMake(33, 156, 255, 255);
    
        } else {
    
            // iPhone 3.5 i
            //
            self.LogoImageView.frame = CGRectMake(33, 112, 255, 255);
        }
    
        [UIView animateWithDuration:1.0 delay:2.35f usingSpringWithDamping:.10 initialSpringVelocity:.50 options:UIViewAnimationOptionCurveEaseInOut animations:^{
    
            self.LogoImageView.frame = CGRectMake(33, 48, 200, 200);
    
        } completion:nil];
    }
    

    You can delete the dispatch_after as well since it's there is a delay parameters on the animation method.

    Hope that helps.