Skip to content

Instantly share code, notes, and snippets.

@voxels
Last active August 2, 2018 17:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save voxels/e89587ecfa9a4445bb766e03cf27f120 to your computer and use it in GitHub Desktop.
Save voxels/e89587ecfa9a4445bb766e03cf27f120 to your computer and use it in GitHub Desktop.
An example of animateCollectionViews, a method to swap out two collection views located in the same container
// MARK: - Animation
extension GalleryViewController {
func animateCollectionViews(preview:Bool, with indexPath:IndexPath?) {
guard let oldCollectionView = collectionView, let collectionViewModel = oldCollectionView.model else {
return
}
let angle = CGFloat(Measurement(value: 90, unit: UnitAngle.degrees)
.converted(to: .radians).value)
closeButton.transform = preview ? CGAffineTransform.init(rotationAngle:angle) : .identity
closeButton.isHidden = false
var scrollToIndexPath = IndexPath(item: 0, section: 0)
if let visibleIndexPath = indexPath {
scrollToIndexPath = visibleIndexPath
}
let timingDuration:TimeInterval = 0.45 * (FeaturePolice.useSlowAnimation ? 4.0 : 1.0)
let layout = collectionViewLayout(for: preview ? .horizontal : .vertical, errorHandler: oldCollectionView.model?.resourceDelegate?.errorHandler)
let newCollectionView = galleryCollectionView(with: layout, collectionViewModel:collectionViewModel)
newCollectionView.backgroundColor = preview ? .black : .white
backingView.backgroundColor = newCollectionView.backgroundColor
contentContainerView.backgroundColor = newCollectionView.backgroundColor
headingContainerView.backgroundColor = view.backgroundColor
topContentContainerCoverView.isHidden = preview
bottomContentContainerCoverView.backgroundColor = preview ? UIColor.appDarkGrayBackground() : .white
coverCollectionShadowView.backgroundColor = preview ? UIColor.appDarkGrayBackground() : UIColor.appLightGrayBackground()
coverCollectionShadowView.layer.shadowOpacity = preview ? 0.0 : 0.1
var appearance = GalleryCollectionViewImageCellAppearance()
if preview {
appearance.backgroundColor = .black
} else {
appearance.backgroundColor = .white
}
appearance.fadeDuration = timingDuration + 0.25
newCollectionView.cellAppearance = appearance
newCollectionView.alpha = 1.0
contentContainerView.insertSubview(newCollectionView, at: 0)
self.collectionView = newCollectionView
setNeedsStatusBarAppearanceUpdate()
refreshLayout(in: view)
newCollectionView.scrollToItem(at: scrollToIndexPath, at: preview ? .centeredHorizontally : .centeredVertically, animated: false)
newCollectionView.transform = CGAffineTransform.init(scaleX: 0.85, y: 0.95)
let newCollectionAlphaAnimation = CABasicAnimation(keyPath: #keyPath(CALayer.opacity))
newCollectionAlphaAnimation.fromValue = 0.0
newCollectionAlphaAnimation.toValue = 1.0
let oldCollectionAlphaAnimation = CABasicAnimation(keyPath: #keyPath(CALayer.opacity))
oldCollectionAlphaAnimation.fromValue = 1.0
oldCollectionAlphaAnimation.toValue = 0.0
let headerColor = preview ? UIColor.appDarkGrayBackground() : UIColor.appLightGrayBackground()
headingContainerView.backgroundColor = headerColor
view.backgroundColor = headingContainerView.backgroundColor
let timingFunction = CAMediaTimingFunction(controlPoints: 0.45, -0.4, 0.20, 1.25)
CATransaction.begin()
CATransaction.setAnimationDuration(timingDuration)
CATransaction.setAnimationTimingFunction(timingFunction)
CATransaction.setCompletionBlock {
oldCollectionView.removeFromSuperview()
}
oldCollectionView.alpha = 0.0
oldCollectionView.layer.add(oldCollectionAlphaAnimation, forKey: #keyPath(CALayer.opacity))
newCollectionView.alpha = 1.0
newCollectionView.layer.add(newCollectionAlphaAnimation, forKey: #keyPath(CALayer.opacity))
UIView.animate(withDuration: timingDuration, animations: {
newCollectionView.transform = .identity
})
let bottomConstant:CGFloat = preview ? -15 : -60
previewContainerViewBottomConstraint.constant = bottomConstant
if preview {
for index in 1...4 {
if let previewViewButtonImageView = previewContainerView.subviews.first?.subviews.first?.viewWithTag(index) {
previewViewButtonImageView.alpha = 0.0
previewViewButtonImageView.transform = CGAffineTransform.init(scaleX: 0.25, y: 0.25)
}
}
}
view.setNeedsUpdateConstraints()
UIView.animate(withDuration: timingDuration) { [weak self] in
self?.view.layoutIfNeeded()
}
if preview {
closeButton.alpha = 0.5
UIView.animate(withDuration: timingDuration, animations: { [weak self] in
self?.headingLabel.alpha = 0.0
self?.headingLabel.transform = CGAffineTransform.init(scaleX: 0.5, y: 0.5)
self?.closeButton.transform = .identity
self?.closeButton.alpha = 1.0
}) { (didSucceed) in
if preview {
UIView.animate(withDuration: timingDuration / 2.0, animations: { [weak self] in
for index in 1...4 {
if let previewViewButtonImageView = self?.previewContainerView.subviews.first?.subviews.first?.viewWithTag(index) {
previewViewButtonImageView.alpha = 1.0
previewViewButtonImageView.transform = .identity
}
}
})
}
}
} else {
closeButton.alpha = 0.0
closeButton.isHidden = true
UIView.animate(withDuration: timingDuration, animations: { [weak self] in
self?.headingLabel.alpha = 1.0
self?.headingLabel.transform = .identity
self?.closeButton.transform = CGAffineTransform.init(rotationAngle:angle)
}) { (didSucceed) in
}
}
CATransaction.commit()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment