IOS development-UIDynamic (Physical Simulation) is simple to use, ios-uidynamic

Source: Internet
Author: User

IOS development-UIDynamic (Physical Simulation) is simple to use, ios-uidynamic

UIDynamic is a new technology introduced from IOS7 and is affiliated with the UIKit framework. We can think of it as a physical engine that can simulate and simulate physical phenomena in real life, such as gravity and elastic collision.

Developers can achieve some physical simulation effects without using physical formulas.

Here we will briefly introduce the usage of gravity, collision, and capturing.

First, let's take a look at the general steps.

  • Create a Physical Simulator
  • Create corresponding physical simulation Behavior
  • Add the physical simulation behavior to the Physical Simulator

 

1. Gravity and collision

Prepare two uiviews, one red and one blue, on the StoryBoard in advance. Blue is under the red to ensure that the red falls into the Blue view.

 

1 // create a physical simulator 2 UIDynamicAnimator * animator = [[UIDynamicAnimator alloc] initWithReferenceView: self. view]; 3 self. animator = animator; // a strong reference to animator. Otherwise, the code block will be released 4 5 after execution. // create a gravity behavior 6 UIGravityBehavior * gravityBehavior = [[UIGravityBehavior alloc] initWithItems: @ [self. redView]; 7 8 // you can set some attributes (if not set, they are all default) 9 gravityBehavior. gravityDirection = CGVectorMake (0, 1); // The gravity direction is 10 gravityBehavior. angle = M_PI * 0.5; // gravity direction 11 gravityBehavior. magn1_= 0.5; // gravity acceleration. 1 represents the acceleration (100 points per second) 12 13 // Add the gravity behavior to the simulator 14 [animator addBehavior: gravityBehavior]; 15 16 17 // create a collision behavior 18 UICollisionBehavior * collisionBehavior = [[UICollisionBehavior alloc] initWithItems: @ [self. redView, self. blueView]; 19 20 // sets the collision boundary. If this boundary is not set, the screen will fly out, and the setting will generate the collision effect 21 collisionBehavior at the screen border. translatesreferencebounds=boundary = YES; 22 23 // Add the collision behavior to the physical simulator 24 [animator addBehavior: collisionBehavior];

After the code is executed, you can see the red view falling and hitting the Blue View.

 

Ii. Capture Behaviors

1-(void) touchesBegan :( NSSet *) touches withEvent :( UIEvent *) event 2 {3 // obtain the touch point 4 UITouch * touch = [touches anyObject]; 5 CGPoint point = [touch locationInView: touch. view]; 6 7 // create a simulation simulator 8 UIDynamicAnimator * animator = [[UIDynamicAnimator alloc] initWithReferenceView: self. view]; 9 self. animator = animator; // simulator 10 11 // create capture behavior 12 UISnapBehavior * snapBehavior = [[UISnapBehavior alloc] initWithItem: self. redView snapToPoint: point]; 13 14 // set the rebound coefficient (the larger the rebound margin, the larger the value, the smaller the rebound margin) 15 snapBehavior. damping = 0.5; 16 17 // Add behavior to simulator 18 [animator addBehavior: snapBehavior]; 19 20}

Click the screen and you will find that the red view is very naughty and moves with your fingers.

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.