ios - CABasicAnimation fromValue 和 CAKeyframeAnimation 值错误

标签 ios objective-c core-animation

我正在为 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/

相关文章:

ios - 导航栏进入困惑状态

objective-c - 如何在 nsMenuItem Cocoa Mac osx 中对齐文本中心

objective-c - 在 Cocoa 中创建具有 “genie” 效果的动画

objective-c - SecureUDID 在卸载和重新安装之间是否驻留?

ios - 泄漏仪(iOS)发现的内存泄漏

ios - UITableView 的 headerview 中的 UISwitch 正在更改状态但不调用事件处理方法

ios - 向 SKSpriteNode 施加脉冲以赋予其随机方向

iphone - 如何更改 UIView zPosition?

iphone - 是drawRect : called on multiple threads when using a CATiledlayer?

ios - 以编程方式在 swift View 之间创建 segue