Ios-ui-uidynamic (i)

Source: Internet
Author: User

Uidynamic is a technology introduced from IOS7 that belongs to the Uikit framework which can simulate the physical phenomena of life such as collision jitter swing, etc.

A.

To use the Uidynamic step:

1. Create a power effect device Uidynamicanimator

2. Create a Power effect (Behavior) to add to the view on

3. Add the power effect to the power effect

Note: You must abide by the Uidynamicitem this Protocol to use the power effect, UIView by default has complied with the Uidynamicitem protocol

Play power effects such as playing electric guitar electric guitar Effective fruit device can add a variety of electronic effects

The power effect also has an effect device called the Power effect which can add power effect

The switch effect of the electric guitar will remove the previous effect

The power effect is the same.

Electric guitar can overlay multiple effects

The power effect is the same.

The power effects provided by the Uidynamic

Two.

Uigravitybehavior: Gravity effect (set the direction acceleration of gravity to allow the object (view) to fall toward the gravity direction)

Properties of the gravity effect:

Nsarray *items; All effect objects added to the gravity effect

Cgvector gravitydirection; Gravity direction (is a two-dimensional vector) with the upper-left corner as the coordinate Origin x negative to the left positive to the right Y negative to positive to the lower number the greater the gravity effect

CGFloat angle; Gravity direction (is an angle, the x-axis positive direction is 0°, clockwise positive, counterclockwise negative)

CGFloat magnitude; magnitude (used to control acceleration, 1.0 for acceleration is points/second²) The greater the gravitational acceleration, the greater the collision.

Uicollisionbehavior: Collision effect () allows for collision between objects, or it can be achieved by adding boundaries (boundary) to the boundary

Boundary-related methods:

-(void) Addboundarywithidentifier: (ID <NSCopying>) identifier Forpath: (uibezierpath*) Bezierpath; Add a boundary for a Bézier curve path

-(void) Addboundarywithidentifier: (ID <NSCopying>) identifier frompoint: (Cgpoint) P1 topoint: (cgpoint) P2; By adding a line of two dots as a boundary

-(uibezierpath*) Boundarywithidentifier: (id <NSCopying>) identifier; Find boundary path by ID

-(void) Removeboundarywithidentifier: (id <NSCopying>) identifier; Remove the boundary of the ID

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

-(void) removeallboundaries; remove all boundaries

typedef ns_options (Nsuinteger, Uicollisionbehaviormode) {

Uicollisionbehaviormodeitems = 1 << 0, Elemental collision

uicollisionbehaviormodeboundaries = 1 << 1, Border collision

uicollisionbehaviormodeeverything = Nsuintegermax All collisions

} ns_enum_available_ios (7_0);

Proxy methods for collision effects:

Two elements collide with one another

-(void) Collisionbehavior: (uicollisionbehavior*) behavior Begancontactforitem: (ID <UIDynamicItem>) item1 Withitem: (id <UIDynamicItem>) item2 atpoint: (Cgpoint) p;

-(void) Collisionbehavior: (uicollisionbehavior*) behavior Endedcontactforitem: (ID <UIDynamicItem>) item1 Withitem: (id <UIDynamicItem>) item2;

Trigger when the view collides with the boundary

-(void) Collisionbehavior: (uicollisionbehavior*) behavior Begancontactforitem: (ID <UIDynamicItem>) Item Withboundaryidentifier: (ID <NSCopying>) identifier atpoint: (Cgpoint) p;

-(void) Collisionbehavior: (uicollisionbehavior*) behavior Endedcontactforitem: (ID <UIDynamicItem>) Item Withboundaryidentifier: (id <NSCopying>) identifier;

Three. Specific code

