再造 “手机QQ” 侧滑菜单(一)——实现侧滑效果
2015-05-29 09:20
597 查看
本系列文章中,我们将尝试再造手机QQ的侧滑菜单,力争最大限度接近手Q的实际效果,并使用 Auto Layout 仿造左侧菜单,实现和主视图的联动。
代码示例:https://github.com/johnlui/SwiftSideslipLikeQQ
最终效果:
![](http://cc.cocimg.com/api/uploads/20150527/1432718156724554.gif)
开发环境
本系列文章的开发环境为:
OS X 10.10.3
Xcode Version 6.3 (6D570)
基本数据采集
初步体验,手Q采用的应该是线性动画,即缩放比例等随着手指滑动的距离以一次方程的形式变化。动画达到最大幅度时截图如下(4.7 寸):
![](http://cc.cocimg.com/api/uploads/20150527/1432718258349570.jpg)
提取基本数据:
右侧主视图左边界距离屏幕左边界的距离占屏幕宽度的比例为:78%
右侧主视图的高度占屏幕高度的比例为:77%
找出线性关系
1. 比例与手指移动距离的关系
字比较丑 o(╯□╰)o。注意:式(1)中的 x 表示“手指移动距离”这个变量,和上面图中表示屏幕宽度的 x 意义不同。
![](http://cc.cocimg.com/api/uploads/20150527/1432718274371675.jpg)
2. 矩形中心向右移动距离和手指移动距离相等
实现侧滑
1. 新建项目,在 StoryBoard 中新增一个 View Controller,并新增一个名为 HomeViewController 的 UIViewController 类,并在 StoryBoard 中完成绑定。
2. 给 HomeViewController 设置背景颜色以示区分。也可以像我一样设一个大 Label 作为更明显的区分。
![](http://cc.cocimg.com/api/uploads/20150527/1432718308690352.jpg)
3. 给 HomeViewController 拖放一个 UIPanGestureRecognizer 并绑定到代码。
从右下角拖一个 Pan Gesture Recognizer 到主窗体上,这一步会让它与 HomeViewController.view 自动绑定。下图为第二步,绑定到代码。
![](http://cc.cocimg.com/api/uploads/20150527/1432718318294556.jpg)
4. 编写代码实现效果:
新建 Common.swift,存储屏幕宽度、高度:
![](http://cc.cocimg.com/api/uploads/20150527/1432718385397643.gif)
下一步:再造 “手机QQ” 侧滑菜单(二)——高仿左视图
代码示例:https://github.com/johnlui/SwiftSideslipLikeQQ
最终效果:
![](http://cc.cocimg.com/api/uploads/20150527/1432718156724554.gif)
开发环境
本系列文章的开发环境为:
OS X 10.10.3
Xcode Version 6.3 (6D570)
基本数据采集
初步体验,手Q采用的应该是线性动画,即缩放比例等随着手指滑动的距离以一次方程的形式变化。动画达到最大幅度时截图如下(4.7 寸):
![](http://cc.cocimg.com/api/uploads/20150527/1432718258349570.jpg)
提取基本数据:
右侧主视图左边界距离屏幕左边界的距离占屏幕宽度的比例为:78%
右侧主视图的高度占屏幕高度的比例为:77%
找出线性关系
1. 比例与手指移动距离的关系
字比较丑 o(╯□╰)o。注意:式(1)中的 x 表示“手指移动距离”这个变量,和上面图中表示屏幕宽度的 x 意义不同。
![](http://cc.cocimg.com/api/uploads/20150527/1432718274371675.jpg)
2. 矩形中心向右移动距离和手指移动距离相等
实现侧滑
1. 新建项目,在 StoryBoard 中新增一个 View Controller,并新增一个名为 HomeViewController 的 UIViewController 类,并在 StoryBoard 中完成绑定。
2. 给 HomeViewController 设置背景颜色以示区分。也可以像我一样设一个大 Label 作为更明显的区分。
![](http://cc.cocimg.com/api/uploads/20150527/1432718308690352.jpg)
3. 给 HomeViewController 拖放一个 UIPanGestureRecognizer 并绑定到代码。
从右下角拖一个 Pan Gesture Recognizer 到主窗体上,这一步会让它与 HomeViewController.view 自动绑定。下图为第二步,绑定到代码。
![](http://cc.cocimg.com/api/uploads/20150527/1432718318294556.jpg)
4. 编写代码实现效果:
新建 Common.swift,存储屏幕宽度、高度:
import UIKit struct Common { static let screenWidth = UIScreen.mainScreen().applicationFrame.maxX static let screenHeight = UIScreen.mainScreen().applicationFrame.maxY }修改 ViewController:
import UIKit class ViewController: UIViewController { var homeViewController: HomeViewController! var distance: CGFloat = 0 let FullDistance: CGFloat = 0.78 let Proportion: CGFloat = 0.77 override func viewDidLoad() { super.viewDidLoad() // 给主视图设置背景 let imageView = UIImageView(image: UIImage(named: "back")) imageView.frame = UIScreen.mainScreen().bounds self.view.addSubview(imageView) // 通过 StoryBoard 取出 HomeViewController 的 view,放在背景视图上面 homeViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("HomeViewController") as! HomeViewController self.view.addSubview(homeViewController.view) // 绑定 UIPanGestureRecognizer homeViewController.panGesture.addTarget(self, action: Selector("pan:")) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } // 响应 UIPanGestureRecognizer 事件 func pan(recongnizer: UIPanGestureRecognizer) { let x = recongnizer.translationInView(self.view).x let trueDistance = distance + x // 实时距离 // 如果 UIPanGestureRecognizer 结束,则激活自动停靠 if recongnizer.state == UIGestureRecognizerState.Ended { if trueDistance > Common.screenWidth * (Proportion / 3) { showLeft() } else if trueDistance < Common.screenWidth * -(Proportion / 3) { showRight() } else { showHome() } return } // 计算缩放比例 var proportion: CGFloat = recongnizer.view!.frame.origin.x >= 0 ? -1 : 1 proportion *= trueDistance / Common.screenWidth proportion *= 1 - Proportion proportion /= 0.6 proportion += 1 if proportion Void in self.homeViewController.view.center = CGPointMake(self.view.center.x + self.distance, self.view.center.y) self.homeViewController.view.transform = CGAffineTransformScale(CGAffineTransformIdentity, proportion, proportion) }, completion: nil) } }5. 查看效果
![](http://cc.cocimg.com/api/uploads/20150527/1432718385397643.gif)
下一步:再造 “手机QQ” 侧滑菜单(二)——高仿左视图
相关文章推荐
- 5,29 求 a的 b次方的最后三位数
- 使用js如何判断IE浏览器的位数
- mysql 中 时间和日期函数
- Chun 阅读笔记 - 如何做一个图片缓存库
- Tomcat源码阅读系列(四)Connector连接器
- [积累]GridView未设置margin、padding容器内出现空隙有关问题
- linux下的程序内存分布 代码段,数据段,BSS段,.rodata段,堆,栈
- 关于authCode的oc实现,以及原理探讨。
- JQuery控制Radio选中方法分析
- python中的关于列表for循环和条件判断的混用
- 代码格式
- 5,29 求a的b次方 的最后三位数
- JPA注解-@SequenceGenerator
- 【iOS-cocos2d-X 游戏开发之一】在Mac下结合Xcode搭建Cocos2d-X开发环境!
- Android利用ViewFlipper实现屏幕切换动画效果
- 解读ASP.NET 5 & MVC6系列(14):View Component
- [Selenium]计算坐标进行拖拽,重写dragAndDropOffset
- c:out输出和直接El输出区别
- leetcode 216: Combination Sum III
- Using a loopback device for storage  使用环回设备用于存储