Created
August 7, 2013 04:44
-
-
Save kleneway/6171239 to your computer and use it in GitHub Desktop.
Animated Shadow on UICollectionViewCell
This file contains hidden or 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
| /** | |
| * Code to add an animated shadow to a UICollectionViewCell | |
| */ | |
| // | |
| // HAIMainMenuCollectionViewCell.m | |
| // app-haikudeck | |
| // | |
| // Created by Kevin Leneway | |
| // definitions for shadow values for main menu | |
| #define kMainMenuImageShadowOpacity 0.08f | |
| #define kMainMenuCellShadowOpacity 0.0f | |
| #define kMainMenuImageShadowOffsetY 2.0f | |
| -(void)setupShadow { | |
| // set up the initial soft grey shadow directly under the cells | |
| self.imageView.layer.shadowColor = [UIColor blackColor].CGColor; | |
| self.imageView.layer.shadowOpacity = kMainMenuImageShadowOpacity; | |
| self.imageView.layer.shadowOffset = CGSizeMake(0.0f, 0.0f); | |
| self.imageView.layer.shadowRadius = 3.0f; | |
| self.imageView.layer.masksToBounds = NO; | |
| CGSize size = self.imageView.bounds.size; | |
| CGRect ovalRect = CGRectMake(5.0f, size.height, size.width-15.0f, 10.0f); | |
| UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:ovalRect]; | |
| self.imageView.layer.shadowPath = path.CGPath; | |
| } | |
| -(void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes | |
| { | |
| [super applyLayoutAttributes:layoutAttributes]; | |
| self.layer.shouldRasterize = YES; | |
| if ([layoutAttributes isKindOfClass:[HAIMainMenuCollectionViewLayoutAttributes class]]) | |
| { | |
| self.imageView.layer.shadowOpacity = [(HAIMainMenuCollectionViewLayoutAttributes *)layoutAttributes imageShadowOpacity]; | |
| self.imageView.layer.shadowOffset = CGSizeMake(0.0f, [(HAIMainMenuCollectionViewLayoutAttributes *)layoutAttributes imageShadowOffsetY]); | |
| } | |
| } | |
| // | |
| // HAIMainMenuCollectionViewFlowLayout.m | |
| // app-haikudeck | |
| -(void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)attributes forVisibleRect:(CGRect)visibleRect | |
| { | |
| // Skip supplementary views | |
| if (attributes.representedElementKind) { | |
| return; | |
| } | |
| CGFloat imageShadowOffsetY = kMainMenuImageShadowOffsetY; | |
| CGFloat imageShadowOpacity = kMainMenuImageShadowOpacity; | |
| CGFloat distanceFromVisibleRectToItem = CGRectGetMidX(visibleRect) - attributes.center.x; | |
| CGFloat normalizedDistance = distanceFromVisibleRectToItem / (kMainMenuCellWidth*.25); | |
| CGAffineTransform transform = CGAffineTransformIdentity; | |
| // calculate the zoom and shadow properties for the selected item | |
| if (fabsf(distanceFromVisibleRectToItem) < kMainMenuCellWidth * .25) { | |
| CGFloat zoom = 1 + .2*(1 - ABS(normalizedDistance)); | |
| transform = CGAffineTransformScale(CGAffineTransformIdentity, zoom, zoom); | |
| imageShadowOffsetY = kMainMenuImageShadowOffsetY + 15.0f*(1 - ABS(normalizedDistance)); | |
| imageShadowOpacity = kMainMenuImageShadowOpacity + 0.15f*(1 - ABS(normalizedDistance)); | |
| } | |
| attributes.transform = transform; | |
| [(HAIMainMenuCollectionViewLayoutAttributes *)attributes setImageShadowOffsetY:imageShadowOffsetY]; | |
| [(HAIMainMenuCollectionViewLayoutAttributes *)attributes setImageShadowOpacity:imageShadowOpacity]; | |
| } | |
| // | |
| // HAIMainMenuCollectionViewLayoutAttributes.h | |
| // app-haikudeck | |
| // | |
| @interface HAIMainMenuCollectionViewLayoutAttributes : UICollectionViewLayoutAttributes | |
| @property (nonatomic) CGFloat imageShadowOpacity; | |
| @property (nonatomic) CGFloat imageShadowOffsetY; | |
| @end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment