The idea is: Add a cagradientlayer to the layer of the view, add a mask at the same time, add cabasicanimation to the Cagradientlayer, and the white slide animation
1. Create an inheritance UIView subclass: Animatedmasklabel
Add Property
var gradientlayer = Cagradientlayer () let text:string = ' Slide to see ' Let textattributes: [String:anyobject] = { Let style = Nsmutableparagraphstyle () style.alignment =. Center return [ nsfontattributename:uifont (Name: "Helveticaneue-thin", size:25)!, Nsparagraphstyleattributename:style ] } ()
2. Set Gradientlayer properties: color, and corresponding position
Func Configuregradientlayer () { gradientlayer.startpoint = cgpointmake (0.0, 0.5) Gradientlayer.endpoint = Cgpointmake (1.0, 0.5) let colors = [ uicolor.blackcolor (). Cgcolor, Uicolor.whitecolor (). Cgcolor, Uicolor.blackcolor (). Cgcolor ] gradientlayer.colors = colors let locations = [ 0.25,0.5,0.75 ] Gradientlayer.locations = Locations }
Set Frame:
Override Func Layoutsubviews () { gradientlayer.frame = CGRectMake (-bounds.size.width, BOUNDS.ORIGIN.Y, 3 * Bounds.size.width, bounds.size.height) }
3. Add slide to see text
Func AddText () { uigraphicsbeginimagecontextwithoptions (frame.size, False, 0) text.drawinrect (Bounds, withattributes:textattributes) Let image = Uigraphicsgetimagefromcurrentimagecontext () Uigraphicsendimagecontext () let masklayer = Calayer () Masklayer.backgroundcolor = Uicolor.clearcolor (). Cgcolor masklayer.frame = Cgrectoffset (bounds, bounds.size.width, 0) masklayer.contents = image. Cgimage gradientlayer.mask = Masklayer }
Init is called when
Override Init (frame:cgrect) { super.init (frame:frame) self.configuregradientlayer () Self.addtext ( ) }
4. The main animation effect: Basicanimation
Override Func Didmovetowindow () { layer.addsublayer (gradientlayer) let gradientanimation = Cabasicanimation ( KeyPath: "Locations") Gradientanimation.fromvalue = [0.0,0.0,0.25] gradientanimation.tovalue = [ 0.75,1.0,1.0] gradientanimation.duration = 3.0 Gradientanimation.repeatcount = float.infinity Gradientlayer.addanimation (Gradientanimation, Forkey:nil) }
5. Add to Uiviewcontroller View
Override Func Viewdidload () { super.viewdidload () let Animatedlabel = Animatedmasklabel (Frame:cgrectmake (0, (View.frame.size.width, ()); Animatedlabel.backgroundcolor = Uicolor.graycolor () View.addsubview (Animatedlabel) }
Effect. Color is not very corresponding, can be adjusted appropriately
Imitate iphone slide boot animation