使用您提供的对象名称,这是一个创建第二个视图的示例,也是一个 UIButton (subViewOfInnerUIView),它将与第一个按钮 (drawButton) 具有相同的框架。这两个按钮都是主视图中不同超级视图的子视图。我使用了一个 UIButton,因此我可以标记视图,但任何其他可以设置框架的 UIView 子类也可以工作。
请注意,由于它们具有相同的帧,除了具有相同的大小之外,它们相对于其父视图的位置也相同。
即使有问题的对象位于几层深或浅的子视图中,这也应该有效。应该没关系。
可以在最新 XCode 的单一视图游乐场中重新创建该示例。希望这会有所帮助!
//: A UIKit based Playground for presenting user interface
import UIKit
import PlaygroundSupport
class MyViewController : UIViewController {
override func loadView() {
let view = UIView()
view.backgroundColor = .white
self.view = view
let subView1 = UIView(frame: CGRect(x: 40, y: 250, width: 300, height: 300))
subView1.backgroundColor = .red
view.addSubview(subView1)
let subView2 = UIView(frame: CGRect(x: 20, y: 50, width: 340, height: 100))
subView2.backgroundColor = .green
view.addSubview(subView2)
let drawButton = UIButton(frame: CGRect(x: subView1.frame.width / 2 - 50, y: 25, width: 150, height: 50))
drawButton.backgroundColor = .blue
drawButton.setTitle("DRAW BTN", for: .normal)
subView1.addSubview(drawButton)
let subViewOfInnerUIView = UIButton()
subViewOfInnerUIView.setTitle("DRAW BTN2", for: .normal)
subViewOfInnerUIView.backgroundColor = .brown
subView2.addSubview(subViewOfInnerUIView)
let frame = view.convert(drawButton.frame, to: nil)
subViewOfInnerUIView.frame = frame
}
}
// Present the view controller in the Live View window
PlaygroundPage.current.liveView = MyViewController()
这是使两个视图重叠的更新代码。我对更改进行了动画处理,因此很清楚,并且还评论了特定的框架以尝试解释它是如何完成的:
//: A UIKit based Playground for presenting user interface
import UIKit
import PlaygroundSupport
class MyViewController : UIViewController {
override func loadView() {
let view = UIView()
view.backgroundColor = .white
self.view = view
let subView1 = UIView(frame: CGRect(x: 40, y: 250, width: 300, height: 300))
subView1.backgroundColor = .red
view.addSubview(subView1)
let subView2 = UIView(frame: CGRect(x: 20, y: 50, width: 340, height: 100))
subView2.backgroundColor = .green
view.addSubview(subView2)
let drawButton = UIButton(frame: CGRect(x: subView1.frame.width / 2 - 50, y: 25, width: 150, height: 50))
drawButton.backgroundColor = .blue
drawButton.setTitle("DRAW BTN", for: .normal)
subView1.addSubview(drawButton)
let subViewOfInnerUIView = UIButton()
subViewOfInnerUIView.setTitle("DRAW BTN2", for: .normal)
subViewOfInnerUIView.backgroundColor = .brown
subView2.addSubview(subViewOfInnerUIView)
let frame1 = drawButton.frame
let frame2 = subView1.convert(drawButton.frame, to: view)
let frame3 = view.convert(frame2, to: subView2)
print(frame1) // original drawButton frame in its superview
print(frame2) // drawButton frame relative to the main view (self.view)
print(frame3) // drawButton frame relative to subView2 (this is the frame you want)
subViewOfInnerUIView.frame = view.convert(drawButton.frame, to: nil)
UIView.animate(withDuration: 5.0, delay: 0.0, options: [.autoreverse, .repeat], animations: {
subViewOfInnerUIView.frame = frame3
}, completion: nil)
subViewOfInnerUIView.frame = frame3
}
}
// Present the view controller in the Live View window
PlaygroundPage.current.liveView = MyViewController()
将 subViewOfInnerUIView.frame 设置为 frame3 的最后一行基本上就是你想要的。您可能可以创建一个函数来完成这 3 行的工作以使其更容易(将 2 个子视图作为参数),尽管如果视图层次结构很深,它可能不会那么简单:
let frame2 = subView1.convert(drawButton.frame, to: view)
let frame3 = view.convert(frame2, to: subView2)
subViewOfInnerUIView.frame = view.convert(drawButton.frame, to: nil)
希望这会有所帮助!