View fade, transparency gradient

Source: Internet
Author: User

1 Function description

  Development often encounter such demand: View2 show on View1 above, through View2 can gradually see view1. Effect 1: View1 is a imageview,view2 is a normal view. View1 and view2 completely overlap, view2 from bottom to top from gray to white, from opaque to transparent. Figure 2 is the only view2 effect, and Figure 3 is the only view1 effect.

The 2 code is as follows

  You can set Alpha to 1.0 if you want only the color gradient and you don't need a gradient for transparency.

1- (void) Viewdidload {2 [Super Viewdidload];3Uiimageview *imgview =[[Uiimageview alloc] initWithFrame:self.view.bounds];4Imgview.image = [UIImage imagenamed:@"Sun"];5 [Self.view Addsubview:imgview];6     7UIView *gradview =[[UIView alloc] initWithFrame:self.view.bounds];8Cagradientlayer *layer =[self getGradientWithFrame:gradView.bounds];9[Gradview.layer insertsublayer:layer Atindex:0];Ten [Self.view Addsubview:gradview]; One } A  --(Cagradientlayer *) Getgradientwithframe: (CGRect) Frame { -     intColoronev = -; the     intColortwov = -; -     intColorthreev = -; -     //set the color and opacity alpha, and set Alpha to 1.0 if you want to only fade and not want to be transparent.  -Uicolor *colorone = [Uicolor colorwithred: (coloronev/255.0) Green: (coloronev/255.0) Blue: (coloronev/255.0) Alpha:0.0]; +Uicolor *colortwo = [Uicolor colorwithred: (colortwov/255.0) Green: (colortwov/255.0) Blue: (colortwov/255.0) Alpha:0.5]; -Uicolor *colorthree = [Uicolor colorwithred: (colorthreev/255.0) Green: (colorthreev/255.0) Blue: (colorthreev/255.0) Alpha:1.0]; +Nsarray *colors = [Nsarray arraywithobjects: (ID) Colorone.cgcolor, Colortwo.cgcolor, Colorthree.cgcolor, nil]; ANSNumber *stopone = [NSNumber numberwithfloat:0.0]; atNSNumber *stoptwo = [NSNumber numberwithfloat:0.5]; -NSNumber *stopthree = [NSNumber numberwithfloat:1.0]; -Nsarray *locations =[Nsarray Arraywithobjects:stopone, Stoptwo, Stopthree, nil]; -     //Gradient Layer -Cagradientlayer *layer =[Cagradientlayer layer]; -Layer.colors =colors; inLayer.locations =locations; -Layer.frame =frame; to     returnlayer; +}

View fade, transparency gradient

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.