1 First definition
@interface Viewcontroller ()
{
cagradientlayer* _gridentlayer;
}
@end
2 implemented in Viewdidload
There is another subclass of Calayer Cagradientlayer, which is the function of drawing a gradient color on the layer, and then adding the following code to the Viewdidload ():
_gridentlayer = [[Cagradientlayer alloc] init];
_gridentlayer.bounds=cgrectmake (0, 0,self.backgroundview.frame.size.width, self.backgroundView.frame.size.height );
_gridentlayer.position=cgpointmake (SELF.BACKGROUNDVIEW.FRAME.SIZE.WIDTH/2, SELF.BACKGROUNDVIEW.FRAME.SIZE.HEIGHT/2);
Gradientlayer.colors = [
Uicolor.blackcolor (). Cgcolor,
Uicolor.whitecolor (). Cgcolor,
Uicolor.blackcolor (). Cgcolor
// ]
The code for the above two lines is to set the size and position of the layer
[_gridentlayer setstartpoint:cgpointmake (0, 0.5)];
[_gridentlayer setendpoint:cgpointmake (1, 0.5)];
/**
The Colors property type of Cagradientlayer is an array [anyobject], which means that we can implement multiple color gradients, and can specify the order of each color. However, in our example we only need two colors, but it is important to note that although there are only two colors, but the whole color gradient process There are three primary color points, that is black, white, black, so we in this array also need to follow the number of primary color points and order to add the corresponding colors, even if the color is the same.
Since we have set the three primary colors of the gradient, we need to set the position where the primary colors appear, and then add the following code:
*/
[_gridentlayer Setcolors:[nsarray arraywithobjects: (ID) [Uicolor blackcolor]. Cgcolor, (ID) [Uicolor whitecolor]. Cgcolor, (ID) [Uicolor blackcolor]. Cgcolor, Nil]];
[_gridentlayer setlocations:@[@0.2, @0.5,@0.8]];
[Self.backgroundView.layer Addsublayer:_gridentlayer];
}
-(void) Viewdidappear: (BOOL) animated{
[Self gradinetanimate];
}
-(void) gradinetanimate{
[Email protected] "Caoxiang";
_gridentlayer.mask=self.textlabel.layer;
First, create an animated instance of the locations type gradient, set the property of the Fromvalue property, which is the starting position, to [0, 0, 0.25], which means that before the animation begins, the position of the two primary colors, black and white, is at the front of the entire layer, The second black primary color is in the 0.25 position:
Cabasicanimation * animation=[cabasicanimation animationwithkeypath:@ "locations"];
[Animation setfromvalue:@[@0,@0,@0.25]];
[Animation settovalue:@[@0.75,@1,@1]];
And the end position tovalue, the white and the second black primary color position is set at the end of the entire layer, the first black primary color in the position of 0.75:
As you can see, the entire layer becomes black at this point. That is, the first black primary color moves from 0 to 0.75 in the entire animation, the white primary color moves from 0 to 1, and the second black primary color moves from 0.25 to 1. Then set the animation time to 2 seconds, the number of wireless repetitions, and finally add the gradient animation to the Gradientlayer. We call the animation method Gradientanimate () in the Viewdidappear () method, and the compilation runs to see the effect:
animation.duration=2;
Animation.repeatcount=huge;
[_gridentlayer addanimation:animation Forkey:nil];
[Email protected] "hahahahaha";
_gridentlayer.mask=self.textlabel.layer;
}
:
Apple Boot Classic Sliding animation