Skip to content

Instantly share code, notes, and snippets.

@zzz6519003
Created March 25, 2014 12:38
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 zzz6519003/9761010 to your computer and use it in GitHub Desktop.
Save zzz6519003/9761010 to your computer and use it in GitHub Desktop.
A simple category that creates a relection view for your UIImageView.
//
// UIImageView+Reflect.h
// UIImageView-Reflect
//
// Created by Snowmanzzz on 3/19/14.
// Copyright (c) 2014 zzz. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface UIImageView (Reflect)
- (void)reflect;
@end
//
// UIImageView+Reflect.m
// UIImageView-Reflect
//
// Created by Snowmanzzz on 3/19/14.
// Copyright (c) 2014 zzz. All rights reserved.
//
#import "UIImageView+Reflect.h"
@implementation UIImageView (Reflect)
- (void)reflect {
CGRect frame = self.frame;
frame.origin.y += (frame.size.height + 1);
UIImageView *reflectionImageView = [[UIImageView alloc] initWithFrame:frame];
self.clipsToBounds = TRUE;
reflectionImageView.contentMode = self.contentMode;
[reflectionImageView setImage:self.image];
reflectionImageView.transform = CGAffineTransformMakeScale(1.0, -1.0);
CALayer *reflectionLayer = [reflectionImageView layer];
CAGradientLayer *gradientLayer = [CAGradientLayer layer];
gradientLayer.bounds = reflectionLayer.bounds;
gradientLayer.position = CGPointMake(reflectionLayer.bounds.size.width / 2, reflectionLayer.bounds.size.height * 0.5);
gradientLayer.colors = [NSArray arrayWithObjects:
(id)[[UIColor clearColor] CGColor],
(id)[[UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.3] CGColor], nil];
gradientLayer.startPoint = CGPointMake(0.5,0.5);
gradientLayer.endPoint = CGPointMake(0.5,1.0);
reflectionLayer.mask = gradientLayer;
[self.superview addSubview:reflectionImageView];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment