先上一张图片
今天又学习到了一个新的动画效果,就赶快拿出来和大家分享啦。
首先,将我Demo中的LLSlideMenu文件夹拖入到你的工程中。
然后在桥文件中import
#import "LLSlideMenu.h"
ViewController中调用
//
// ViewController.swift
// Swift-弹性侧滑菜单
//
// Created by ibokan on 16/7/27.
// Copyright © 2016年 张宇. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
/*创建一个菜单*/
var slideMenu:LLSlideMenu!
/*设置全屏侧滑手势*/
var leftSwipe:UIPanGestureRecognizer!
var percent:UIPercentDrivenInteractiveTransition!
/*菜单按钮监听*/
@IBAction func menu(sender: UIButton) {
if (slideMenu.ll_isOpen) {
slideMenu.ll_closeSlideMenu()
}else{
slideMenu.ll_openSlideMenu()
}
}
override func viewDidLoad() {
super.viewDidLoad()
/*初始化*/
slideMenu = LLSlideMenu()
/*菜单添加到视图上*/
self.view.addSubview(slideMenu)
/*设置菜单宽度*/
slideMenu.ll_menuWidth = 200
/*设置菜单背景色*/
slideMenu.ll_menuBackgroundColor = UIColor(red: 102/255, green: 153/255, blue: 204/255, alpha: 1)
/*设置弹力和速度,默认的是20,15,60*/
slideMenu.ll_springDamping = 20 //阻力
slideMenu.ll_springVelocity = 15 //速度
slideMenu.ll_springFramesNum = 60 //关键帧数量
/*
* Menu添加子View
*/
let img = UIImageView(frame: CGRectMake(50, 40, 80, 80))
img.image = UIImage(named: "002")
img.layer.masksToBounds = true
img.layer.cornerRadius = 40
img.layer.borderWidth = 1
img.layer.borderColor = UIColor.whiteColor().CGColor
slideMenu.addSubview(img)
let label = UILabel(frame: CGRectMake(30, 140, 150, 400))
label.text = "这是第一行菜单\n\n这是第二行菜单\n\n这是第三行菜单\n\n这是第四行菜单\n\n这是第五行菜单\n\n这是第六行菜单\n\n这是第七行菜单\n\n这是第八行菜单\n\n这是第九行菜单\n\n这是第十行菜单"
label.textColor = UIColor.whiteColor()
label.numberOfLines = 0
slideMenu.addSubview(label)
/*
* 添加全屏手势
*/
self.leftSwipe = UIPanGestureRecognizer(target: self, action: #selector(ViewController.swipeLeftHandle(_:)))
self.leftSwipe.maximumNumberOfTouches = 1
self.view.addGestureRecognizer(leftSwipe)
}
/**
* 全局侧滑手势监听
*/
func swipeLeftHandle(recognizer:UIScreenEdgePanGestureRecognizer){
//如果菜单已经打开就禁止滑动
if(slideMenu.ll_isOpen){
return
}
//计算手指滑的物理距离(滑了多远,与起始位置无关)
var progress:CGFloat = recognizer.translationInView(self.view).x / (self.view.bounds.size.width * 1.0)
//把这个百分比限制在 0~1 之间
progress = min(1.0, max(0.0, progress))
//当手势刚刚开始,我们创建一个 UIPercentDrivenInteractiveTransition 对象
if(recognizer.state == UIGestureRecognizerState.Began){
self.percent = UIPercentDrivenInteractiveTransition()
}else if(recognizer.state == UIGestureRecognizerState.Changed){
//当手慢慢划入时,我们把总体手势划入的进度告诉 UIPercentDrivenInteractiveTransition 对象。
self.percent.updateInteractiveTransition(progress)
slideMenu.ll_distance = recognizer.translationInView(self.view).x
}else if(recognizer.state == UIGestureRecognizerState.Cancelled || recognizer.state == UIGestureRecognizerState.Ended){
//当手势结束,我们根据用户的手势进度来判断过渡是应该完成还是取消并相应的调用 finishInteractiveTransition 或者 cancelInteractiveTransition 方法.
if(progress > 0.4){
self.percent.finishInteractiveTransition()
slideMenu.ll_openSlideMenu()
}else{
self.percent.cancelInteractiveTransition()
slideMenu.ll_closeSlideMenu()
}
self.percent = nil
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
喜欢的话,记得点击上方的关注按钮!