gpt4 book ai didi

objective-c - 相对于动画将 CGPoints 从一个 View 转换为另一个 View

转载 作者:技术小花猫 更新时间:2023-10-29 10:37:12 29 4
gpt4 key购买 nike

我正在尝试制作一个动画,其中我将一个 CGPoint 从一个 View 移动到另一个 View ,我想找到该点将引用第一个的坐标,以便我可以制作动画。

假设我在 view2 中有一个点 (24,15),我想将它动画化到 view1,我仍然想在新 View 中保留该点的值,因为我将点添加为新 View 的 subview ,但对于动画,我需要知道点所在位置的值,以便我可以进行补间。

请引用这张图:

enter image description here

现在这就是我要做的:

customObject *lastAction = [undoStack pop];
customDotView *aDot = lastAction.dot;
CGPoint oldPoint = aDot.center;
CGPoint newPoint = lastAction.point;

newPoint = [lastAction.view convertPoint:newPoint toView:aDot.superview];


CABasicAnimation *anim4 = [CABasicAnimation animationWithKeyPath:@"position"];
anim4.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
anim4.fromValue = [NSValue valueWithCGPoint:CGPointMake(oldPoint.x, oldPoint.y )];
anim4.toValue = [NSValue valueWithCGPoint:CGPointMake( newPoint.x, newPoint.y )];
anim4.repeatCount = 0;
anim4.duration = 0.1;
[aDot.layer addAnimation:anim4 forKey:@"position"];


[aDot removeFromSuperview];


[lastAction.view addSubview:aDot];
[lastAction.view bringSubviewToFront:aDot];

aDot.center = newPoint;

有什么想法吗?

最佳答案

使用 block 动画更容易看到。我认为目标是在其坐标空间中制作 view2 subview 的动画,然后在动画完成后,使用转换为新坐标空间的结束位置向 view1 添加 subview 。

// assume we have a subview of view2 called UIView *dot;
// assume we want to move it by some vector relative to it's initial position
// call that CGPoint offset;

// compute the end point in view2 coords, that's where we'll do the animation
CGPoint endPointV2 = CGPointMake(dot.center.x + offset.x, dot.center.y + offset.y);

// compute the end point in view1 coords, that's where we'll want to add it in view1
CGPoint endPointV1 = [view2 convertPoint:endPointV2 toView:view1];

[UIView animateWithDuration:1.0 animations:^{
dot.center = endPointV2;
} completion:^(BOOL finished) {
dot.center = endPointV1;
[view1 addSubview:dot];
}];

请注意,将点添加到 view1 会将其从 view2 中移除。另请注意,如果偏移 vector 将点移出其边界,view1 应该有 clipsToBounds == NO

关于objective-c - 相对于动画将 CGPoints 从一个 View 转换为另一个 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12293676/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com