ios - UIView 圆角半径动画

标签 ios xcode animation autolayout cornerradius

我有一些像这样的 UIButton 类:

@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];
}

因此它创建了圆形按钮并更改了自动布局的圆角半径。

但我的问题是当我想做这样的动画时:

[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) {

    }];
}];

所以我改变了这个按钮的宽度和高度。问题是拐角半径没有正确改变。为什么?它应该调用 layoutIfNeeded 方法并更改它。

马可

编辑:

这意味着在动画之前按钮的宽度和高度为 16。我设置为 40。但是所有动画的角都是 8 px。然后在结束动画中,我再次从宽度值 40 到宽度值 16 设置动画,现在所有动画的角半径为 20 像素。就像它会在动画结束时单独调用 layoutIfNeeded

最佳答案

认为您需要在 layoutIfNeeded 之前调用 [self setNeedsUpdateConstraints]。我不确定 self.frame.size.width/2 是否会在 layoutIfNeeded 中为您提供正确的值,可能值得尝试使用硬编码值,例如20 看看这是否可能是原因。

Hanv 没有看到有人在 layoutIfNeeded 中使用动画代码也值得尝试将要动画的更改移到动画 block 中。

关于ios - UIView 圆角半径动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23910299/

相关文章:

ios - 如何获取所有包含特定字符串的网址?

ios - 计算 UIScrollView 的内容大小

iphone - 在 Objective-C 中,为什么它增加 4 而不是 1?

iphone - tableView 中疯狂的数组排序! sortedArrayUsingSelector 帮助?

ios - 总是从 iOS 中的函数中得到 nil 值

iphone - 更改cocos2d中的背景图片

cocoa - 主菜单栏中的菜单项以不同的字体大小显示。什么给出了?

iphone - 使用动画更改 UITextView 文本颜色

performance - JavaFx动画性能不佳,消耗了我所有的CPU

javascript - 如何解决在 JavaScript 中使用视差效果滚动时元素闪烁?