iOS 3D engine SceneKit Development (6)--scnaction

Source: Internet
Author: User

The previous two articles about rotation we use cabasicanimation to achieve the rotation of animation, in fact, in Scenekit, there is a more simple way to achieve some basic animation, that is scnaction, its execution object is Scnnode.

A simple example:

SCNAction *shipMoveAction = [SCNAction moveTo:SCNVector3Make(10,10,5) duration:4];[shipRotationNode runAction:shipMoveAction];

The above code is easy to understand Shiprotationnode animation moved to (10,10,5) This position, the time interval is 4s.

Let's take a brief look at Scnaction's main API:

+ (Scnaction *) Movebyx: (CGFloat) DeltaX y: (CGFloat) DeltaY Z: (CGFloat) Deltaz Duration: (Nstimeinterval) duration//How many distances to move node from X, Y, z+ (Scnaction *) Moveby: (SCNVECTOR3) Delta Duration: (Nstimeinterval) duration//Ibid., except that the incoming parameter is SCNVector3+ (Scnaction *) MoveTo: (SCNVECTOR3) Location Duration: (Nstimeinterval) duration//Move node to location+ (Scnaction *) Rotatebyx: (CGFloat) Xangle y: (CGFloat) Yangle Z: (CGFloat) Zangle Duration: (Nstimeinterval) duration//How many degrees to rotate node from x, Y, z+ (Scnaction *) Rotatetox: (CGFloat) Xangle y: (CGFloat) Yangle Z: (CGFloat) Zangle Duration: (Nstimeinterval) duration//Rotate node from x, Y and z to specified angle+ (Scnaction *) Rotatetox: (CGFloat) Xangle y: (CGFloat) Yangle Z: (CGFloat) Zangle Duration: (Nstimeinterval) Duration Shortestunitarc: (BOOL) Shortestunitarc//Ibid, the difference from the above method is that it is more shortestunitarc this parameter, bool value. //For example: we need to rotate a node from 0 degrees to 270 degrees,//If the SHORTESTUNITARC is set to no,node it will rotate clockwise to 270 degrees;//If setting Shortestunitarc to Yes,node rotates 90 degrees to 270 degrees counterclockwise,//Select the smallest rotation angle to rotate to a specific degree. The default is No. + (Scnaction *) Rotatebyangle: (CGFloat) Angle Aroundaxis: (SCNVECTOR3) axis duration: (Nstimeinterval) duration//Rotates the angle degree along a specific axis. The front rotation is rotated along the x, Y, z axes, all perpendicular to each other,//Have you ever thought about how to rotate node in the direction of the x-axis in the 45-degree angle? //This API everyone notice here,//Mentioned in the previous article is not x-z this plane rotation, will use this method in the following demo to solve. + (Scnaction *) Rotatetoaxisangle: (SCNVECTOR4) axisangle Duration: (Nstimeinterval) duration//SCNVector4 (X,y,z,angle) rotates along a specific axis to angle degrees. //Here to explain angle similar to π, if angle=2,//We can not understand to rotate to 2 degrees, but instead rotate to 2/π*180 degrees. + (Scnaction *) Scaleby: (CGFloat) Scale Duration: (Nstimeinterval) sec//Zoom Out (zoom in) how much+ (Scnaction *) Scaleto: (CGFloat) Scale Duration: (Nstimeinterval) sec//Zoom Out (zoom in) to how much+ (Scnaction *) Fadeinwithduration: (Nstimeinterval) sec//literal meaning can be understood, fade in. Gradually turning node opacity into 1.+ (Scnaction *) Fadeoutwithduration: (Nstimeinterval) sec//Fade out+ (Scnaction *) Fadeopacityby: (CGFloat) Factor Duration: (Nstimeinterval) sec//Change the opacity of node gradually to a specific value+ (Scnaction *) Fadeopacityto: (CGFloat) Opacity Duration: (Nstimeinterval) sec//Change the opacity of node to a specific value+ (Scnaction *) Hide//Hide node+ (Scnaction *) unhide//Show node+ (Scnaction *) Removefromparentnode//Remove node+ (Scnaction *) Playaudiosource: (Scnaudiosource *) source waitforcompletion: (BOOL) wait//Play audio. Waitforcompletion,bool value,//If the duration for Yes action is the length of the audio;//If no, duration is considered to be 0. //You can go to see the Scnaudioplayer API.+ (Scnaction *) Group: (Nsarray<scnaction *> *) actions//group is used to execute multiple scnaction concurrently+ (Scnaction *) Sequence: (Nsarray<scnaction *> *) actions//sequential execution of multiple scnaction, execution of the last scnaction after the execution of the next scnaction+ (Scnaction *) Repeataction: (scnaction *) Action count: (Nsuinteger) Count//A scnaction is executed count times+ (Scnaction *) Repeatactionforever: (scnaction *) action//Always execute a scnaction+ (Scnaction *) Waitforduration: (Nstimeinterval) sec//Delay scnaction, such as when multiple scnaction are executed in sequence order,//You can add a scnaction B to the middle of the scnaction a,c,//etc A after the execution, delay a while, then go to execute C+ (Scnaction *) Runblock: (void(^) (Scnnode *node)) Block//Custom scnaction, you can do some things in block+ (Scnaction *) Runblock: (void(^) (Scnnode *node)) Block Queue: (dispatch_queue_t) queue//In a specific queue, execute block+ (Scnaction *) Customactionwithduration: (Nstimeinterval) Seconds Actionblock: (void(^) (Scnnode *node,CGFloatElapsedTime)) block//The method used in mathematical rotation, when this scnaction is executed,//scenekit will repeatedly call Actionblock during this interval,//And pass the elapsed time to Actionblock+ (Scnaction *) Javascriptactionwithscript: (NSString*) Script Duration: (Nstimeinterval) seconds//Within a time interval, execute a piece of JavaScript code-(Scnaction *) reversedaction//Reverse an already created scnaction, well understood,//corresponds to the autoreverses attribute of cabasicanimation. Where to come, go back to where. @property(nonatomic)NstimeintervalDuration//scnaction Properties, time interval, real time interval affected by speed@property(nonatomic)CGFloatSpeed//scnaction properties, speed coefficients. Assuming that duration is 10, but speed is 2,//is twice times the speed of the previous, the actual duration is 5@property(nonatomic) Scnactiontimingmode Timingmode//scnaction Property, timing mode, has four constant values:Constantsscnactiontimingmodelinear//constant speedScnactiontimingmodeeasein//Start slowly, slowly speed upScnactiontimingmodeeaseout//Start fast, gradually slowScnactiontimingmodeeaseineaseout//Start slowly, through the middle of time to accelerate, and then slow down again

OK, once we know the API of Scnaction, we can solve the problem of the previous one: Add a spaceship and let it circle around the x-axis in a 45-degree direction.

API to use:

+ (SCNAction *)rotateByAngle:(CGFloat)angle                  aroundAxis:(SCNVector3)axis                    duration:(NSTimeInterval)duration

Final results:

The code is all in the demo, interested students can download

Https://github.com/pzhtpf/SceneKitRoationDemo

iOS 3D engine SceneKit Development (6)--scnaction

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.