IOS View Color Gradient

Source: Internet
Author: User

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

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.