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