简体   繁体   中英

Animate UIView scale to zero

I am having trouble animating scale of a view to zero. Here's my code:

[UIView animateWithDuration:0.3 animations:^{
    myView.transform = CGAffineTransformMakeScale(0.0, 0.0);
} completion:^(BOOL finished){

}];

For some reason, the view stretches and squeezes horizontally like old TV tube switching off. If I make the scale to (0.1, 0.1) instead, it scales properly, but of course, not til zero.

Why is this happening?

If someone is still interested, here's what I did (in Swift) to make it work (almost):

UIView.animate(withDuration: 1) {
    myView.transform = CGAffineTransform(scaleX: 0.01, y: 0.01)
}

If the duration is short, you don't get to see that is doesn't scale exactly to 0 and it effectively fades.

In case anyone is still having this issue, the problem lies with the affine transform matrix not being unique for a scale factor of zero – there is no way of knowing how to interpolate "properly" between the initial matrix and the zero matrix, so you get weird effects like you described.

The solution is simply to use a small but nonzero scale value, eg

[UIView animateWithDuration:0.3
                 animations:^{ myView.transform = CGAffineTransformMakeScale(0.01, 0.01); }
                 completion:nil];

Try:

[UIView animateWithDuration:0.3 animations:^{
    myView.frame = CGRectMake(myView.origin.x, myView.origin.y, 0.0, 0.0);
} completion:^(BOOL finished){
}];

请用:

myView.layer.transform = CGAffineTransformMakeScale(0.0, 0.0);

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