Skip to content

Instantly share code, notes, and snippets.

View SamStone92's full-sized avatar

Sam Stone SamStone92

  • Gloucester, UK
View GitHub Profile
class CustomInteractor : UIPercentDrivenInteractiveTransition {
var navigationController : UINavigationController
var shouldCompleteTransition = false
var transitionInProgress = false
init?(attachTo viewController : UIViewController) {
if let nav = viewController.navigationController {
self.navigationController = nav
super.init()
func navigationController(_ navigationController: UINavigationController, animationControllerFor operation: UINavigationControllerOperation, from fromVC: UIViewController, to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
guard let frame = self.selectedFrame else { return nil }
guard let song = self.selectedSong else { return nil }
switch operation {
case .push:
return CustomAnimator(duration: TimeInterval(UINavigationControllerHideShowBarDuration), isPresenting: true, originFrame: frame, image: song.artwork)
default:
return CustomAnimator(duration: TimeInterval(UINavigationControllerHideShowBarDuration), isPresenting: false, originFrame: frame, image: song.artwork)
}
func navigationController(_ navigationController: UINavigationController, animationControllerFor operation: UINavigationControllerOperation, from fromVC: UIViewController, to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
self.selectedSong = songs[indexPath.row]
let theAttributes:UICollectionViewLayoutAttributes! = collectionView.layoutAttributesForItem(at: indexPath)
selectedFrame = collectionView.convert(theAttributes.frame, to: collectionView.superview)
self.performSegue(withIdentifier: "Player", sender: self)
}
self.navigationController?.delegate = self
class CustomAnimator : NSObject, UIViewControllerAnimatedTransitioning {
var duration : TimeInterval
var isPresenting : Bool
var originFrame : CGRect
var image : UIImage
public let CustomAnimatorTag = 99
init(duration : TimeInterval, isPresenting : Bool, originFrame : CGRect, image : UIImage) {
self.duration = duration
UIView.animate(withDuration: duration, animations: {
transitionImageView.frame = self.isPresenting ? artwork.frame : self.originFrame
detailView.frame = self.isPresenting ? fromView.frame : CGRect(x: toView.frame.width, y: 0, width: toView.frame.width, height: toView.frame.height)
detailView.alpha = self.isPresenting ? 1 : 0
}, completion: { (finished) in
transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
transitionImageView.removeFromSuperview()
artwork.alpha = 1
})
let transitionImageView = UIImageView(frame: isPresenting ? originFrame : artwork.frame)
transitionImageView.image = image
container.addSubview(transitionImageView)
guard let artwork = detailView.viewWithTag(CustomAnimatorTag) as? UIImageView else { return }
artwork.image = image
artwork.alpha = 0
public let CustomAnimatorTag = 99