简体   繁体   中英

UIView animateWithDuration doesn't work with Rect width/height animation

The following code works. It animates all those components nicely

CGRect logoRect = self.logoImageView.frame;
CGRect loginBackgroundRect = self.loginControlsBkImageView.frame;
CGRect loginButtonRect = self.loginButton.frame;
CGRect tableViewRect = self.tableView.frame;
CGRect forgotPasswordRect = self.forgotButton.frame;
CGRect signupButtonRect = self.signUpButton.frame;


if (!iPhone) {

    // ipad keyboard-on-screen re-layout

    logoRect.origin.y-= 60;
    loginBackgroundRect.origin.y-= 110;
    loginButtonRect.origin.y-=110;
    tableViewRect.origin.y-=110;
    forgotPasswordRect.origin.y-=110;
    signupButtonRect.origin.y-=200;

}
else {

    // iphone keyboard-on-screen re-layout

    if (portrait) {
        logoRect.origin.y-=17;
        logoRect.origin.x-=50;
        loginBackgroundRect.origin.y-= 70;
        loginButtonRect.origin.y-=70;
        tableViewRect.origin.y-=70;
        forgotPasswordRect.origin.y-=70;
        //signupButtonRect.origin.y+=200; // get off screen!
    } else {
        logoRect.origin.y-= 30;
        loginBackgroundRect.origin.y-= 25;
        loginButtonRect.origin.y-=25;
        tableViewRect.origin.y-=25;
    }
}    


[UIView animateWithDuration:0.2f
                      delay:0.0f
                    options:UIViewAnimationOptionCurveEaseIn
                 animations:^(void) {
                     self.logoImageView.frame = logoRect;
                     self.loginControlsBkImageView.frame = loginBackgroundRect;
                     self.loginButton.frame = loginButtonRect;
                     self.tableView.frame = tableViewRect;
                     self.forgotButton.frame = forgotPasswordRect;
                     self.signUpButton.frame = signupButtonRect;
                 }
                 completion:NULL];

Take the following code and add one line (see below) to animate the WIDTH of the logoImageView... and puff... only the logoImageView animation works - the rest simply doesn't move. as if the frame size animation causes everything else not to animate if in the same animation block.

if (portrait) {
        logoRect.origin.y-=17;
        logoRect.origin.x-=50;
        loginBackgroundRect.origin.y-= 70;
        loginButtonRect.origin.y-=70;
        tableViewRect.origin.y-=70;
        forgotPasswordRect.origin.y-=70;
        //signupButtonRect.origin.y+=200; // get off screen!
    } else {
        logoRect.origin.y-= 30;
        logoRect.size.width-= 30;   // <------- LINE BEING ADDED HERE

        loginBackgroundRect.origin.y-= 25;
        loginButtonRect.origin.y-=25;
        tableViewRect.origin.y-=25;
    }

I'm at a loss here. Does anyone know what's going on?

Try to change whole frame, not just width. It should work.

Put these lines before commiting animation (not in block):

self.logoImageView.frame = logoRect;
etc.

And instead of using animate method try to use commit animations this way:

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.75];
// Here some more animation settings
// Here your animations 
[UIView commitAnimations];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM