【问题标题】:UIView corner radius animationUIView 圆角半径动画
【发布时间】:2014-05-28 11:23:54
【问题描述】:

我有一些这样的 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 像素。然后在结束动画上,我再次从宽度值 40 到宽度值 16 进行动画处理,现在所有动画的角半径为 20 px。就像它只会在动画结束时调用 layoutIfNeeded

【问题讨论】:

    标签: ios xcode animation autolayout cornerradius


    【解决方案1】:

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

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

    【讨论】:

    • 现在我做了 CABasicAnimation 来为圆角半径设置动画,但我不喜欢它,因为我现在必须同时进行两个单独的动画。但它有效。
    【解决方案2】:

    更改按钮的宽度/高度 然后打电话。 [自我设置需求显示];

    【讨论】:

    • 当我写问题时,我这样做了。
    • 所以它对你有用吗?如果它是正确的然后投票给我的答案.. :)
    • 不,正如我在问题中所写的那样,我已经这样做了,但它没有用,否则我不会发布问题:)
    猜你喜欢
    • 2015-01-02
    • 2017-10-01
    • 2014-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多