Imitate iphone slide boot animation

Source: Internet
Author: User

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

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.