最近看一个app的源码,发现基本没有用第三方的开源组件,但是特效也做得不错,总结一下实现的思路
简单的抽屉效果
效果如图:
这种抽屉效果很常见,开源组件也很多。但是一般开源组件都对Controller的结构有要求,有时候不是很方便。
原理主要是:主页面加侧边栏。当弹出侧边栏时,设置主页面的x为一个负数;当收回侧边栏时,将主页面的x设置为0。再加上一些动画和手势就可以了。
初始化侧边栏
func addSidePanelController() {
if (sidePanelController == nil) {
sidePanelController = UIStoryboard.deviceListPanelController() // 初始化侧边栏的controller,用storyboard或者代码不是核心
view.insertSubview(sidePanelController!.view, atIndex: 0) // 添加侧边栏view
sidePanelController!.view.frame = CGRectMake(
expandedOffset,
topLayoutGuide.length,
view.bounds.width - expandedOffset,
view.bounds.height - topLayoutGuide.length) // 设置侧边栏frame
addChildViewController(sidePanelController!) // 添加侧边栏controller为子controller
sidePanelController!.didMoveToParentViewController(self)
}
}
处理弹出和收回