重点逻辑在于,实现动画过程给最上层的控制器添加一个view,来实现动画的返回功能。
class first: UIViewController {
//动画按钮
var aniBtn:UIButton?
//三个测试控制器
var three: Three?
var four: Three?
var two: Two?
//覆盖的view
private var rightCoverView: UIView!
//动画参数
var leftShowTransform: CGAffineTransform {
get {
return CGAffineTransformScale(CGAffineTransformMakeTranslation(SlideMaxOffset * 0.8, SlideMaxOffset * 0.8 * 0.2 ), 0.8, 0.8)
}
}
var leftHideTransform: CGAffineTransform {
get {
return CGAffineTransformScale(CGAffineTransformMakeTranslation(SlideMinOffset * 0.8, 0), 1.00, 1.00)
}
}
var SlideMaxOffset: CGFloat {
get {
return UIScreen.mainScreen().bounds.size.width * 0.7
}
}
var SlideMinOffset: CGFloat {
get {
return UIScreen.mainScreen().bounds.size.width * 0.0
}
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
print("touch")
}
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.blueColor()
self.setupSubview()
}
func setupSubview() {
two = Two()
self.view.addSubview(two!.view)
self.addChildViewController(two!)
three = {
let th = Three()
return th
}()
self.view.addSubview(three!.view)
self.addChildViewController(three!)
//核心,实现动画的过程给最上面的控制器添加一个view,来实现动画的返回功能
rightCoverView = {
let view = UIButton(frame: CGRectMake(0, 0, UIScreen.mainScreen().bounds.size.width, UIScreen.mainScreen().bounds.size.height))
let gesture = UITapGestureRecognizer(target: self, action: "hideLeft")
view.userInteractionEnabled = true
view.addGestureRecognizer(gesture)
view.backgroundColor = UIColor.greenColor()
return view
} ()
aniBtn = UIButton()
self.view.addSubview(aniBtn!)
aniBtn?.frame = CGRectMake(100, 100, 20, 20)
aniBtn?.backgroundColor = UIColor.redColor()
aniBtn?.userInteractionEnabled = true
aniBtn?.addTarget(self, action: "aniBtnClick", forControlEvents: .TouchDown)
}
func aniBtnClick (){
three!.view.addSubview(rightCoverView)
UIView.animateWithDuration(0.45, delay: 0, usingSpringWithDamping: 1.0, initialSpringVelocity: 0.45, options: .BeginFromCurrentState, animations: { () -> Void in
self.three?.view.transform = self.leftShowTransform
}) { (complete) -> Void in
print("回调成功")
}
}
func hideLeft() {
rightCoverView.removeFromSuperview()
UIView.animateWithDuration(0.45, delay: 0, usingSpringWithDamping: 1.0, initialSpringVelocity: 0.45, options: .BeginFromCurrentState, animations: { () -> Void in
self.three?.view.transform = self.leftHideTransform
}) { (complete) -> Void in
print("yes")
}
}
}