先上一个效果图:
2017-03-20 20_10_49.gif新建一个动画的类 实现UIViewControllerAnimatedTransitioning,CAAnimationDelegate
class WHLAnimatedTransition: NSObject {
weak var transitionContext : UIViewControllerContextTransitioning!
override init() {
super.init()
}
}
extension WHLAnimatedTransition : UIViewControllerAnimatedTransitioning,CAAnimationDelegate{
func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
return 0.5
}
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
self.transitionContext = transitionContext
//获得即将消失的view
let fromView = transitionContext.view(forKey: UITransitionContextViewKey.from)
let toView = transitionContext.view(forKey:UITransitionContextViewKey.to)
let containerView = transitionContext.containerView
containerView.addSubview(fromView!)
containerView.addSubview(toView!)
toView?.frame = CGRect(x:0,y:64,width:containerView.frame.width,height:containerView.frame.height)
transitionContext.completeTransition(true)
}
func animationDidStop(_ anim: CAAnimation, finished flag: Bool) {
//动画完成
self.transitionContext.completeTransition(true)
}
}
在控制器中实现UINavigationControllerDelegate的协议
var selectImageView : UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.delegate = self
createCollectionView()
selectImageView = UIImageView()
self.view.addSubview(selectImageView)
}
extension WHLClassifyViewController : UINavigationControllerDelegate{
func navigationController(_ navigationController: UINavigationController, didShow viewController: UIViewController, animated: Bool) {
if viewController != self {
self.selectImageView.frame = CGRect.null
}
}
func navigationController(_ navigationController: UINavigationController, animationControllerFor operation: UINavigationControllerOperation, from fromVC: UIViewController, to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
if operation == UINavigationControllerOperation.push {
toVC.edgesForExtendedLayout = UIRectEdge.bottom
let animatedTransition = WHLAnimatedTransition.init()
return animatedTransition
}
return nil
}
}
实现collectionView点击的代理方法时
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let cell = collectionView.cellForItem(at: indexPath)as! WHLClassifyCollectionViewCell
let rect = cell.frame
let cellRect = collectionView.convert(rect, to: collectionView.superview)
self.selectImageView.frame = CGRect(x:(cell.frame.origin.x),y:cellRect.origin.y,width:(cell.frame.width),height:(cell.frame.height))
self.selectImageView.image = cell.goodsImg.image
let detailVC = DetailViewController()
detailVC.goodsModel = [self.classifyModel[indexPath.item]]
UIView.animate(withDuration: 0.6, animations: {
self.selectImageView.frame = CGRect(x:0,y:64,width:SCREEN_WIDTH,height:300)
}, completion: {(_)in
self.navigationController?.pushViewController(detailVC, animated: true)
})
}
这样就欧啦!
网友评论