CABasicAnimation fromValue & CAKeyframeAnimation values error



我正在为视图制作动画(moveonView)。已为此视图设置了自动布局。当我在CABasicAnimation中将moveonView的"y"位置作为fromValue时,它在动画过程中不在它的位置。我需要给出一些填充值以将其放置在正确的位置。为什么 fromValue 放置我的视图不正确?断点还显示fromValue从"moveonView"中获取正确的"y"值,但仍然错误地放置视图。可能是什么原因。

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时,这是视图左上角的 y 坐标(在 iOS 上),这意味着它与 CGRectGetMinY(frame) 相同

但是,图层的位置对应于视图的center。因此,当您对position.y进行动画处理时,这与移动视图中心相同。

您可以将 from 值更新为使用 CGRectGetMidY(frame)(请注意从 min 到 mid 的更改)。

animation.fromValue = @( CGRectGetMidY(_moveonView.frame) );  

最新更新