效果图
代码如下:
import UIKit
class ViewController: UIViewController ,UITableViewDelegate,UITableViewDataSource{
let view1 = UIView()
let view2 = UIView()
let tbView = UITableView()
var isOpen:Bool = false
let viewWidth = UIScreen.main.bounds.width
let viewHeight = UIScreen.main.bounds.height
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
cell.textLabel?.text = "标题\(indexPath.row)"
return cell
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
view1.frame = CGRect(x: 0, y: 0, width: viewWidth, height: viewHeight)
view1.backgroundColor = UIColor.blue
view2.frame = CGRect(x: 0, y: 0, width: 0, height: viewHeight)
view2.backgroundColor = UIColor.red
self.view.addSubview(view1)
self.view.addSubview(view2)
let button = UIButton(frame: CGRect(x: 17, y: 20, width: 50, height: 30))
button.setTitle("列表", for: UIControlState())
button.addTarget(self, action: #selector(openList), for: UIControlEvents.touchUpInside)
view1.addSubview(button)
tbView.frame = CGRect(x: 0, y: 0, width: view2.frame.width, height: view2.frame.height)
view2.addSubview(tbView)
tbView.dataSource = self
tbView.delegate = self
}
@objc func openList(){
print("2333")
if isOpen == false {
UIView.animate(withDuration: 0.3, delay: 0, animations: {
self.view1.frame = CGRect(x: self.viewWidth/5*4, y: 0, width: self.viewWidth/5, height: self.viewHeight)
self.view2.frame = CGRect(x: 0, y: 0, width: self.viewWidth/5*4, height: self.viewHeight)
self.tbView.frame = CGRect(x: 0, y: 0, width: self.view2.frame.width, height: self.view2.frame.height)
}, completion: { _ in
self.isOpen = true
})
} else {
UIView.animate(withDuration: 0.3, delay: 0, animations: {
self.view1.frame = CGRect(x: 0, y: 0, width: self.viewWidth, height: self.viewHeight)
self.view2.frame = CGRect(x: 0, y: 0, width: 0, height: self.viewHeight)
self.tbView.frame = CGRect(x: 0, y: 0, width: self.view2.frame.width, height: self.view2.frame.height)
}, completion: { _ in
self.isOpen = false
})
}
}
}
这种就是不支持随手拖动,还有一种实现方法就是借助滚动视图
import UIKit
class ViewController: UIViewController , UIScrollViewDelegate {
//滚动视图是一个可以拖动的组件
var scrollView = UIScrollView()
override func viewDidLoad (){
super.viewDidLoad()
//获取当前设备的屏幕尺寸信息
var screenFrame = UIScreen.main.bounds
let screenWidth = screenFrame.width
let screenHeight = screenFrame.height
scrollView.frame = screenFrame
//设置分页模式,滚动一次就是一页
scrollView.isPagingEnabled = true
//设置滚动视图的宽度
scrollView.contentSize = CGSize(width:screenWidth/5*9,height:screenHeight)
//设置滚动视图的背景色为白色
scrollView.backgroundColor = UIColor.white
self.scrollView.showsHorizontalScrollIndicator = false //滚动时是否显示滚动条
self.scrollView.showsVerticalScrollIndicator = false //是否加反弹效果
self.scrollView.bounces = false //滚动条的样式
//设置滚动视图对像的代理为当前类,这样就可以在当前文件中编写方法,以捕捉滚动视图的相关动作
scrollView.delegate = self
//创建第一个视图控制器的实例
let firstContorller = firstViewController()
//设置坐标原点的横向值为零
screenFrame.origin.x = 0
firstContorller.view.frame = CGRect(x: 0, y: 0, width: screenWidth/5*4, height: screenHeight)
//创建第二个视图控制器的实例
let secondControler = secondViewController()
screenFrame.origin.x = screenFrame.size.width/5*4
secondControler.view.frame = screenFrame
scrollView.addSubview(firstContorller.view)
scrollView.addSubview(secondControler.view)
self.view.addSubview(scrollView)
//默认在第二个页面
scrollView.contentOffset.x = screenWidth/5*4
}
}