swift 自定义Modal动画
2016-07-27 00:00
218 查看
摘要: swift 自定义Modal动画
// // PopoverAnimator.swift // DS11WB // // Created by xiaomage on 16/4/6. // Copyright © 2016年 小码哥. All rights reserved. // import UIKit class PopoverAnimator: NSObject { var isPresented : Bool = false } // MARK:- 自定义转场代理的方法 extension PopoverAnimator : UIViewControllerTransitioningDelegate { // 目的:改变弹出View的尺寸 func presentationControllerForPresentedViewController(presented: UIViewController, presentingViewController presenting: UIViewController, sourceViewController source: UIViewController) -> UIPresentationController? { return XMGPresentationController(presentedViewController: presented, presentingViewController: presenting) } // 目的:自定义弹出的动画 func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? { isPresented = true return self } // 目的:自定义消失的动画 func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? { isPresented = false return self } } // MARK:- 弹出和消失动画代理的方法 extension PopoverAnimator : UIViewControllerAnimatedTransitioning { /// 动画执行的时间 func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval { return 0.5 } /// 获取`转场的上下文`:可以通过转场上下文获取弹出的View和消失的View // UITransitionContextFromViewKey : 获取消失的View // UITransitionContextToViewKey : 获取弹出的View func animateTransition(transitionContext: UIViewControllerContextTransitioning) { isPresented ? animationForPresentedView(transitionContext) : animationForDismissedView(transitionContext) } /// 自定义弹出动画 private func animationForPresentedView(transitionContext: UIViewControllerContextTransitioning) { // 1.获取弹出的View let presentedView = transitionContext.viewForKey(UITransitionContextToViewKey)! // 2.将弹出的View添加到containerView中 transitionContext.containerView()?.addSubview(presentedView) // 3.执行动画 presentedView.transform = CGAffineTransformMakeScale(1.0, 0.0) presentedView.layer.anchorPoint = CGPointMake(0.5, 0) UIView.animateWithDuration(transitionDuration(transitionContext), animations: { () -> Void in presentedView.transform = CGAffineTransformIdentity }) { (_) -> Void in // 必须告诉转场上下文你已经完成动画 transitionContext.completeTransition(true) } } /// 自定义消失动画 private func animationForDismissedView(transitionContext: UIViewControllerContextTransitioning) { // 1.获取消失的View let dismissView = transitionContext.viewForKey(UITransitionContextFromViewKey) // 2.执行动画 UIView.animateWithDuration(transitionDuration(transitionContext), animations: { () -> Void in dismissView?.transform = CGAffineTransformMakeScale(1.0, 0.00001) }) { (_) -> Void in dismissView?.removeFromSuperview() // 必须告诉转场上下文你已经完成动画 transitionContext.completeTransition(true) } } }
相关文章推荐
- swift 闭包的使用
- swift 单例的使用
- swift3.0 hello swift(1)
- 一.swift和OC的比较
- iOS 的本地推送(swift实现)
- Swift 给图片添加水印
- 是时候适配 Swift 3 了吗——专访 LINE iOS 开发工程师王巍
- swift 版本 无限滚动Banner条
- tableviewcell 单个选中状态(swift)
- Swift 版本相册,防微信相册
- Swift基础(二十五)UIPageControl
- OC 与Swift混合开发之相互调用
- Swift weakSelf 在闭包语法
- Swift基础(二十四)UIScrollView
- Swift-Switch穿透
- Swift weakSelf写法
- swift - UIScrollView 的使用
- Xcode 8 beta iOS10 swift还是有这个错误提示
- 01-swift微博初始化项目
- swift使用cocoapods导入oc三方库