【问题标题】:Get object position within view within scroll view获取滚动视图内视图内的对象位置
【发布时间】:2022-01-06 14:33:53
【问题描述】:

我有这样的层次结构:

  • 滚动视图
  • --- 容器视图
  • ----- 循环视图

这个滚动视图也可以缩放。 当我按下这个循环视图时,我需要在循环视图的中心显示 view2(那里我有点击手势识别器)。但是 view2 应该是 container view 的碱液,因为我们还需要使用 cycle view 对其进行缩放。

我尝试用这种方法在需要显示 view2 的地方得到 rect。

func getSourceRect(for cycleView: UIView) -> CGRect {
   let rectOnContainer = cycleView.convert(cycleView.frame, to: containerView)
   let rectOnScrollView = containerView.convert(rectOnContainer, to: scrollView)
        
   return rectOnScrollView
}

然后我做下一步

let sourceRect = getSourceRect(for: cycleView)
view2.center = CGPoint(x: sourceRect.midX, y: sourceRect.midY)

它不起作用。 请帮助我理解这种行为并获得可以展示 view2

的位置

【问题讨论】:

  • 所以你希望 view2 以循环视图为中心但附加到容器视图?
  • 是cycleview的父容器视图吗?
  • @Biclops 是的,需要居中。而且cycleview的父级是容器视图。

标签: ios swift uiview uiscrollview scrollview


【解决方案1】:

所以您希望 view2 以循环视图为中心但附加到容器视图?

如果是这样的话

// if view2 and cycle view's parent is container view 
// then there should not be a need to transform view2's 
// coordinate space
view2.center = cycleView.center

// make sure z position is correct so that cycle view isn't obscuring
// view2's visibility.
view2.parent?.bringSubviewToFront(view2)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多