微博首页的popoverView
- 要是实现上面的popoverView效果,iPad开发的话本身有PopOverView,可是iPhone开发是没有这个控件的,需要自己来实现
- 解决办法:
- 1.添加一个UIWindow在最上层,再在该window里面添加UIView
- 2.自定义转场
自定义转场
- 1.给导航栏的按钮添加动作,让它modal出一个自定义的控制器(TYMPopoverViewController)
@objc private func navBtnClick(button:TYMNavBtn) {
// 创建控制器
let popoverSb = UIStoryboard(name: "TYMPopoverViewController", bundle: nil)
if let popoverVC = popoverSb.instantiateInitialViewController() {
// modal出一个控制器
presentViewController(popoverVC, animated: true, completion:nil)
}
}
// MARK: lazy
// 自定义一个转场代理者
private lazy var transManager:TYMTransitioningManager = {
let manager = TYMTransitioningManager()
// 设置弹出的内容的尺寸
manager.presentedViewFrame = CGRect(x: 100, y: 56, width: 200, height: 200)
return manager
}()
@objc private func navBtnClick(button:TYMNavBtn) {
// 创建控制器
let popoverSb = UIStoryboard(name: "TYMPopoverViewController", bundle: nil)
if let popoverVC = popoverSb.instantiateInitialViewController() {
// 1. 设置负责自定义转场代理
popoverVC.transitioningDelegate = transManager
// 2. 设置转场的样式
popoverVC.modalPresentationStyle = UIModalPresentationStyle.Custom
// modal出控制器
presentViewController(popoverVC, animated: true, completion:nil)
}
}
- 3.实现转场代理的方法(告诉系统谁负责转场)
- 3.1.创建一个基于UIPresentationController的类,用于创建负责转场的控制器
// 转场控制器
class TYMPresentationController: UIPresentationController {
override func containerViewWillLayoutSubviews() {
super.containerViewWillLayoutSubviews()
// 1.添加蒙版
containerView?.insertSubview(cover, atIndex: 0)
cover.frame = containerView!.bounds
// 设置弹出的内容的尺寸
presentedView()?.frame = CGRectMake(100, 56, 200, 200)
}
@objc func coverViewClick() {
presentedViewController.dismissViewControllerAnimated(true, completion: nil)
}
// MARK: 懒加载
// 蒙版
private lazy var cover:UIButton = {
let btn = UIButton()
btn.addTarget(self, action: Selector("coverViewClick"), forControlEvents: UIControlEvents.TouchUpInside)
return btn
}()
}
class TYMTransitioningManager: NSObject, UIViewControllerTransitioningDelegate, UIViewControllerAnimatedTransitioning {
// 定义标记,标记是否展开
private lazy var isPresented = true
var presentedViewFrame: CGRect = CGRectZero
// MARK: UIViewControllerTransitioningDelegate
// 该方法用于返回,负责转场的对象
func presentationControllerForPresentedViewController(presented: UIViewController, presentingViewController presenting: UIViewController, sourceViewController source: UIViewController) -> UIPresentationController? {
let presentCtr = TYMPresentationController(presentedViewController: presented, presentingViewController: presenting)
presentCtr.presentedViewFrame = presentedViewFrame
return presentCtr
}
// 告诉系统谁来负责转场如何出现,出现的同时发送一个通知给控制器
func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning?
{
isPresented = true
NSNotificationCenter.defaultCenter().postNotificationName(popoverAnimationDidShow, object: self, userInfo: nil)
return self
}
// 告诉系统谁来负责转场如何消失, 消失的同时发送一个通知给控制器
func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning?
{
isPresented = false
NSNotificationCenter.defaultCenter().postNotificationName(popoverAnimationDidDismiss, object: self, userInfo: nil)
return self
}
- 4.让转场管理者遵守UIViewControllerAnimatedTransitioning协议,实现转场动画方法
// 无论转场出现还是消失都会调用这个方法,我们需要在这里,自定义转场动画的呈现样式
// transitionContext 上下文
func animateTransition(transitionContext: UIViewControllerContextTransitioning)
{
if isPresented { // 显示转场
// 1.获得被展示的view
let toView = transitionContext.viewForKey(UITransitionContextToViewKey)!
// 2.将需要展示的view添加到containerView中
transitionContext.containerView()!.addSubview(toView)
// 3.设置被展示的view,如何显示和消失
toView.transform = CGAffineTransformMakeScale(1.0, 0.0)
UIView.animateWithDuration(0.5, animations: { () -> Void in
toView.transform = CGAffineTransformIdentity
}) { (_) -> Void in
// 如果是自定义转场,一定要在动画执行后告诉系统,动画执行完毕了,否则有可能引发位置错误
transitionContext.completeTransition(true)
}
} else { // 转场消失
let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey)
// 注意: 消失动画一下子就不见了的原因是因为CGFloat是不准确的
// 想解决这个问题, 只需要将y的CGFloat的值改为一个非常小得值即可
UIView.animateWithDuration(0.5, animations: { () -> Void in
fromView?.transform = CGAffineTransformMakeScale(1.0, 0.0001)
}, completion: { (_) -> Void in
// 如果是自定义转场,一定要在动画执行后告诉系统,动画执行完毕了,否则有可能引发位置错误
transitionContext.completeTransition(true)
})
}
}
// 监听标题按钮发出的通知
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("titleBtnChange"), name: popoverAnimationDidShow, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("titleBtnChange"), name: popoverAnimationDidDismiss, object: nil)
// MARK: 监听
@objc private func titleBtnChange() {
// 修改标题按钮的箭头
titleBtn.selected = !titleBtn.selected
}