Code Block swiftfunc animateTransition(using transitionContext: UIViewControllerContextTransitioning) { |
|
let containerView = transitionContext.containerView |
guard let purchaseDetailVC = transitionContext.viewController(forKey: .to) as? PurchaseDetailViewController, |
let purchaseDetailView = transitionContext.view(forKey: .to) else { |
return |
} |
let toFrame = transitionContext.finalFrame(for: purchaseDetailVC) |
let dismissButton = UIButton() |
dismissButton.setBackgroundImage( imageLiteral(resourceName: "icon-close"), for: .normal) |
dismissButton.translatesAutoresizingMaskIntoConstraints = false |
|
let cardView: PurchaseView = PurchaseView(frame: .zero) |
cardView.translatesAutoresizingMaskIntoConstraints = false |
cardView.purchasable = fromCell.purchaseView.purchasable |
cardView.buyButtonState = fromCell.purchaseView.buyButtonState |
cardView.buyButton.titleLabel?.font = UIFont.preferredFont(forTextStyle: .title3).withWeight(.medium) |
|
cardView.addSubview(dismissButton) |
let buttonConstraints = [ |
dismissButton.topAnchor.constraint(equalTo: cardView.topAnchor, constant: 20), |
dismissButton.trailingAnchor.constraint(equalTo: cardView.trailingAnchor, constant: -20) |
] |
|
NSLayoutConstraint.activate(buttonConstraints) |
|
let detailBGView = UIView(frame: .zero) |
detailBGView.translatesAutoresizingMaskIntoConstraints = false |
|
detailBGView.backgroundColor = purchaseDetailVC.tableView.backgroundColor |
|
// Card view constraints |
containerView.addSubview(detailBGView) |
let bgVerticalConstraint = detailBGView.centerYAnchor.constraint(equalTo: containerView.centerYAnchor, constant: (fromFrame.height/2 + fromFrame.minY) - containerView.bounds.height/2) |
let bgConstraints = [ |
bgVerticalConstraint, |
detailBGView.centerXAnchor.constraint(equalTo: containerView.centerXAnchor) |
] |
|
NSLayoutConstraint.activate(bgConstraints) |
let bgWidthConstraint = detailBGView.widthAnchor.constraint(equalToConstant: fromFrame.width) |
let bgHeightConstraint = detailBGView.heightAnchor.constraint(equalToConstant: fromFrame.height) |
NSLayoutConstraint.activate([bgWidthConstraint, bgHeightConstraint]) |
containerView.addSubview(cardView) |
|
let verticalConstraint = cardView.centerYAnchor.constraint(equalTo: containerView.centerYAnchor, constant: (fromFrame.height/2 + fromFrame.minY) - containerView.bounds.height/2) |
let cardConstraints = [ |
verticalConstraint, |
cardView.centerXAnchor.constraint(equalTo: containerView.centerXAnchor) |
] |
|
NSLayoutConstraint.activate(cardConstraints) |
let cardWidthConstraint = cardView.widthAnchor.constraint(equalToConstant: fromFrame.width) |
let cardHeightConstraint = cardView.heightAnchor.constraint(equalToConstant: fromFrame.height) |
NSLayoutConstraint.activate([cardWidthConstraint, cardHeightConstraint]) |
|
cardView.cornerRadius = 12 |
cardView.cornerCurve = .continuous |
detailBGView.cornerRadius = 12 |
detailBGView.cornerCurve = .continuous |
|
fromCell.isHidden = true |
fromCell.transform = .identity |
containerView.layoutIfNeeded() |
springAnimator.addAnimations { |
bgVerticalConstraint.constant = toFrame.minY/2 |
verticalConstraint.constant = (purchaseDetailVC.headerHeight/2 - toFrame.height/2 + toFrame.minY/2) |
cardView.topConstraint.constant = purchaseDetailVC.purchaseView.topConstraint.constant |
containerView.layoutIfNeeded() |
} |
springAnimator.startAnimation() |
let expandingAnimator = UIViewPropertyAnimator(duration: springAnimator.duration * 0.6, curve: .linear) |
expandingAnimator.addAnimations { |
cardView.containerView.borderWidth = 0.0 |
cardView.buyButton.style = .onDarkBackground |
cardView.headlineHidden = true |
cardWidthConstraint.constant = toFrame.width |
cardHeightConstraint.constant = purchaseDetailVC.headerHeight |
bgWidthConstraint.constant = toFrame.width |
bgHeightConstraint.constant = toFrame.height |
// Must use layer because using view var breaks this for some reason |
cardView.layer.cornerRadius = 0.0 |
detailBGView.layer.cornerRadius = 0.0 |
containerView.layoutIfNeeded() |
} |
|
expandingAnimator.startAnimation() |
springAnimator.addCompletion { (_) in |
detailBGView.removeFromSuperview() |
cardView.removeFromSuperview() |
containerView.addSubview(purchaseDetailView) |
let success = !transitionContext.transitionWasCancelled |
transitionContext.completeTransition(success) |
} |
} |