iOS animation--dynamicanimate

Source: Internet
Author: User

Mechanics Animation

The dynamicanimate-led mechanics animation is an API added by Apple in IOS7, which contains a lot of mechanical behavior, which is based on box2d implementation. This includes gravity, collisions, pushes, dumps, and custom behaviors.

The classes involved are as follows

Related classes Describe
Uidynamicanimator Equivalent to a manager for managing all added mechanical behavior
Uidynamicbehavior All mechanical behavior of the parent class, is an abstract class
Uigravitybehavior Gravity
Uicollisionbehavior Collision, elasticity
Uiattachmentbehavior Adsorption force
Uipushbehavior Thrust
Uisnapbehavior The force of the swing.
Uidynamicitembehavior Custom behavior

Uidynamicanimator needs to be an instance variable, and if a local variable animation does not work, the individual feels like the action was not added to the runloop after the function execution is finished.

The initialization of the uidynamicanimator requires a view to be bound, and the view must be a one-to-one relationship.

Each action can work on a lot of item, as long as this object implements the <UIDynamicItem> protocol, uiview default is so we do not need to implement manually.

Let's look at a couple of examples to explain

Gravity

Gravity is simple, let's look at a demo first

-(void  ) viewdidload {[Super viewdidload]; View  = [[UIView alloc] Initwithframe:cgrectmake (100 , 100 , 100 , 100  )];    View.backgroundcolor  = [Uicolor Graycolor];        [Self.view Addsubview:view];    _animator  = [[Uidynamicanimator alloc] initWithReferenceView:self.view]; Uigravitybehavior  *gravity = [[Uigravitybehavior alloc] Initwithitems:@[view]    ; [Gravity Setangle:  3.14 /0.5         }  

Where _animator is an instance variable, as explained above. When you run it, you will find that the view looks like it is acting downward to accelerate the linear motion.

The above code lets gravity work on the view and sets the direction and size of gravity.

@property (ReadWrite, nonatomic) cgvector gravitydirection; @property (ReadWrite, nonatomic) cgfloat angle;@ Property (ReadWrite, nonatomic) cgfloat magnitude; -(void) Setangle: (cgfloat) Angle magnitude: (cgfloat) magnitude;

Above are the methods and properties of gravity, and we look at them one at a glance.

Gravitydirection is a vector of gravity, and since it is a vector, it has direction and size. The coordinate system used is the Uikit coordinate system, so the default upper-left corner is the (0,0) point, and the size of the vector is the size of gravity.

Angle is the direction of the vector, we can set the direction of gravity without gravitydirection to the angle, because angle is more intuitive without calculation.

Similarly, use magnitude to set the gravity size.

Elastic

Elasticity is a very interesting behavior, in addition to we set the elastic item, but also set the elastic boundary.

Let's look at examples first

Uicollisionbehavior *collisionbehavior = [[Uicollisionbehavior alloc] initwithitems:@[view]];[ Collisionbehavior addboundarywithidentifier:@ "123ß" Frompoint:cgpointmake (0  topoint:cgpointmake(= YES;
[_animator Addbehavior:collisionbehavior];

There is an attribute in elasticity

@property (nonatomic, ReadWrite) BOOL translatesreferenceboundsintoboundary;

Whether to set the associated view as a boundary, the associated view here refers to the view in Uidynamicanimator. Set this property to Yes, run the code, people will send the view drop to the bottom will be released elastic collision with the bottom.

In fact, elastic behavior provides a lot of ways about boundaries.

-(void) settranslatesreferenceboundsintoboundarywithinsets: (uiedgeinsets) insets; -(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;

These are relatively simple to explain, interested people can try it yourself.

The following highlights an attribute

ID <UICollisionBehaviorDelegate> collisiondelegate;

Elasticity has a proxy, isn't it interesting that we continue to look at examples

- (void) Collisionbehavior: (uicollisionbehavior*) behavior Begancontactforitem: (ID<UIDynamicItem>) item1 Withitem: (ID<UIDynamicItem>) item2 atpoint: (cgpoint) p{NSLog (@"began contact item");}- (void) Collisionbehavior: (uicollisionbehavior*) behavior Endedcontactforitem: (ID<UIDynamicItem>) item1 Withitem: (ID<UIDynamicItem>) item2{NSLog (@"End CONTANCT Item");}- (void) Collisionbehavior: (uicollisionbehavior*) behavior Begancontactforitem: (ID<UIDynamicItem>) Item Withboundaryidentifier: (ID<NSCopying>) identifier Atpoint: (cgpoint) p{NSLog (@"began contact boundary");}- (void) Collisionbehavior: (uicollisionbehavior*) behavior Endedcontactforitem: (ID<UIDynamicItem>) Item Withboundaryidentifier: (ID<NSCopying>) identifier{NSLog (@"End Contact Boundary");}

We implement the elastic agent and implement the method, run the program, when we first hit the bottom we found that the system printed out

 -- ,- +  the: to:49.123transanimate[25564:17037174] began contact boundary -- ,- +  the: to:49.157transanimate[25564:17037174] End Contact boundary -- ,- +  the: to:49.524transanimate[25564:17037174] began contact boundary -- ,- +  the: to:49.557transanimate[25564:17037174] End Contact boundary

The proxy method is called each time the elasticity is to be and ends.

According to the method name can be understood, the two sets of proxy methods for the object collision, a set of collision against the boundary.

Adsorption force

On the adsorption force, first of all to explain, we can understand the adsorption force in the adsorption of the origin of a stick, note that the stick is not a rope, connected to the item. This means that the adsorption force is rigid.

See demo below

    Uiattachmentbehavior *attachment = [[Uiattachmentbehavior alloc] Initwithitem:view attachedtoanchor:cgpointmake (  )];      - ;    [_animator addbehavior:attachment];

Can see here we use the adsorption force of the construction method is a point, the length of the "stick", the running program found that the object in the role of gravity will be anchor as the center, with length radius, a little turn.

Adsorption force provides a lot of construction methods

-(Instancetype) Initwithitem: (ID <UIDynamicItem>) Item Attachedtoanchor: (Cgpoint) point; -(Instancetype) Initwithitem: (ID <UIDynamicItem>) Item Offsetfromcenter: (uioffset) offset Attachedtoanchor: (cgpoint) point; -(Instancetype) Initwithitem: (ID <UIDynamicItem>) item1 Attachedtoitem: (ID < Uidynamicitem>) item2; -(Instancetype) Initwithitem: (ID <UIDynamicItem>) item1 offsetfromcenter: (uioffset) Offset1 Attachedtoitem: (ID <UIDynamicItem>) item2 offsetfromcenter: (uioffset) Offset2;

The difference between these two sets of construction methods is the adsorption object, the first group is the point as the object, and the second group is the item object.

To explain offset, this refers to the anchor offset of the Adsorbed object, which is the default center.

Here's a look at the properties

@property (readonly

The Uiattachmentbehaviortype property indicates whether the snap point is an object or an anchor point.

The following are the anchor points, adsorption length, resistance and vibration frequency, do not say.

Thrust
It's almost like gravity. Here's an example. Look at properties and methods

-(void) Settargetoffsetfromcenter: (Uioffset) o Foritem: (ID <UIDynamicItem>   ReadOnly) uipushbehaviormode mode; @property (nonatomic, ReadWrite) BOOL active; @property (ReadWrite, nonatomic) cgfloat angle; @property (ReadWrite, nonatomic) cgfloat magnitude; @property (ReadWrite, nonatomic) cgvector pushdirection; -(void) Setangle: (cgfloat) Angle magnitude: (cgfloat) magnitude;

The following angle, magnitude, pushdirection and gravity do not say much.

Focus on Uipushbehaviormode and active.

Uipushbehaviormode Indicates whether the thrust is continuous or transient, and active indicates if the thrust is still in effect.

Above-(void) Settargetoffsetfromcenter: (Uioffset) o Foritem: (id <UIDynamicItem>) item; The method is to say the offset of the thrust action point, which is the default center.

The force of the swing.

I also explained that it is not very clear, I think it can be understood as a black hole in the adsorption of the object, it should be quite image ...

Just look at the examples.

We add a Tapgesture event to the controller using the storyboard

-(Ibaction) Handlegesture: (Uigesturerecognizer *) Sender {    = [sender LocationInView:self.view];    *snap = [[Uisnapbehavior alloc] Initwithitem:view snaptopoint:point];    [_animator Addbehavior:snap];}

The runtime discovers where we clicked, and the view flies directly past and there is resistance in the middle.

There is only one property of the swing force, which is resistance.

Custom behavior

Generally we do not use custom behavior, only a few times need to customize their own

There are many properties that we can customize

Property Describe
Desnsity Density, if a 100*100 point object, its density is 1.0, the Force is 1.0 then its acceleration is 100 points/s2
Elasticity Elastic wash, value range 0.0~1.0,0.0 means no elasticity, 1.0 for rebate elastic collision
Friction Friction coefficient, 0.0 means no friction, 1.0 means strong friction, if you want to set a stronger can be greater than 1
Resistance Resistance, when the object is moving, resistance in the linear direction, 0.0 no resistance, Cgfloat_max represents the maximum resistance
Allowrotation Whether to allow rotation.
Angularresistance Angular resistance, when the object rotates, the resistance of the direction of rotation

iOS animation--dynamicanimate

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.