Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 找出当前动画块的动画持续时间_Iphone_Animation_Uiview_Uiviewanimation - Fatal编程技术网

Iphone 找出当前动画块的动画持续时间

Iphone 找出当前动画块的动画持续时间,iphone,animation,uiview,uiviewanimation,Iphone,Animation,Uiview,Uiviewanimation,在UIView动画块中,是否有方法获取当前动画的持续时间 [UIView animateWithDuration:1.0 animations:^{ // float duration = ? }]; 既然您使用的是块,为什么不用它捕获一个变量呢 CGFloat duration = 1.0; [UIView animateWithDuration:duration animations:^{ CGFloat theDuration = duration; }]; [Ca

在UIView动画块中,是否有方法获取当前动画的持续时间

[UIView animateWithDuration:1.0 animations:^{
    // float duration = ?
}];

既然您使用的是块,为什么不用它捕获一个变量呢

CGFloat duration = 1.0;
[UIView animateWithDuration:duration animations:^{ 
    CGFloat theDuration = duration; 
}];

[CatTransaction animationDuration]
是您要查找的内容

您可以非常轻松地获取当前动画。例如,设置
CATTransaction

CAAnimation *animation = [self.layer animationForKey:self.layer.animationKeys.firstObject];
[CATransaction begin];
[CATransaction setAnimationDuration:animation.duration];
[CATransaction setAnimationTimingFunction:animation.timingFunction];

// CALayer animation here

[CATransaction commit];

TL;DR:使用CALayer-actionForKey:,而不是-animationForKey:

@Dimitri Bouniol的回答在动画块中受影响的二传手调用时对我不起作用。根据我的理解,原因是UIView的动画系统在开始实际动画之前设置状态(并在开始实际动画之前调用setter)。不过,对我有效的是在层上调用类似的-actionForKey:method。返回的操作设置了正确的持续时间,并且可以在其回答中使用

CAAnimation *animation = (CAAnimation *)[self.layer actionForKey@"position"]; // or property of interest

[CATransaction begin];
[CATransaction setAnimationDuration:animation.duration];
[CATransaction setAnimationTimingFunction:animation.timingFunction];

// CALayer animation here

[CATransaction commit];

如果您的目标是iOS 9.0或更高版本,那么您正在寻找一个名为
inheritedAnimationDuration
的类属性
UIView

用法:

let currentAnimationDuration = UIView.inheritedAnimationDuration
从苹果文档中:

摘要

返回当前动画的继承持续时间

讨论

此方法仅在
UIView
动画块内调用时返回非零值


我不确定我是否理解你的意思,动画持续时间不是1.0吗?是的,但我正在块内使用一个组件,需要知道持续时间,我不想将持续时间作为变量传递好的问题+1对不起,我帮不上忙。我认为没有变量是不可能的。。。不使用它有什么特别的原因吗?不幸的是,这不起作用,UIView动画块显然不能直接与CATransaction.Agree一起使用。我们已经通过了持续时间,那么我们为什么要找到另一种方法呢?在iOS 11中不起作用
actionForKey:
返回符合
CAAction
协议但不是
CAAnimation的
\uUIViewAdditiveAnimationAction
。因此,
计时函数
属性不存在@迪米特里·布尼奥尔的答案对我很有用。谢谢,它节省了我很多时间