UIView.layer.presentationLayer返回最终值(而不是当前值) [英] UIView.layer.presentationLayer returns final value (rather than current value)
问题描述
UIView子类中有一些相关代码:
- (void) doMyCoolAnimation {
CABasicAnimation* anim = [CABasicAnimation animationWithKeyPath:@"position.x"];
anim.duration = 4;
[self.layer setValue:@200 forKeyPath:anim.keyPath];
[self.layer addAnimation:anim forKey:nil];
}
- (CGFloat) currentX {
CALayer* presLayer = self.layer.presentationLayer;
return presLayer.position.x;
}
当我在动画运行时使用 [self currentX]
时,得到的是 200
(最终值),而不是介于之间的值 0
(起始值)和 200
.是的,动画对用户可见,所以我在这里真的很困惑.
When I use [self currentX]
while the animation is running, I get 200
(the end value) rather than a value between 0
(the start value) and 200
. And yes, the animation is visible to the user, so I'm really confused here.
这是我调用 doMyCoolAnimation:
以及1秒后 currentX
的代码.
Here's the code where I call doMyCoolAnimation:
, as well as currentX
after 1 second.
[self doMyCoolAnimation];
CGFloat delay = 1; // 1 second delay
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
NSLog(@"%f", [self currentX]);
});
有什么想法吗?
推荐答案
我的UIView图层的presentationLayer没有提供当前值.相反,它给了我动画的最终价值.
My UIView's layer's presentationLayer was not giving me the current values. It was instead giving me the end values of my animation.
要解决此问题,我要做的就是添加...
To fix this, all I had to do was add...
anim.fromValue = [self.layer valueForKeyPath:@"position.x"];
...对于我的 doMyCoolAnimation
方法之前,我将最终值设置为:
...to my doMyCoolAnimation
method BEFORE I set the end value with:
[self.layer setValue:@200 forKeyPath:@"position.x"];
所以最后, doMyCoolAnimation
看起来像这样:
So in the end, doMyCoolAnimation
looks like this:
- (void) doMyCoolAnimation {
CABasicAnimation* anim = [CABasicAnimation animationWithKeyPath:@"position.x"];
anim.duration = 4;
anim.fromValue = [self.layer valueForKeyPath:anim.keyPath];
[self.layer setValue:@200 forKeyPath:anim.keyPath];
[self.layer addAnimation:anim forKey:nil];
}
这篇关于UIView.layer.presentationLayer返回最终值(而不是当前值)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!