我试图从内嵌在scrollView中的stackView中剥离视图,然后将所述视图重新放置在与scrollView处于同一层次但位于视图层次结构相同级别的另一个视图中。
我试图实现的效果是,我正在为删除视图设置动画效果—该视图将被叠加在另一个视图中,而scrollView将向上滚动,而新视图将被添加到stackView中。被撕开的东西消失了。
不幸的是,由于rippedView处于(x:0,y:0)位置,实现这种效果仍然遥不可及。当我尝试将新帧强加到该视图上时,它很困难,因为我猜测像素是完美的正确帧。这是我的viewController的一些代码:
/*
I tried to make insertionView and imposeView have the same dimensions as the scrollView and
the stackView respectively as I thought if the rippedView’s original superView is the same
dimensions as it’s new superView, the rippedView would be positioned in the same place
without me needing to alter its frame.
*/
let insertionView = UIView(frame: scrollView.frame)
let imposeView = UIView(frame: stackView.frame)
rippedView.removeFromSuperview()
insertionView.addSubview(imposeView)
imposeView.addSubview(rippedView)
let newFrame = CGRect(x: 0, y: 450, width: rippedView.intrinsicContentSize.width, height:
rippedView.intrinsicContentSize.height)
rippedView.frame = newFrame
self.view.addSubview(insertionView)
最佳答案
在删除rippedView
之前,获取它的实际框架:
let newFrame = self.view.convert(rippedView.bounds, from: rippedView)