Swift:左侧滑动的判断,连动。
来源:互联网 发布:php不好找工作 编辑:程序博客网 时间:2024/04/30 02:30
HomeViewController.swift
func setBarUI() {
...
//一定要将menuView.view添加进去
UIApplication.shared.keyWindow?.addSubview(menuView.view)
menuView.bindtoNav =navigationController?.tabBarController
view.addGestureRecognizer(UIPanGestureRecognizer(target:self, action:#selector(panGesture(pan:))))
menuView.view.addGestureRecognizer(UIPanGestureRecognizer(target:self, action:#selector(panGesture(pan:))))
}
func panGesture(pan:UIPanGestureRecognizer) {
menuView.panGesture(pan: pan)
}
MenuViewController.swift
var bindtoNav: UITabBarController?
let menuView = MenuViewController.shareInstance
var showView =false {
didSet {
showView ?showMenu() : dismissMenu()
}
}
extension MenuViewController {
staticlet shareInstance = createMenuView()
privatestatic func createMenuView() ->MenuViewController {
let storyboard =UIStoryboard.init(name:"Main", bundle: Bundle.main)
let menuView = storyboard.instantiateViewController(withIdentifier:"MenuViewController") as? MenuViewController
menuView?.view.frame =CGRect.init(x:-225, y: 0, width:225, height: screenH)
return menuView!
func panGesture(pan:UIPanGestureRecognizer) {
let xoff = pan.translation(in:view).x
if pan.state == .began {
beganDate =Date()
}
if pan.state == .ended {
endDate =Date()
//区分是轻扫还是滑动,nanoseconds:毫微秒十亿分之一秒
ifendDate! <beganDate! +150000000.nanoseconds {
if xoff >0 {
showView =true
} else {
showView =false
}
return
}
}
if (0 < xoff&& xoff <= 225&& !showView)|| (0 > xoff&& xoff >= -225&& showView) {
if pan.translation(in:view).x >0 {
moveMenu(pan.translation(in:view).x)
} else {
moveMenu(225 + pan.translation(in:view).x)
}
if pan.state == .ended {
ifshowView {
if pan.translation(in:view).x <-175 {
showView =false
} else {
showView =true
}
} else {
if pan.translation(in:view).x >50 {
showView =true
} else {
showView =false
}
}
}
}
}
func moveMenu(_ xoff:CGFloat) {
let view =UIApplication.shared.keyWindow?.subviews.first
let menuView =UIApplication.shared.keyWindow?.subviews.last
UIApplication.shared.keyWindow?.bringSubview(toFront: (UIApplication.shared.keyWindow?.subviews[1])!)
view?.transform =CGAffineTransform.init(translationX: xoff, y:0)
//menuView和view一样的行为
menuView?.transform = (view?.transform)!
}
func showMenu() {
let view =UIApplication.shared.keyWindow?.subviews.first
let menuView =UIApplication.shared.keyWindow?.subviews.last
UIApplication.shared.keyWindow?.bringSubview(toFront: (UIApplication.shared.keyWindow?.subviews[1])!)
UIView.animate(withDuration:0.5, animations: {
view?.transform =CGAffineTransform.init(translationX:225, y: 0)
menuView?.transform = (view?.transform)!
})
}
func dismissMenu() {
let view =UIApplication.shared.keyWindow?.subviews.first
let menuView =UIApplication.shared.keyWindow?.subviews.last
UIApplication.shared.keyWindow?.bringSubview(toFront: (UIApplication.shared.keyWindow?.subviews[1])!)
UIView.animate(withDuration:0.5, animations: {
view?.transform =CGAffineTransform.init(translationX:0, y: 0)
menuView?.transform = (view?.transform)!
})
}
}
- Swift:左侧滑动的判断,连动。
- Swift--禁止当前页面左侧滑动返回
- 多级的连动
- ListView添加headerview实现连动滑动
- 我的二级连动
- 简单的三级连动
- DataGridView的两级连动
- 连动
- 自定义导航栏的,使用左侧滑动
- Java与C/C++的连动
- 连动的数字编辑框
- Struts标签+javascript的省市连动
- Javascript实现日期的连动选择
- Javascript实现日期的连动选择
- JS连动下拉框的实现
- Java与C/C++的连动
- 下拉条的连动(36)
- 实现标题栏与viewPager的连动
- postgreSQL 统计语句
- 欢迎使用CSDN-markdown编辑器
- pycharm快捷键
- 数据结构——简介
- HTML中      等6种空白空格的区别
- Swift:左侧滑动的判断,连动。
- Linux命令大全----常用文件操作命令
- C++中堆和栈的完全解析
- 小萨的烦恼
- Elasticsearch集群无法自动集群处理
- React Native之瀑布流
- Java与对象
- 正则表达式
- Linux命令大全----系统管理相关命令