IOS Physics engine

Source: Internet
Author: User

From IOS7 by tutorials Here's a little bit of a personal translation summary

1, first draw a block in the initialization method

UIView* square = [[UIView alloc] initWithFrame:
CGRectMake (+, +, + )];

Square.backgroundcolor = [Uicolor graycolor]; [self. View addsubview: square];

2, declare two variables.

Uidynamicanimator* _animator;

Uigravitybehavior* _gravity;

3, Initialize

_animator = [[uidynamicanimator alloc] Initwithreferenceview:self. View];

_gravity = [[uigravitybehavior alloc] initwithitems:@[Square]]; [_animator addbehavior:_gravity];

4, idle this block directly out of the screen, we need to add a border.

declaring variables

Uicollisionbehavior* _collision;

Code

_collision = [[Uicollisionbehavior alloc] initwithitems:@[Square]];

_collision. Translatesreferenceboundsintoboundary = YES; [_animator addbehavior:_collision];

5, collisions between multiple blocks

Then declare a strip.

UIView* barrier = [[UIView alloc]
initWithFrame: CGRectMake (0, +,-- )];

Barrier.backgroundcolor = [Uicolor redcolor]; [self. View addsubview: barrier];

We saw the block go through the strip.

Modify Code

_collision = [[uicollisionbehavior alloc] initwithitems:@[square, barrier]];

Then the strip was smashed down.

6, we put the strip also solutionkeys the border of the box.

_collision = [[Uicollisionbehavior alloc] initwithitems:@[Square]];

Add a boundary that coincides with the top edge

Cgpoint Rightedge = cgpointmake (barrier.frame.origin.x + barrier.frame. Size. width,

BARRIER.FRAME.ORIGIN.Y); [_collision Addboundarywithidentifier:@ "barrier"

FromPoint:barrier.frame.origin Topoint:rightedge];

You can see that the box is bounced open and landed at the bottom.

7, each animation is performed by a method, such as collision, bounce, etc... Add the following code

_collision.action = ^{NSLog (@ "%@,%@",

Nsstringfromcgaffinetransform (Square.transform), Nsstringfromcgpoint (Square.center));

};

The above code prints the change of center and attribute when the box falls. Square.center is the center of the box, and Square.transform is the property of the box.

The first part you will see the center of the box Square.center, the y-axis data in the change, such as

2014-07-24 11:41:42.133 dynamicsplayground[766:60b] [1, 0, 0, 1, 0, 0], {150, 161}

2014-07-24 11:41:42.149 dynamicsplayground[766:60b] [1, 0, 0, 1, 0, 0], {150, 164}

2014-07-24 11:41:42.167 dynamicsplayground[766:60b] [1, 0, 0, 1, 0, 0], {150, 167}

When the block hits the bar, the block starts to turn, and the result is a print

2014-07-24 11:41:42.649 dynamicsplayground[766:60b] [0.80305022, 0.59591132,-0.59591132, 0.80305022, 0, 0], {170, 266}

2014-07-24 11:41:42.666 dynamicsplayground[766:60b] [0.7690773, 0.63915575,-0.63915575, 0.7690773, 0, 0], {172, 269}

2014-07-24 11:41:42.683 dynamicsplayground[766:60b] [0.72068948, 0.69325799,-0.69325799, 0.72068948, 0, 0], {174, 272}

8, the dynamic behavior of the object is to adopt the Uidynamicitem protocol, the following is the definition of the protocol.

@protocol Uidynamicitem <nsobject>

@property (nonatomic, ReadWrite) cgpoint Center;
@property (nonatomic, readonly) cgrect bounds;
@property (nonatomic, ReadWrite) cgaffinetransform transform;

@end

9, collision notification. Add the code in the. m file.

@interface Viewcontroller () <UICollisionBehaviorDelegate> @end

After the collision

_collision.collisiondelegate = self ;

Post-collision methods

-(void)Collisionbehavior: (uicollisionbehavior *) behavior begancontactforitem: (id< uidynamicitem>) Item

Withboundaryidentifier: (id<nscopying>) identifier Atpoint: (Cgpoint) p {

NSLog (@ "Boundary contact occurred-%@", identifier);}

You can see the information that is printed after the collision.

2014-07-24 12:15:45.124 dynamicsplayground[820:60b] boundary contact Occurred-barrier

2014-07-24 12:15:45.692 dynamicsplayground[820:60b] Boundary contact occurred-(NULL)

10, add code after printing

uiview* view = (uiview*) item; view. BackgroundColor = [uicolor Yellowcolor];

[UIView animatewithduration:0.3

animations:^{

View. backgroundcolor = [uicolor Graycolor];

}];

You will see the color changes that occur after each collision with the block.

11, configure the properties.

The following code is appended to the bottom of the Viewdidload method,

uidynamicitembehavior* itembehaviour =

[[Uidynamicitembehavior alloc] initwithitems:@[Square]];

Itembehaviour. elasticity = 0.6;

[_animator addbehavior:itembehaviour];

Found that the blocks became more resilient. In addition to elasticity, Itembehaviour has more properties that can be changed.

12, adding dynamic behavior

Add a Boolean variable in the VIEWCONTROLLER.M

BOOL _firstcontact;

The following code is added to the protocol method.

if (!_firstcontact) {

_firstcontact = YES;

uiview* square = [[UIView alloc] initwithframe:CGRectMake (0, 100) ];

Square. backgroundcolor = [uicolor Graycolor];

[Self. view Addsubview:square];

[_collision additem:square];

[_gravity additem:square];

Uiattachmentbehavior* attach = [[Uiattachmentbehavior alloc]

Initwithitem:view

Attachedtoitem:square];

[_animator Addbehavior:attach];

} 

Two blocks are connected by a hidden line.

From-ios7 by tutorials, chapter II, the first section, the current online download, no Chinese version of the translation.

--Original can be reproduced, if there is inappropriate place to leave a message--guanliyang

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.