【发布时间】:2020-02-25 08:34:23
【问题描述】:
我有两个视图,比如 view1 和 view2 添加到一个超级视图。 View1 在前面,并且比后面的 view2 有更大的框架,view2 的顶部在 view1 上方可见,使其看起来像一副纸牌。当用户水平滑动 view1 时,它应该在 view2 后面,而 view2 应该在前面。我不知道如何实现这一点,我需要在一个动画中交换两个视图位置并在视图层次结构中交换它们。任何帮助将不胜感激。
【问题讨论】:
我有两个视图,比如 view1 和 view2 添加到一个超级视图。 View1 在前面,并且比后面的 view2 有更大的框架,view2 的顶部在 view1 上方可见,使其看起来像一副纸牌。当用户水平滑动 view1 时,它应该在 view2 后面,而 view2 应该在前面。我不知道如何实现这一点,我需要在一个动画中交换两个视图位置并在视图层次结构中交换它们。任何帮助将不胜感激。
【问题讨论】:
试试这个:
if
let firstIndex = view.subviews.firstIndex(of: firstView),
let secondIndex = view.subviews.firstIndex(of: secondView)
{
let firstViewFrame = firstView.frame
UIView.animate(withDuration: 1, animations: {
self.firstView.frame = self.secondView.frame
self.secondView.frame = firstViewFrame
self.view.exchangeSubview(at: firstIndex, withSubviewAt: secondIndex)
})
}
示例:https://drive.google.com/open?id=1jAUokRRuUNtFmmtI1sBVh_eMpf3gUylV
【讨论】: