微博首頁的popoverView
- 要是實現(xiàn)上面的popoverView效果蒂培,iPad開發(fā)的話本身有PopOverView戏羽,可是iPhone開發(fā)是沒有這個控件的递礼,需要自己來實現(xiàn)
- 解決辦法:
- 1.添加一個UIWindow在最上層怯疤,再在該window里面添加UIView
- 2.自定義轉(zhuǎn)場
自定義轉(zhuǎn)場
- 1.給導(dǎo)航欄的按鈕添加動作食呻,讓它modal出一個自定義的控制器(TYMPopoverViewController)
@objc private func navBtnClick(button:TYMNavBtn) {
// 創(chuàng)建控制器
let popoverSb = UIStoryboard(name: "TYMPopoverViewController", bundle: nil)
if let popoverVC = popoverSb.instantiateInitialViewController() {
// modal出一個控制器
presentViewController(popoverVC, animated: true, completion:nil)
}
}
- 2.設(shè)置轉(zhuǎn)場控制器的代理和轉(zhuǎn)場的樣式
// MARK: lazy
// 自定義一個轉(zhuǎn)場代理者
private lazy var transManager:TYMTransitioningManager = {
let manager = TYMTransitioningManager()
// 設(shè)置彈出的內(nèi)容的尺寸
manager.presentedViewFrame = CGRect(x: 100, y: 56, width: 200, height: 200)
return manager
}()
@objc private func navBtnClick(button:TYMNavBtn) {
// 創(chuàng)建控制器
let popoverSb = UIStoryboard(name: "TYMPopoverViewController", bundle: nil)
if let popoverVC = popoverSb.instantiateInitialViewController() {
// 1. 設(shè)置負(fù)責(zé)自定義轉(zhuǎn)場代理
popoverVC.transitioningDelegate = transManager
// 2. 設(shè)置轉(zhuǎn)場的樣式
popoverVC.modalPresentationStyle = UIModalPresentationStyle.Custom
// modal出控制器
presentViewController(popoverVC, animated: true, completion:nil)
}
}
- 3.實現(xiàn)轉(zhuǎn)場代理的方法(告訴系統(tǒng)誰負(fù)責(zé)轉(zhuǎn)場)
- 3.1.創(chuàng)建一個基于UIPresentationController的類友雳,用于創(chuàng)建負(fù)責(zé)轉(zhuǎn)場的控制器
// 轉(zhuǎn)場控制器
class TYMPresentationController: UIPresentationController {
override func containerViewWillLayoutSubviews() {
super.containerViewWillLayoutSubviews()
// 1.添加蒙版
containerView?.insertSubview(cover, atIndex: 0)
cover.frame = containerView!.bounds
// 設(shè)置彈出的內(nèi)容的尺寸
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 {
// 定義標(biāo)記稿湿,標(biāo)記是否展開
private lazy var isPresented = true
var presentedViewFrame: CGRect = CGRectZero
// MARK: UIViewControllerTransitioningDelegate
// 該方法用于返回,負(fù)責(zé)轉(zhuǎn)場的對象
func presentationControllerForPresentedViewController(presented: UIViewController, presentingViewController presenting: UIViewController, sourceViewController source: UIViewController) -> UIPresentationController? {
let presentCtr = TYMPresentationController(presentedViewController: presented, presentingViewController: presenting)
presentCtr.presentedViewFrame = presentedViewFrame
return presentCtr
}
// 告訴系統(tǒng)誰來負(fù)責(zé)轉(zhuǎn)場如何出現(xiàn)押赊,出現(xiàn)的同時發(fā)送一個通知給控制器
func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning?
{
isPresented = true
NSNotificationCenter.defaultCenter().postNotificationName(popoverAnimationDidShow, object: self, userInfo: nil)
return self
}
// 告訴系統(tǒng)誰來負(fù)責(zé)轉(zhuǎn)場如何消失, 消失的同時發(fā)送一個通知給控制器
func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning?
{
isPresented = false
NSNotificationCenter.defaultCenter().postNotificationName(popoverAnimationDidDismiss, object: self, userInfo: nil)
return self
}
- 4.讓轉(zhuǎn)場管理者遵守UIViewControllerAnimatedTransitioning協(xié)議饺藤,實現(xiàn)轉(zhuǎn)場動畫方法
// 無論轉(zhuǎn)場出現(xiàn)還是消失都會調(diào)用這個方法,我們需要在這里流礁,自定義轉(zhuǎn)場動畫的呈現(xiàn)樣式
// transitionContext 上下文
func animateTransition(transitionContext: UIViewControllerContextTransitioning)
{
if isPresented { // 顯示轉(zhuǎn)場
// 1.獲得被展示的view
let toView = transitionContext.viewForKey(UITransitionContextToViewKey)!
// 2.將需要展示的view添加到containerView中
transitionContext.containerView()!.addSubview(toView)
// 3.設(shè)置被展示的view涕俗,如何顯示和消失
toView.transform = CGAffineTransformMakeScale(1.0, 0.0)
UIView.animateWithDuration(0.5, animations: { () -> Void in
toView.transform = CGAffineTransformIdentity
}) { (_) -> Void in
// 如果是自定義轉(zhuǎn)場,一定要在動畫執(zhí)行后告訴系統(tǒng)神帅,動畫執(zhí)行完畢了,否則有可能引發(fā)位置錯誤
transitionContext.completeTransition(true)
}
} else { // 轉(zhuǎn)場消失
let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey)
// 注意: 消失動畫一下子就不見了的原因是因為CGFloat是不準(zhǔn)確的
// 想解決這個問題, 只需要將y的CGFloat的值改為一個非常小得值即可
UIView.animateWithDuration(0.5, animations: { () -> Void in
fromView?.transform = CGAffineTransformMakeScale(1.0, 0.0001)
}, completion: { (_) -> Void in
// 如果是自定義轉(zhuǎn)場再姑,一定要在動畫執(zhí)行后告訴系統(tǒng),動畫執(zhí)行完畢了,否則有可能引發(fā)位置錯誤
transitionContext.completeTransition(true)
})
}
}
// 監(jiān)聽標(biāo)題按鈕發(fā)出的通知
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("titleBtnChange"), name: popoverAnimationDidShow, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("titleBtnChange"), name: popoverAnimationDidDismiss, object: nil)
// MARK: 監(jiān)聽
@objc private func titleBtnChange() {
// 修改標(biāo)題按鈕的箭頭
titleBtn.selected = !titleBtn.selected
}