Search code examples
iphoneobjective-ciosipadcore-animation

weird view with core animation


Here's the code:

 UIImageView * imgView = [[UIImageView alloc] initWithImage:image];



                CGRect imgFrame = imgView.frame;
                imgFrame.size.width = originalImageSize.width;
                imgFrame.size.height = originalImageSize.height;
                imgFrame.origin.x = 110;
                imgFrame.origin.y = 215;
                imgView.frame = imgFrame;

[UIView beginAnimations:nil context:NULL];
                [UIView setAnimationDuration:10];

                CGRect frame = imgView.frame;
                frame.size.width = SCREEN_WIDTH_PORTRAIT;
                [imgView setFrame:frame];
                 [imgView setCenter:CGPointMake(SCREEN_WIDTH_PORTRAIT/2, (SCREEN_HEIGHT_PORTRAIT)/2)];

 [fullSizeImageView setBackgroundColor:[UIColor blackColor]];
                [UIView commitAnimations];

Not sure why when the animation starts, it shifts a bit to the right by some far amount of distance. Any idea? Here's the video


Solution

  • Try to setCenter before beginAnimations

    imgView.center = CGPointMake(SCREEN_WIDTH_PORTRAIT/2, yYouLike);
    

    Your code should look like this:

    UIImageView * imgView = [[UIImageView alloc] initWithImage:image];
    
                    CGRect imgFrame = imgView.frame;
                    imgFrame.size.width = originalImageSize.width;
                    imgFrame.size.height = originalImageSize.height;
                    imgFrame.origin.x = 110;
                    imgFrame.origin.y = 215;
                    imgView.frame = imgFrame;
    
    imgView.center = CGPointMake(SCREEN_WIDTH_PORTRAIT/2, yYouLike);
    
    [UIView beginAnimations:nil context:NULL];
                    [UIView setAnimationDuration:10];
    
                    CGRect frame = imgView.frame;
                    frame.size.width = SCREEN_WIDTH_PORTRAIT;
                    [imgView setFrame:frame];
                     [imgView setCenter:CGPointMake(SCREEN_WIDTH_PORTRAIT/2, (SCREEN_HEIGHT_PORTRAIT)/2)];
    
     [fullSizeImageView setBackgroundColor:[UIColor blackColor]];
                    [UIView commitAnimations];
    

    Then, if your target is iOS 4.0 or later, it is better to use block-based animation methods, like written here: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIView_Class/UIView/UIView.html