0
votes

I have some class for UIButton like this :

@implementation UIRoundButton

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self.layer setCornerRadius:self.frame.size.width/2];
    }
    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self.layer setCornerRadius:self.frame.size.width/2];
    }
    return self;
}

- (void) layoutIfNeeded
{
    [super layoutIfNeeded];
    [self.layer setCornerRadius:self.frame.size.width/2];
}

So it create rounded button and change corner radius for auto layout.

But my problem is when I want to do animation like this :

[UIView animateWithDuration:0.6 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{


    self.deleteButton.alpha = 1;

    self.buttonHeight.constant = 40;
    self.buttonWidth.constant = 40;

    [self layoutIfNeeded];

} completion:^(BOOL finished) {
    [UIView animateWithDuration:0.6 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{

        self.deleteButton.alpha = 0;

        self.buttonHeight.constant = 16;
        self.buttonWidth.constant = 16;

        [self layoutIfNeeded];

    } completion:^(BOOL finished) {

    }];
}];

So I change width and height for this button. The problem is that corner radius does not change properly. Why? It should call layoutIfNeeded method and change it.

Marko

EDIT :

This means that before animation the button has width and height 16. I animate to be 40. But the corners are through all animation 8 px. Then on the end animations I animate again from width value 40 to width value 16, now the corner radius is 20 px through all animation. Like it would call layoutIfNeeded onelly at the end of animations

2

2 Answers

2
votes

Think you need to call [self setNeedsUpdateConstraints] before layoutIfNeeded. I'm not sure that self.frame.size.width/2 will give you the right value in layoutIfNeeded, could be worth to try with a hard coded value, e.g. 20 to see if that could be the cause.

Hanv't seen anyone using animation code in layoutIfNeeded could also be worth to try to move the to be animated changes to the animation block instead.

1
votes

make changes to the width/height of the button and then call. [self setNeedsDisplay];