给视图加上倒影效果
给视图加上倒影效果
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
const CGFloat kReflectPercent = -0.25f;const CGFloat kReflectOpacity = 0.3f;const CGFloat kReflectDistance = 10.0f;+ (void) addSimpleReflectionToView: (UIView *) theView{ CALayer *reflectionLayer = [CALayer layer]; reflectionLayer.contents = [theView layer].contents; reflectionLayer.opacity = kReflectOpacity; reflectionLayer.frame = CGRectMake(0.0f, 0.0f, theView.frame.size.width, theView.frame.size.height * kReflectPercent); CATransform3D stransform = CATransform3DMakeScale(1.0f, -1.0f, 1.0f); CATransform3D transform = CATransform3DTranslate(stransform, 0.0f, -(kReflectDistance + theView.frame.size.height), 0.0f); reflectionLayer.transform = transform; reflectionLayer.sublayerTransform = reflectionLayer.transform; [[theView layer] addSublayer:reflectionLayer];} |
给视图加上颜色渐变的背景
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
@interface ColorGradientView : NSView{ NSColor *startingColor; NSColor *endingColor; int angle;} // Define the variables as properties@property(nonatomic, retain) NSColor *startingColor;@property(nonatomic, retain) NSColor *endingColor;@property(assign) int angle; @end @implementation ColorGradientView // Automatically create accessor methods@synthesize startingColor;@synthesize endingColor;@synthesize angle; - (id)initWithFrame:(NSRect)frame;{ if (self = [super initWithFrame:frame]) { [self setStartingColor:[NSColor colorWithCalibratedWhite:1.0 alpha:1.0]]; [self setEndingColor:nil]; [self setAngle:270]; } return self;} - (void)drawRect:(NSRect)rect;{ if (endingColor == nil || [startingColor isEqual:endingColor]) { // Fill view with a standard background color [startingColor set]; NSRectFill(rect); } else { // Fill view with a top-down gradient // from startingColor to endingColor NSGradient* aGradient = [[[NSGradient alloc] initWithStartingColor:startingColor endingColor:endingColor] autorelease]; [aGradient drawInRect:[self bounds] angle:angle]; }} - (void)setStartingColor:(NSColor *)newColor;{ [startingColor autorelease]; startingColor = [newColor retain]; [self setNeedsDisplay:YES];} - (void)setEndingColor:(NSColor *)newColor;{ [endingColor autorelease]; endingColor = [newColor retain]; [self setNeedsDisplay:YES];} - (void)dealloc;{ [self setStartingColor:nil]; [self setEndingColor:nil]; [super dealloc];} @end |
-
一个效果更好的视图倒影效果,其倒影的影像会在其底部逐渐消失。带有渐隐效果的倒影方案首先要把视图内容复制到一个缩短到位图中,然后应用一个渐变蒙版。然后这些效果作为一个UIImage返回并被添加到原始的视图中,成为一个新的UIImageCiew。
要使这种倒影有效,必须要禁用视图的剪裁功能,即将视图的clipsToView属性设置为NO。123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657+ (CGImageRef)createGradientImage: (CGSize)size{CGFloatcolors[] = {0.0,1.0,1.0,1.0};// Create gradient in gray device color spaceCGColorSpaceRefcolorSpace = CGColorSpaceCreateDeviceGray();CGContextRefcontext = CGBitmapContextCreate(nil, size.width,size.height,8,0, colorSpace, kCGImageAlphaNone);CGGradientRefgradient =CGGradientCreateWithColorComponents(colorSpace, colors,NULL,2);CGColorSpaceRelease(colorSpace);// Draw the linear gradientCGPointp1=CGPointZero;CGPointp2= CGPointMake(0, size.height);CGContextDrawLinearGradient(context, gradient, p1, p2,kCGGradientDrawsAfterEndLocation);// Return the CGImageCGImageReftheCGImage = CGBitmapContextCreateImage(context);CFRelease(gradient);CGContextRelease(context);returntheCGImage;}// Create a shrunken frame for the reflection+ (UIImage*)reflectionOfView: (UIView*)theViewwithPercent: (CGFloat) percent{// Retain the width but shrink the heightCGSizesize = CGSizeMake(theView.frame.size.width,theView.frame.size.height* percent);// Shrink the viewUIGraphicsBeginImageContext(size);CGContextRefcontext = UIGraphicsGetCurrentContext();[theView.layerrenderInContext:context];UIImage*partialimg =UIGraphicsGetImageFromCurrentImageContext();UIGraphicsEndImageContext();// build the maskCGImageRefmask = [ImageHelpercreateGradientImage:size];CGImageRefref = CGImageCreateWithMask(partialimg.CGImage, mask);UIImage*theImage = [UIImageimageWithCGImage:ref];CGImageRelease(ref);CGImageRelease(mask);returntheImage;}constCGFloatkReflectDistance =10.0f;+ (void)addReflectionToView: (UIView*) theView{theView.clipsToBounds=NO;UIImageView*reflection = [[UIImageViewalloc]initWithImage:[ImageHelperreflectionOfView:theViewwithPercent:0.45f]];CGRectframe = reflection.frame;frame.origin= CGPointMake(0.0f, theView.frame.size.height+kReflectDistance);reflection.frame= frame;// add the reflection as a simple subview[theViewaddSubview:reflection];[reflectionrelease];}
浙公网安备 33010602011771号