Swift moves along the path animation to achieve a circle, curve, line, etc. path trajectory

Source: Internet
Author: User

Typically, animations that follow complex paths can be implemented with the help of key-frame animations (cakeyframeanimation). Because of the convenient path attribute provided by Cakeyframeanimation, we only need to set the appropriate path to it.

1, ready to work

First we add an orange square to the page, and then demonstrate the use of cakeyframeanimation by adding a trajectory animation to the square.


Import Uikit

Class Viewcontroller:uiviewcontroller {

var square:uiview!

Override Func Viewdidload () {
Super.viewdidload ()

Square = UIView (frame:cgrectmake (0, 0, 20, 20))
Square.backgroundcolor = Uicolor.orangecolor ()
Self.view.addSubview (Square)
}

Override Func didreceivememorywarning () {
Super.didreceivememorywarning ()
}
}

2, do circular motion

The following sample of Chinese blocks will be circled along a circular trajectory (played only once)
(1) through the Cgpathcreatewithellipseinrect () method, we create a circular cgpath as our key frame animation path.
(2) Set the Calculationmode to kcaanimationpaced, allowing the Core Animation to impose a constant speed on the object being driven, regardless of how long each segment of the path is.


Let CenterX = View.bounds.size.width/2
Let Boundingrect:cgrect = CGRectMake (centerX-75, 50, 150, 150)

Let orbit = Cakeyframeanimation (keypath: "Position")
Orbit.duration = 3
Orbit.path = Cgpathcreatewithellipseinrect (Boundingrect,nil)
Orbit.calculationmode = kcaanimationpaced

Square.layer.addAnimation (orbit,forkey: "Move")

3, keep the layer to show the status of animation after the execution

By default, the graphic is restored to the state before the animation executes. For example, the above example, the box moves a week later will run to the page to sit on the corner position. If you want the box to remain in the state after the animation has been performed, there are two ways:
(1) The code below the animation directly sets the square final position coordinates


Let CenterX = View.bounds.size.width/2
Let Boundingrect:cgrect = CGRectMake (centerX-75, 50, 150, 150)

Let orbit = Cakeyframeanimation (keypath: "Position")
Orbit.duration = 3
Orbit.path = Cgpathcreatewithellipseinrect (Boundingrect,nil)
Orbit.calculationmode = kcaanimationpaced
Square.layer.addAnimation (orbit,forkey: "Move")

The following code lets the square move in a week after the end position (otherwise it will return to its original position)
Square.layer.position = Cgpointmake (centerx+75, 125)

(2) The Removedoncompletion property of the animation is set to False,fillmode set to Kcafillmodeforwards.

Let CenterX = View.bounds.size.width/2
Let Boundingrect:cgrect = CGRectMake (centerX-75, 50, 150, 150)

Let orbit = Cakeyframeanimation (keypath: "Position")
Orbit.duration = 3
Orbit.path = Cgpathcreatewithellipseinrect (Boundingrect,nil)
Orbit.calculationmode = kcaanimationpaced
Orbit.removedoncompletion = False
Orbit.fillmode = Kcafillmodeforwards

Square.layer.addAnimation (orbit,forkey: "Move")

4, repeat the motion on the track.
The RepeatCount parameter can set the number of repetitions of the animation and set it to HUGE to allow the circular motion animation of the square to loop continuously.

Let CenterX = View.bounds.size.width/2
Let Boundingrect:cgrect = CGRectMake (centerX-75, 50, 150, 150)

Let orbit = Cakeyframeanimation (keypath: "Position")
Orbit.duration = 3
Orbit.path = Cgpathcreatewithellipseinrect (Boundingrect,nil)
Orbit.calculationmode = kcaanimationpaced
Orbit.repeatcount = HUGE

Square.layer.addAnimation (orbit,forkey: "Move")

5, let the squares rotate along the path

Setting the Rotationmode property to Kcaanimationrotateauto allows the square to rotate along the path, which is always toward the center of the square, regardless of where it is moved.


Let CenterX = View.bounds.size.width/2
Let Boundingrect:cgrect = CGRectMake (centerX-75, 50, 150, 150)

Let orbit = Cakeyframeanimation (keypath: "Position")
Orbit.duration = 3
Orbit.path = Cgpathcreatewithellipseinrect (Boundingrect,nil)
Orbit.calculationmode = kcaanimationpaced
Orbit.repeatcount = HUGE
Orbit.rotationmode = Kcaanimationrotateauto

Square.layer.addAnimation (orbit,forkey: "Move")

6, build a more complex path

Through the cgpathcreatemutable, we can combine many arcs, lines, etc. into a complex path, so that the square in this path movement.


Let CenterX = View.bounds.size.width/2
Create the transform for the transfer coordinates so we don't have to do the coordinates according to the actual display.
var transform:cgaffinetransform = cgaffinetransformmaketranslation (CenterX, 50);
Let path = Cgpathcreatemutable ()
Cgpathmovetopoint (path, &transform, 0, 0);
Cgpathaddlinetopoint (path, &transform, 0, 75);
Cgpathaddlinetopoint (Path, &transform, 75, 75);
Cgpathaddarc (path, &transform, 0, N, 0, cgfloat (1.5 * m_pi), false);

Let orbit = Cakeyframeanimation (keypath: "Position")
Orbit.duration = 3
Orbit.path = Path
Orbit.calculationmode = kcaanimationpaced
Orbit.repeatcount = HUGE
Orbit.rotationmode = Kcaanimationrotateauto

Square.layer.addAnimation (orbit,forkey: "Move")


7, using animation group to achieve a combination of multiple animations

Through the animation group (Caanimationgroup), we can combine multiple animations together to play simultaneously.
In the following example, the small square moves along the trajectory and spins quickly. (The square moves one week, itself rotates 100 times)


Rotate animation
Let rotateanimation = Cakeyframeanimation (keypath: "Transform.rotation.z")
Rotateanimation.values = [0,200*M_PI]

Trajectory animation path
Let CenterX = View.bounds.size.width/2
Create the transform for the transfer coordinates so we don't have to do the coordinates according to the actual display.
var transform:cgaffinetransform = cgaffinetransformmaketranslation (CenterX, 50);
Let path = Cgpathcreatemutable ()
Cgpathmovetopoint (path, &transform, 0, 0);
Cgpathaddlinetopoint (path, &transform, 0, 75);
Cgpathaddlinetopoint (Path, &transform, 75, 75);
Cgpathaddarc (path, &transform, 0, N, 0, cgfloat (1.5 * m_pi), false);

Trajectory animation
Let orbitanimation = Cakeyframeanimation (keypath: "Position")
Orbitanimation.path = Path
Orbitanimation.calculationmode = kcaanimationpaced

Combine two animations
Let Animationgroup = Caanimationgroup ()
Animationgroup.animations = [Rotateanimation, orbitanimation]
Animationgroup.duration = 4
Animationgroup.repeatcount = HUGE
Square.layer.addAnimation (animationgroup,forkey: "Move")

Original: http://www.hangge.com/blog/cache/detail_1072.html

Related Article

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.