這里是針對朋友提出如何寫代理的疑問,發(fā)現(xiàn)自己的文章絲毫沒有提到這個,在這里做出一個補充
如何寫協(xié)議
/// 設置呈現(xiàn)的轉(zhuǎn)場動畫的代理
protocol AnimatorPresentedDelegate : NSObjectProtocol{
/// 開始位置
func startRect(indexPath:IndexPath) -> CGRect
/// 結(jié)束位置
func endRect(indexPath:IndexPath) -> CGRect
/// 需要呈現(xiàn)的圖片控件
func imageView(indexPath:IndexPath) -> UIImageView
}
定義代理
var presentedDelegate : AnimatorPresentedDelegate?
其中一部分的調(diào)用代理方法
func animationForPresentedView(transitionContext: UIViewControllerContextTransitioning){
//用可選綁定進行代理和indexPath 的nil值校驗
guard let presentedDelegate = presentedDelegate,let indexPath = indexPath else {
return;
}
//取出彈出的view -- 強制解包
let presentedView = transitionContext.view(forKey:.to)!;
//將presentedView添加到containerView中
transitionContext.containerView.addSubview(presentedView);
//獲取執(zhí)行動畫的imageView,和開始坐標
let startRect = presentedDelegate.startRect(indexPath: indexPath);
let imageView = presentedDelegate.imageView(indexPath: indexPath);
//把imageView加到轉(zhuǎn)場上下文里面
transitionContext.containerView.addSubview(imageView);
//設置尺寸--也就是開始的位置的尺寸
imageView.frame = startRect;
//執(zhí)行動畫(目的:特殊漸變動畫) -- 由透明到不透明
presentedView.alpha = 0.0;
//設置containerView為黑色 -- 為了剛開始是看得到微博界面的BUG
transitionContext.containerView.backgroundColor = UIColor.black;
UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: {
//presentedView.alpha = 1.0;
//動畫過程中走到結(jié)束的坐標
imageView.frame = presentedDelegate.endRect(indexPath: indexPath);
}) { (_) in
//針對圖片模糊的BUG
imageView.removeFromSuperview();
//這個是防止重復的BUG
presentedView.alpha = 1.0;
transitionContext.completeTransition(true);
//執(zhí)行完之后要還原
transitionContext.containerView.backgroundColor = UIColor.clear;
}
}