iOS Development uidynamic

Source: Internet
Author: User

1 , overview

What is Uidynamic?

Uidynamic is a new technology introduced from iOS 7 and belongs to the Uikit framework.

Can be thought of as a physics engine that can simulate and emulate physical phenomena in real life. For example: gravity, elastic collisions and other phenomena.

The value of the physics engine:

(1) widely used in game development, the classic success story is "Angry Birds"

(2) enables developers to achieve cool physical simulations without the physics formula

(3) Improve the game development efficiency, produce more excellent fun physics simulation game

Well-known 2D physics engine:

Box2d

Chipmunk

2 , use steps

To use uidynamic to achieve physical simulation, the following steps are outlined:

First step: Create a physical emulator (by the way, set the simulation scope).

Step Two: Create the appropriate physical simulation behavior (by adding physical simulation elements).

Step three: Add the physical simulation behavior to the physical emulator à start the simulation.

3 , three major concepts

Physical emulation Element (Dynamic Item)

Who wants to perform physical simulations?

Attention:

(1) Not all objects can do physical simulation elements

(2) Not all objects can be physically emulated

These objects can be made into physical simulation elements:

(1) Any object that complies with the Uidynamicitem agreement

(2) UIView has complied with the Uidynamicitem protocol by default, so any UI control can do a physical simulation

(3) Uicollectionviewlayoutattributes class default also complies with Uidynamicitem protocol

Physical simulation Behavior (Dynamic Behavior)

What kind of physical simulation effect does it perform? What kind of animated effect?

The uidynamic provides several physical simulation behaviors:

Uigravitybehavior: Gravity Behavior

Uicollisionbehavior: Collision Behavior

Uisnapbehavior: Snapping behavior

Uipushbehavior: Driving behavior

Uiattachmentbehavior: Adhesion behavior

Uidynamicitembehavior: Dynamic Elemental Behavior

Physical Simulation Behavior Notice:

(1) All of the above physical simulation behaviors are inherited from Uidynamicbehavior

(2) All uidynamicbehavior can be carried out independently.

(3) When combining multiple behaviors, you can achieve some more complex effects.

Physical Emulator (Dynamic Animator)

Allow physical simulation elements to perform specific physical simulation behavior

Physical Emulator notes:

(1) It allows physical simulation elements to perform physical simulation behavior

(2) It is an object of type Uidynamicanimator

Initialization of the Uidynamicanimator:

-(Instancetype) Initwithreferenceview: (UIView *) view;

View parameter: is a reference view that represents the scope of a physical simulation

Uidynamicanimator Common methods of:

-(void) Addbehavior: (Uidynamicbehavior *) behavior;

Add 1 physical emulation behavior

-(void) Removebehavior: (Uidynamicbehavior *) behavior;

Remove 1 Physical simulation behaviors

-(void) removeallbehaviors;

Remove any physical emulation behavior that was previously added

Uidynamicanimator Common Properties for:

@property (nonatomic, readonly) uiview* Referenceview;

Reference view

@property (nonatomic, readonly, copy) nsarray* behaviors;

All physical emulation behaviors added to the physical emulator

@property (Nonatomic, readonly, getter = isrunning) BOOL running;

Whether physical emulation is in progress

@property (nonatomic, assign) ID <UIDynamicAnimatorDelegate> delegate;

Proxy object (a simulation process that listens to a physical emulator, such as Start and end)

4 , gravity behavior ( Uigravityanimator )

Brief introduction:

Given the direction of gravity and acceleration, the object drops in the direction of gravity.

Uigravitybehavior Initialization of:

-(Instancetype) Initwithitems: (Nsarray *) items;

Item parameter: A physical simulation element is stored inside

Uigravitybehavior Common methods:

-(void) AddItem: (ID <UIDynamicItem>) item;

Add 1 physical emulation elements

-(void) RemoveItem: (ID <UIDynamicItem>) item;

Remove 1 Physical simulation elements

Uigravitybehavior Common Properties

@property (nonatomic, readonly, copy) nsarray* items;

All the physical simulation elements added to the gravity behavior

@property (ReadWrite, nonatomic) Cgvector gravitydirection;

Gravity direction (is a two-dimensional vector)

@property (ReadWrite, nonatomic) cgfloat angle;

Gravity direction (is an angle, the positive direction of the X axis is 0°, clockwise positive, counterclockwise negative)

