Advanced iOS development-enables camera-like animation to be launched in a similar camera

Source: Internet
Author: User

If mobile access is poor, you can visit my personal blog

Recently wrote a photo album of the demo, occasionally saw the camera overload animation is very interesting, just want to add a special effect in my Photo album demo, the following my ideas and implementation process to share with you

First on: (Demo address)

Step analysis

This action looks very special but it is very simple to implement, only need to use CALayer and CAShapeLayer as a presentation layer, and then through CABasicAnimation the implementation of animation on the line ~

    • Use two CALayer to render a boot image
    • UIBezierPath CAShapeLayer to draw a cashapelayer with curves.
    • And then make the mask of the curve CAShapeLayer CALayer
    • Finally CABasicAnimation , by making a simple displacement animation
Draw the top half of the layer first
/** drawing the upper half of the layer * /Private Func Configtopshapelayer () {//Draw Besle curve let Topbezier:uibezierpath = Uibezierpath () topbezier. moveToPoint(Cgpointmake (-1, -1)) Topbezier. Addlinetopoint(Cgpointmake (bounds. Width+1, -1)) Topbezier. Addcurvetopoint(Cgpointmake (bounds. Width/2.0+1, bounds. Height/2.0+1), Controlpoint1:cgpointmake (bounds. Width+1,0+1), Controlpoint2:cgpointmake (343.5+1,242.5+1)) Topbezier. Addcurvetopoint(Cgpointmake (-1, bounds. Height+2), Controlpoint1:cgpointmake (31.5+2,424.5+2), Controlpoint2:cgpointmake (0+2, bounds. Height+2)) Topbezier. Addlinetopoint(Cgpointmake (-1, -1)) Topbezier. Closepath()//Create a cashapelayer that will draw the path of the Besle curve to cashapelayer let Topshape = Cashapelayer () topshape. Path= Topbezier. CgpathSet contents for Toplayer to start diagram Toplayer. Contents= Launchimage?. CgimageToplayer. Frame= Bounds Layer. Addsublayer(Toplayer)//will be painted Cashapelayer as Toplayer mask Toplayer. Mask= Topshape}

The result of the drawing is this:

Then draw the bottom half of the layer with the same principle
/** draw the bottom half of the layer * /Private Func Configbottomshapelayer () {//Draw Besle curve let Bottombezier:uibezierpath = Uibezierpath () Bott Ombezier. moveToPoint(Cgpointmake (bounds. Width,0)) Bottombezier. Addcurvetopoint(Cgpointmake (bounds. Width/2.0, bounds. Height/2.0), Controlpoint1:cgpointmake (bounds. Width,0), Controlpoint2:cgpointmake (343.5,242.5)) Bottombezier. Addcurvetopoint(Cgpointmake (0, bounds. Height), Controlpoint1:cgpointmake (31.5,424.5), Controlpoint2:cgpointmake (0, bounds. Height)) Bottombezier. Addlinetopoint(Cgpointmake (bounds. Width, bounds. Height)) Bottombezier. Addlinetopoint(Cgpointmake (bounds. Width,0)) Bottombezier. Closepath()//Create a cashapelayer that will draw the path of the Besle curve to cashapelayer let Bottomshape = Cashapelayer () bottomshape. Path= Bottombezier. CgpathSet contents for Bottomlayer to start diagram Bottomlayer. Contents= Launchimage?. CgimageBottomlayer. Frame= Bounds Layer. Addsublayer(Bottomlayer)//will be painted Cashapelayer as Bottomlayer mask Bottomlayer. Mask= Bottomshape}

It is noted here that the upper part of the Besle curve is to be shifted from 1 to 2 pixels in the overall downward direction, in order to prevent the jagged effect caused by the Besle curve drawing curve, following the bottom half:

Finally give two layer a displacement animation
/** Unfolding animation * /Func staranimation () {//Create a cabasicanimation that acts on Calayer AnchorpointLet topanimation = Cabasicanimation.init (keypath:"Anchorpoint")//Set Move pathTopanimation.tovalue = Nsvalue.init (Cgpoint:cgpointmake (1,1))//Animation timeTopanimation.duration =0.6        //Set up the agent to remove the current view after the animation is completeTopanimation.delegate = Self//Set animation speed to uniformTopanimation.timingfunction = Camediatimingfunction.init (name:kcamediatimingfunctionlinear)//Set the end of the animation without removing the point, keeping the position after the moveTopanimation.removedoncompletion =falseTopanimation.fillmode = Kcafillmodeforwards toplayer.addanimation (topanimation, Forkey:"Topanimation")//Create a cabasicanimation that acts on Calayer AnchorpointLet bottomanimation = Cabasicanimation.init (keypath:"Anchorpoint")//Set Move pathBottomanimation.tovalue = Nsvalue.init (Cgpoint:cgpointmake (0,0))//Animation timeBottomanimation.duration =0.6        //Set animation speed to uniformBottomanimation.timingfunction = Camediatimingfunction.init (name:kcamediatimingfunctionlinear)//Set the end of the animation without removing the point, keeping the position after the moveBottomanimation.removedoncompletion =falseBottomanimation.fillmode = Kcafillmodeforwards bottomlayer.addanimation (bottomanimation, ForKey:"Topanimation")    }/** The current view after the animation is complete * /InternalOverrideFunc animationdidstop (anim:caanimation, finished Flag:bool) {ifFlag {Removefromsuperview ()}}

Here for the convenience of observation, I will animate the time to become longer, the following is done:

Here this dynamic effect on the completion of almost, I hope you can learn something, if you have a better way to implement can also give me advice, I study, thank you to watch, additional demo address, like to pay attention to

Advanced iOS development-enables camera-like animation to be launched in a similar camera

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.