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];
}
Use core graphics to create reflection:
+ (Cgimageref) creategradientimage: (cgsize) Size
{
Cgfloat colors [] = {0.0, 1.0, 1.0, 1.0 };
// Create Gradient in gray device color space
Cgcolorspaceref colorspace = cgcolorspacecreatedevicegray ();
Cgcontextref context = cgbitmapcontextcreate (nil, size. Width,
Size. Height, 8, 0, colorspace, kcgimagealphanone );
Cggradientref gradient =
Cggradientcreatewithcolorcomponents (colorspace, colors,
Null, 2 );
Cgcolorspacerelstrap (colorspace );
// Draw the linear gradient
Cgpoint p1 = cgpointzero;
Cgpoint P2 = cgpointmake (0, size. Height );
Cgcontextdrawlineargradient (context, gradient, P1, P2,
Kcggradientdrawsafterendlocation );
// Return the cgimage
Cgimageref thecgimage = cgbitmapcontextcreateimage (context );
Cfrelease (gradient );
Cgcontextrelease (context );
Return thecgimage;
}
// Create a shrunken frame for the reflection
+ (Uiimage *) reflectionofview: (uiview *) theview
Withpercent: (cgfloat) percent
{
// Retain the width but shrink the height
Cgsize size = cgsizemake (theview. Frame. Size. Width,
Theview. Frame. Size. Height * percent );
// Shrink the view
Uigraphicsbeginimagecontext (size );
Cgcontextref context = uigraphicsgetcurrentcontext ();
[Theview. layer renderincontext: Context];
Uiimage * partialimg =
Uigraphicsgetimagefromcurrentimagecontext ();
Uigraphicsendimagecontext ();
// Build the mask
Cgimageref mask = [imagehelper creategradientimage: Size];
Cgimageref ref = cgimagecreatewithmask (partialimg. cgimage, mask );
Uiimage * theimage = [uiimage imagewithcgimage: ref];
Cgimagerelease (REF );
Cgimagerelease (mask );
Return theimage;
}
Const cgfloat kreflectdistance = 10.0f;
+ (Void) addreflectiontoview: (uiview *) theview
{
Theview. clipstobounds = no;
Uiimageview * reflection = [[uiimageview alloc] initwithimage:
[Imagehelper reflectionofview: theview withpercent: 0.45f];
Cgrect frame = reflection. frame;
Frame. Origin = cgpointmake (0.0f, theview. Frame. Size. height +
Kreflectdistance );
Reflection. Frame = frame;
// Add the reflection as a simple subview
[Theview addsubview: Reflection];
[Reflection release];
}