Add a reflection effect to a view

Source: Internet
Author: User

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];
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.