【问题标题】:How to check if NSLayoutConstraint is animating如何检查 NSLayoutConstraint 是否动画
【发布时间】:2013-12-09 10:07:11
【问题描述】:

我正在创建一个自定义 NSLayoutConstraint 子类,我需要知道布局约束的 constant 属性当前是否正在为内部状态处理设置动画。也就是说,我需要区分:

{ //no animation
    myLayoutConstraint.constant = 100;
} 

{ //animated
    myLayoutConstraint.constant = 100;
    [UIView animateWithDuration:0.2 animations:^{
        [self.myViewThatHasTheConstraintAttached layoutIfNeeded];

    } completion:^(BOOL finished) {
        [...]
    }];
}

这样我就可以处理在动画中间接收消息的极端情况。这可能吗?

【问题讨论】:

    标签: ios objective-c cocoa-touch core-animation nslayoutconstraint


    【解决方案1】:

    做到这一点的唯一方法是在您想要访问它的任何地方都有一个布尔值并执行类似...

    { //no animation
        theView.animatingChange = NO;
        myLayoutConstraint.constant = 100;
    }
    
    { //animated
        theView.animatingChange = YES;
        myLayoutConstraint.constant = 100;
        [UIView animateWithDuration:0.2 animations:^{
            [self.myViewThatHasTheConstraintAttached layoutIfNeeded];
    
        } completion:^(BOOL finished) {
            [...]
            theView.animatingChange = NO;
        }];
    }
    

    视图上的属性立即更改为动画的“结束”值。它在制作动画时不会更改为所有中间值。只是屏幕上的绘图是动画的。

    【讨论】:

    • 我需要隐式处理这个,使用约束类的人不应该在外部设置任何东西。
    • 可以解释更多关于你想要达到的目标。您所问的问题的答案“视图是否有可能‘知道’它是否正在制作动画?”很简单。不,这是不可能的。你想达到什么目的。也许还有另一种方法。
    • 我正在创建一个布局系统,可以自然地打开和关闭视图(通过滑动标题等),并且我正在使用我编写的自定义 NSLayoutConstraint 类来完成此操作。目前,该系统非常适合我自己的项目,但我想进一步了解它的状态并使其成为一个开源框架。这就是我想做这个的原因。
    猜你喜欢
    • 2014-05-28
    • 2020-05-02
    • 2014-10-25
    • 1970-01-01
    • 2012-02-26
    • 1970-01-01
    • 1970-01-01
    • 2022-09-27
    • 2020-06-05
    相关资源
    最近更新 更多