Last active
September 14, 2016 15:42
-
-
Save CanTheAlmighty/8b226118a1aea03738e8d24334c3abe6 to your computer and use it in GitHub Desktop.
Layout that applies a rotational transform on scrolling
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
override func layoutAttributesForItemAtIndexPath(indexPath: NSIndexPath) -> UICollectionViewLayoutAttributes? | |
{ | |
guard let collectionView = collectionView else { return nil } | |
let attributes = OnboardingTutorialAttributes(forCellWithIndexPath: indexPath) | |
// Bounds independant of scroll offset | |
let bounds = CGRect(origin: CGPoint.zero, size: collectionView.frame.size) | |
var frame : CGRect = CGRect.zero | |
// Get the offset | |
let offset = delegate?.cellPhoneOffset ?? Defaults.PhoneOffset | |
// Size comes from the delegate | |
frame.size = delegate?.cellPhoneSize ?? Defaults.PhoneSize | |
// Position centered horizontally | |
frame.origin.x = bounds.size.width * (CGFloat(indexPath.item) + 0.5) - frame.size.width/2.0 + offset.horizontal | |
// Position centered vertically too | |
frame.origin.y = (bounds.size.height - frame.size.height)/2.0 + offset.vertical + 32 | |
// Animate transformation | |
var transform = CGAffineTransformIdentity | |
if (appliesTransform) | |
{ | |
// Normalized delta | |
// = 0 when the view is fully visible | |
let delta = (collectionView.bounds.origin.x - bounds.size.width * CGFloat(indexPath.item)) / frame.size.width | |
transform = CGAffineTransformMakeRotation(-delta * CGFloat(M_PI_2) / 8.0) | |
} | |
attributes.frame = frame | |
attributes.transform = transform | |
attributes.zIndex = ZIndex.Phone.rawValue | |
return attributes | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment