What is the most efficient way to add a reflection to a UIImageView
I just want the easiest way to make a reflection under a UIImageVies that is开发者_开发知识库 easily managable.
Just use the sample code in the the iPhone SDK library
Update: Link now updated to new location
As Phil says, you can have a "reflected" UIImageView instance:
@interface ReflectedImageView : UIView
{
@private
UIImageView *_imageView;
UIImageView *_imageReflectionView;
}
@property (nonatomic, retain) UIImage *image;
@end
And then, in your implementation, something like this
@implementation ReflectedImageView
@dynamic image;
- (id)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame])
{
self.backgroundColor = [UIColor clearColor];
// This should be the size of your image:
CGRect rect = CGRectMake(0.0, 0.0, 320.0, 290.0);
_imageReflectionView = [[UIImageView alloc] initWithFrame:rect];
_imageReflectionView.contentMode = UIViewContentModeScaleAspectFit;
_imageReflectionView.alpha = 0.4;
_imageReflectionView.transform = CGAffineTransformMake(1, 0, 0, -1, 0, 290.0);
[self addSubview:_imageReflectionView];
_imageView = [[UIImageView alloc] initWithFrame:rect];
_imageView.contentMode = UIViewContentModeScaleAspectFit;
[self addSubview:_imageView];
}
return self;
}
- (void)setImage:(UIImage *)newImage
{
_imageView.image = newImage;
_imageReflectionView.image = newImage;
}
- (UIImage *)image
{
return _imageView.image;
}
- (void)dealloc
{
[_imageView release];
[_imageReflectionView release];
[super dealloc];
}
@end
I wrote an article on how to generate reflections of any UI element or group of elements. The canned technique can be dropped into your project and is really easy to use. Source code and the article can be found at http://aptogo.co.uk/2011/08/no-fuss-reflections/
The easiest way is to do it by hand in Photoshop! (seriously - if that's practical just do that).
Otherwise you'll need to make an inverted copy of the image (or at least the bottom half) to place below the real one, overlaid with a gradient from black (assuming your background is black) with alpha=1 to alpha=0.
If you need to place it over arbitrary backgrounds it's a little more complex as you'll have to apply the gradient from alpha = 0 to alpha = 1 to your inverted image. I knocked up some code once to do it - will try and dig it out later when I get to my Mac, if nobody else has come up with anything sooner.
精彩评论