@interfaceViewcontroller () <UIDynamicAnimatorDelegate,UICollisionBehaviorDelegate>{uidynamicanimator*Dynamicanimator; UIView*View1; UIView*View2; UIView*view3;}@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload];//Power effect DeviceDynamicanimator =[[Uidynamicanimator Alloc]initwithreferenceview:self.view];//Self.view areas that produce dynamic effectsDynamicanimator.Delegate=Self ; View1=[[uiview Alloc]initwithframe:cgrectmake (0,0, -, -)]; View1.center=Self.view.center; View1.backgroundcolor=[Uicolor Redcolor];    [Self.view Addsubview:view1]; View2=[[uiview Alloc]initwithframe:cgrectmake (0,0, -, -)]; View2.center= Cgpointmake (self.view.center.x+ -, self.view.center.y+ -); View2.backgroundcolor=[Uicolor Orangecolor];    [Self.view Addsubview:view2]; VIEW3=[[uiview Alloc]initwithframe:cgrectmake (0,0, -, -)]; View3.backgroundcolor=[Uicolor Bluecolor];   [Self.view ADDSUBVIEW:VIEW3]; }
#pragmaMark-------finger to touch the screen to add a power effect---------------------------(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *)Event{Uitouch* Touch =[touches anyobject]; Cgpoint touchpoint=[Touch LocationInView:self.view]; View1.center=touchpoint; View2.center= Cgpointmake (self.view.center.x+ -, self.view.center.y+ -); View3.center= Cgpointmake (self.view.center.x+Ten, self.view.center.y+Ten); View1.hidden=NO;}-(void) touchesended: (Nsset *) touches withevent: (Uievent *)Event{//to remove the previous power effect[Dynamicanimator removeallbehaviors];//add gravity effect on View1Uigravitybehavior * Gravity =[[Uigravitybehavior Alloc]initwithitems:@[view1]];//Set AccelerationGravity.magnitude = -;//set the direction of the power effectGravity.gravitydirection = Cgvectormake (0,1);//add to effect start power effect[Dynamicanimator addbehavior:gravity]; Uicollisionbehavior* Collision =[[Uicollisionbehavior ALLOC]INITWITHITEMS:@[VIEW1,VIEW2,VIEW3]]; Collision.collisiondelegate=Self ; Collision.translatesreferenceboundsintoboundary=YES;//If you set the two elements to collide with each other and set the boundaries, it doesn't work.//collision.collisionmode = uicollisionbehaviormodeitems;[Dynamicanimator addbehavior:collision];#pragmaMark----------------Power Element effect----------------------------//can be used in conjunction with other power effectsUidynamicitembehavior * item =[[Uidynamicitembehavior ALLOC]INITWITHITEMS:@[VIEW2,VIEW1,VIEW3]];//set the jumping degree of an elementItem.elasticity =0.9;    [Dynamicanimator Addbehavior:item]; }

Of course, you can also set the bounds, when only adding View1

-(void) touchesended: (Nsset *) touches withevent: (Uievent *)Event{//to remove the previous power effect[Dynamicanimator removeallbehaviors];//add gravity effect on View1Uigravitybehavior * Gravity =[[Uigravitybehavior Alloc]initwithitems:@[view1]];//Set AccelerationGravity.magnitude = -;//set the direction of the power effectGravity.gravitydirection = Cgvectormake (0,1);//add to effect start power effect[Dynamicanimator addbehavior:gravity];#pragmaMark------Collision Effect-----------------------Uicollisionbehavior* Collision =[[Uicollisionbehavior Alloc]initwithitems:@[view1,view2]]; Collision.collisiondelegate=Self ;//set a slash boundary//[Collision addboundarywithidentifier:@ "Line1" frompoint:cgpointmake (0, +) Topoint:cgpointmake (+)];//Set vertical line boundaries//[Collision addboundarywithidentifier:@ "Line2" frompoint:cgpointmake (0) Topoint:cgpointmake (+)];//draw a circle through the Bethel CurveUibezierpath *path = [Uibezierpath bezierpathwithovalinrect:cgrectmake (0, $, -, -)]; [Collision Addboundarywithidentifier:@"Yuan"Forpath:path]; //the range of the power effect as the boundaryCollision.translatesreferenceboundsintoboundary =YES; [Dynamicanimator addbehavior:collision];}

Ios-ui-uidynamic (i)

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.