@property (ReadWrite, nonatomic) cgfloat magnitude;

Magnitude (used to control acceleration, 1.0 means acceleration is points/second²)

For example, to make a uiimageview drop effect

To set member properties:

@property (nonatomic, strong) Uidynamicanimator *animator;

First step: Create a physical emulator (by the way, set the simulation scope).

Self.animator =

[[Uidynamicanimator alloc] initWithReferenceView:self.view];

Step Two: Create the appropriate physical simulation behavior (by adding physical simulation elements).

Uigravitybehavior *gravity =

[[Uigravitybehavior alloc] initwithitems:@[self.blueview];

Step three: Add the physical simulation behavior to the physical emulator à start the simulation.

[Self.animator addbehavior:gravity];

You can also combine the collision behavior with the above gravity behavior to animate a uiimageview that falls to the bottom of the screen and adds a bounce effect:

Create physical simulation Behavior---> Gravity behavior (items: physical emulation elements)

Uigravitybehavior *gravity = [[Uigravitybehavior alloc] Initwithitems:@[self.blueview]];

Collision Detection Behavior

Uicollisionbehavior *collision = [[Uicollisionbehavior alloc] initwithitems:@[self.blueview]];//can add multiple, such as @[ Self.blueview,self.blueview2, SELF.BLUEVIEW3]

Make the bounds of the reference view a bounding box for collision detection

Collision.translatesreferenceboundsintoboundary = YES;

Adding physical emulation behavior to the physical emulator

[Self.animator addbehavior:gravity];

[Self.animator addbehavior:collision];

Key points of knowledge:

If you write directly, you may write:

Uidynamicanimator *animator =

[[Uidynamicanimator alloc] initWithReferenceView:self.view];

Uigravitybehavior *gravity =

[[Uigravitybehavior alloc] initwithitems:@[self.blueview];

[Animator Addbehavior:gravity];

The above code will not be expected, because animator will be destroyed immediately after executing the above code, and the physical emulator object will be destroyed when the animation effect is completed. This is why the emulator was set as a member property above. This is an important point of knowledge and is often encountered in other areas as well.

5 , Collision Behavior ( Uicollisionbehavior )

Brief introduction:

(1) can be used to achieve collision between objects

(2) A physical collision can be confined to a space by adding a boundary (boundary)

Uicollisionbehavior boundary-related methods:

-(void) Addboundarywithidentifier: (ID <NSCopying>) identifier Forpath: (uibezierpath*) Bezierpath;

-(void) Addboundarywithidentifier: (ID <NSCopying>) identifier frompoint: (Cgpoint) P1 topoint: (cgpoint) P2;

-(uibezierpath*) Boundarywithidentifier: (id <NSCopying>) identifier;

-(void) Removeboundarywithidentifier: (id <NSCopying>) identifier;

@property (nonatomic, readonly, copy) nsarray* boundaryidentifiers;

-(void) removeallboundaries;

Uicollisionbehavior Common usage:

@property (nonatomic, ReadWrite) BOOL translatesreferenceboundsintoboundary;

Whether to use the bounds of the reference view as the boundary

-(void) Settranslatesreferenceboundsintoboundarywithinsets:

(uiedgeinsets) insets;

Set the bounds of the reference view to the boundary, and set the padding

@property (nonatomic, ReadWrite) Uicollisionbehaviormode Collisionmode;

Collision Mode (divided into 3 types, elemental collisions, boundary collisions, all collisions)

@property (nonatomic, assign, ReadWrite) ID <UICollisionBehaviorDelegate> collisiondelegate;

Proxy object (can listen for element collision process)

6 , snapping behavior ( Uisnapbehavior )

Brief introduction:

The object can be quickly rushed to a position (snap position), the position will be captured with a certain vibration

Uisnapbehavior Initialization of:

-(Instancetype) Initwithitem: (ID <UIDynamicItem>) Item snaptopoint:

(Cgpoint) point;

Uisnapbehavior Common Properties:

@property (nonatomic, assign) CGFloat damping;

For reduction, damping (value range is 0.0 ~ 1.0, the greater the value, the smaller the amplitude of vibration)

Uisnapbehavior Use Note:

If you want to perform a continuous snapping behavior, you need to remove the preceding snapping behavior from the physical emulator first

iOS Development uidynamic

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.