Make 2 animations in Custom View

2019-09-03 14:51发布

问题:

Im creating a custom view, it contains methods like Refresh,*LayoutSubviews*, following this tutorial

I want a label to be animated growing, to it's full width and then back to 0 width. Im using this code in "refresh" method:

[UIView animateWithDuration:1.3 animations:^{
        CGRect frame = CGRectMake(50, 15, 140, 20);
        [labelFav setFrame:frame];
    } completion:^(BOOL finished) {
        //[labelFav setHidden:YES];
        [UIView animateWithDuration:1.3 animations:^{
            CGRect frame = CGRectMake(50, 15, 0, 20);
            [labelFav setFrame:frame];
        }];

    }];

The code works on any other view, but since this is a Custom view by some reason the completion block is not executing. What happens is that it only does the first block. I set a breakpoint in the "completion" block and it stops there but the animation doesnt happen. I tried to setHidden the label and still it doesnt happen.

Any ideas?

回答1:

remove auto layout from you Interface builder main screen. your view may be changing it's frame, but auto layout is (possibly) forcing it elsewhere...



回答2:

Another way to solve problem. Tested.

[UIView animateWithDuration:1.3 animations:^{

    CGRect frame = CGRectMake(50, 15, 140, 20);
    [labelViewForFav setFrame:frame];

} completion:^(BOOL finished) {

   [UIView animateWithDuration:1.3 animations:^{

                CGRect frame = CGRectMake(50, 15, 0, 20);
                [labelViewForFav setFrame:frame];

        } completion:^(BOOL finished) {

        }];
}];

Problem is When you make UIlabel width less than its current width. Animation is not done properly. Animation not visible just jump to final frame.

to solve it Put your label in a view change the size of this view and it will work properly.