iOS Development-uidynamic (physical emulation) simple to use

Source: Internet
Author: User

Uidynamic is a new technology introduced from IOS7, belonging to the Uikit framework, we can think of as a physics engine can simulate and simulate real-life physical phenomena, such as gravity, elastic collision and so on.

Allows developers to move away from the physical formula to achieve some physical simulation effects.

Here is a brief introduction to the use of gravity, collisions, and snaps.

Let's introduce the approximate steps to use

    • Create a physical emulator
    • Create the appropriate physical simulation behavior
    • Adding physical emulation behavior to the physical emulator

One, gravity and collision

Prepare two UIView in advance in storyboard, one red, one blue. Blue is below the red, ensuring that the red drops can touch the blue view.

1     //Create a physical emulator2Uidynamicanimator *animator =[[Uidynamicanimator alloc] initWithReferenceView:self.view];3Self.animator = animator;//Strong reference Animator, otherwise the code block will be freed after execution is complete4     5     //Create gravity behavior6Uigravitybehavior *gravitybehavior =[[Uigravitybehavior alloc] initwithitems:@[self.redview];7     8     //set some properties (can be set without setting it to default)9Gravitybehavior.gravitydirection = Cgvectormake (0,1);//Gravity DirectionTenGravitybehavior.angle = m_pi*0.5;//Gravity Direction OneGravitybehavior.magnitude =0.5;//gravitational acceleration, 1 for acceleration Yes (100 points per second) A      -     //adding the behavior of gravity behavior to the emulator - [animator Addbehavior:gravitybehavior]; the      -      -     //Create collision Behavior -Uicollisionbehavior *collisionbehavior =[[Uicollisionbehavior alloc] initwithitems:@[self.redview,self.blueview]; +      -     //sets the collision boundary, does not set the fly out the screen, the setting will have the collision effect at the screen border +Collisionbehavior.translatesreferenceboundsintoboundary =YES; A      at     //adding collision behavior to the physical emulator -[Animator Addbehavior:collisionbehavior];

After the code executes, you can see the Red view drop and hit the Blue view.

Second, capturing behavior

1- (void) Touchesbegan: (Nsset *) touches withevent: (Uievent *)Event2 {3     //Get Touch Point4Uitouch *touch =[touches anyobject];5Cgpoint point =[Touch LocationInView:touch.view];6     7     //Creating a simulation simulator8Uidynamicanimator *animator =[[Uidynamicanimator alloc] initWithReferenceView:self.view];9Self.animator = animator;//SimulatorTen      One     //Create snapping behavior AUisnapbehavior *snapbehavior =[[Uisnapbehavior alloc] InitWithItem:self.redView snaptopoint:point]; -      -     //set the bounce factor (the greater the bounce, the greater the value, the smaller the bounce amplitude) theSnapbehavior.damping =0.5; -      -     //adding behaviors to the emulator - [animator Addbehavior:snapbehavior]; +      -}

Run, click on the screen, you can find that the red view is very naughty to follow your finger move

iOS Development-uidynamic (physical emulation) simple to use

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.