Gradient transition Natural
Cagradientlayer *layer = [Cagradientlayer layer];
Layer.frame = CGRectMake (0, 0, curw-10,44);
Layer.colors = [Nsarray arraywithobjects: (ID) [Uicolor clearcolor]. Cgcolor, (ID) [Uicolor graycolor]. Cgcolor, nil];
For (Calayer *sublayer in [Self.bgView.layer sublayers])
{
[Sublayer Removefromsuperlayer];
}
[Self.bgView.layer Insertsublayer:layer atindex:0];
Returns the image of the gradient
-(uiimage*) Gradientimagefromcolors: (nsarray*) Colors Bygradienttype: (gradienttype) GradientType inSize: (CGSize) size{
Nsmutablearray *ar = [Nsmutablearray array];
For (Uicolor *c in colors) {
[AR addobject: (ID) c.cgcolor];
}
Uigraphicsbeginimagecontextwithoptions (Size, YES, 1);
Cgcontextref context = Uigraphicsgetcurrentcontext ();
Cgcontextsavegstate (context);
Cgcolorspaceref colorspace = Cgcolorgetcolorspace ([[Colors Lastobject] cgcolor]);
Cggradientref gradient = Cggradientcreatewithcolors (ColorSpace, (cfarrayref) AR, NULL);
Cgpoint start;
Cgpoint end;
Switch (GradientType) {
Up and down gradients
Case 0:
Start = Cgpointmake (0.0, 0.0);
End = Cgpointmake (0.0, size.height);
Break
Case 1:
Left and right gradients
Start = Cgpointmake (0.0, 0.0);
End = Cgpointmake (size.width, 0.0);
Break
Case 2:
Diagonal side gradients
Start = Cgpointmake (0.0, 0.0);
End = Cgpointmake (Size.width, size.height);
Break
Case 3:
Diagonal side gradients
Start = Cgpointmake (size.width, 0.0);
End = Cgpointmake (0.0, size.height);
Break
Case 4:
Linear gradient
Start = Cgpointmake (SIZE.WIDTH/2, SIZE.HEIGHT/2);
End = Cgpointmake (SIZE.WIDTH/2, SIZE.HEIGHT/2);
Break
Default
Break
}
Cgcontextdrawlineargradient (context, gradient, start, end, Kcggradientdrawsbeforestartlocation | Kcggradientdrawsafterendlocation);
if (GradientType = = 4) {
Cgcontextdrawradialgradient (context, gradient, start, ten, End, SIZE.WIDTH/3, Kcggradientdrawsbeforestartlocation | Kcggradientdrawsafterendlocation);
}
UIImage *image = Uigraphicsgetimagefromcurrentimagecontext ();
Cggradientrelease (gradient);
Cgcontextrestoregstate (context);
Cgcolorspacerelease (ColorSpace);
Uigraphicsendimagecontext ();
return image;
}
IOS View Color Gradient