我正在为 View 设置动画 (moveonView)。已为此 View 设置自动布局。当我在 CABasicAnimation 中将 moveonView 的“y”位置作为 fromValue 时,它在动画期间不在它的位置。我需要提供一些填充值以将其放置在正确的位置。为什么 fromValue 错误地放置了我的 View ?断点还揭示了 fromValue 从“moveonView”中获取了正确的“y”值,但仍然错误地放置了 View 。可能是什么原因。
CABasicAnimation *animation = [CABasicAnimation animation];
animation.keyPath = @"position.y";
animation.delegate = self;
animation.fromValue = [NSNumber numberWithFloat:_moveonView.frame.origin.y];
animation.toValue = [NSNumber numberWithFloat:self.view.frame.size.height - 100]; //[NSValue valueWithCGPoint:endPosition.origin];
animation.duration = 3;
[_moveonView.layer addAnimation:animation forKey:@"basic"];
我需要给一些填充值来把它放在准确的位置
animation.fromValue = [NSNumber numberWithFloat:_moveonView.frame.origin.y + 50];
最佳答案
框架的原点和图层的位置不是同一个点。
当您请求 frame.origin.y
时,它是 View 左上角的 y 坐标(在 iOS 上),这意味着它与 CGRectGetMinY(frame )
。
然而,层的位置对应于 View 的中心
。因此,当您为 position.y
设置动画时,这与移动 View 的中心相同。
您可以更新您的 from 值以使用 CGRectGetMidY(frame)
(注意从 min 到 mid 的变化)。
animation.fromValue = @( CGRectGetMidY(_moveonView.frame) );
关于ios - CABasicAnimation fromValue 和 CAKeyframeAnimation 值错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29669617/