gpt4 book ai didi

iphone - 如何将 View 从容器 View 内部移动到另一个容器 View 内部?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:45:00 26 4
gpt4 key购买 nike

我有一个主视图。主视图内部有两个容器 View :按钮容器和显示容器。每个容器内分别有按钮和显示字段。

简而言之,我有三个级别的 View :主视图、 subview (容器)和子 subview (按钮和字段)。

按下按钮时,我想将该按钮的图像从按钮区域动画化到显示区域。也就是说,我需要将其向上移动两层,然后再向下移动两层。

目前,我正在按钮顶部创建一个 UIImage,与自定义按钮的 UIImage 相同。我移动它,然后在动画结束时将其销毁,这样我就不必更改实际按钮(我想将其留在原处以便重新使用它)。

显然我可以获得这个 UIImageView 的中心/边界/框架。

但是,我无法确定目的地的坐标。 Frame 和 Center 是相对于 superview 的,但那只是向上一级。为了将正确的 X 和 Y 偏移相加以到达目的地,似乎需要进行大量数学运算。

这是 UIView 的 convertRect:toView: 或 convertRect:fromView: 的工作吗?我很难确定如何使用它们,或者确定它们是否真的是正确的使用方法。

似乎是一个非常普遍的问题 - 将某些内容从一个“嵌套” View 移动到另一个“嵌套” View - 但我已经搜索过但找不到答案。

最佳答案

那些 convertRect 方法很难掌握。您的 View 包含两个 subview subA 和 subB,而 subA 包含一个按钮,并且您想要为按钮设置动画,使其从 subA 移动到 subB。让我们在具有主视图的 View Controller 中制作动画....

// subA is the receiver.  that's the coordinate system we care about to start
CGRect startFrame = [subA convertRect:myButton.frame toView:self.view];

// this is the frame in terms of subB, where we want the button to land
CGRect endFrameLocal = CGRectMake(10,10,70,30);
// convert it, just like the start frame
CGRect endFrame = [subB convertRect:endFrameLocal toView:self.view];

// this places the button in the identical location as a subview of the main view
// changing the button's parent implicitly removes it from subA
myButton.frame = startFrame;
[self.view addSubview:myButton];

// now we can animate in the view controller's view coordinates
[UIView animateWithDuration:1.0 animations:^{
myButton.frame = endFrame; // this frame in terms of self.view
} completion^(BOOL finished) {
myButton.frame = endFrameLocal; // this frame in terms of subB
[subB addSubview:myButton];
}];

关于iphone - 如何将 View 从容器 View 内部移动到另一个容器 View 内部?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11200336/

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