iOS Development expanded Chapter-uidynamic (Gravity Behavior + collision detection)

Source: Internet
Author: User

iOS Development expanded Chapter-uidynamic (Gravity Behavior + collision detection)

First, gravity behavior

Description: Given gravity direction, acceleration, let the object fall toward the gravity direction

1. Methods

(1) Initialization of Uigravitybehavior

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

Item parameter: A physical simulation element is stored inside

(2) Common methods of Uigravitybehavior

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

Add 1 physical emulation elements

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

Remove 1 Physical simulation elements

2.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²)

Second, the collision behavior

1. Introduction

Description: Can be used to achieve collision between objects

Physical collisions can be confined to a space by adding boundaries (boundary)

2.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;

3.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)

Third, code example

Drag and drop several controls in the storyboard for testing.

Test code:

YYVIEWCONTROLLER.M file

////YYVIEWCONTROLLER.M//12-gravity Behavior and collision behavior////Created by Apple on 14-8-6.//Copyright (c) 2014 Yangyong. All rights reserved.//#import"YYViewController.h"@interface Yyviewcontroller () @property (weak, nonatomic) Iboutlet UIView*Redview, @property (weak, nonatomic) Iboutlet Uiprogressview*Block1, @property (weak, nonatomic) Iboutlet Uisegmentedcontrol*Block2, @property (nonatomic,strong) Uidynamicanimator*animator, @end @implementation Yyviewcontroller-(Uidynamicanimator *) animator{if(_animator==Nil) {        //Create a physical emulator (Referenceview: Reference view, setting emulation range)Self.animator=[[Uidynamicanimator Alloc]initwithreferenceview:self.view]; }    return_animator;}- (void) viewdidload{[Super Viewdidload]; //set the angle of the red viewself.redview.transform=cgaffinetransformmakerotation (m_pi_4);}-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *)Event{    //1. Gravity Behavior//[self testgravity]; //2. Gravity Behavior + Collision detection//[self testgravityandcollsion]; //3. Test some properties of gravity[self testGravityAndCollsion2]; //using 2 lines as the boundary//[self testGravityAndCollision3]; //4. Use circles as boundaries//[self testGravityAndCollision4];}/** * Gravity behavior*/-(void) testgravity{//1. Create simulation behavior (what is the simulation effect?) )//Gravity BehaviorUigravitybehavior *gravity=[[Uigravitybehavior alloc]init]; //2. Adding physical emulation elements[Gravity AddItem:self.redView]; //3. Perform simulation to allow physical simulation elements to perform simulation behavior[Self.animator addbehavior:gravity];}/** * Gravity behavior + Collision detection*/-(void) testgravityandcollsion{//1. Gravity BehaviorUigravitybehavior *gravity=[[Uigravitybehavior alloc]init];        [Gravity AddItem:self.redView]; //2 Collision Detection BehaviorUicollisionbehavior *collision=[[Uicollisionbehavior alloc]init];    [Collision AddItem:self.redView];    [Collision AddItem:self.block1];        [Collision AddItem:self.block2]; //make the bounding rectangle of the reference view the boundary of the collision detectioncollision.translatesreferenceboundsintoboundary=YES; //3. Perform the simulation[Self.animator addbehavior:gravity]; [Self.animator addbehavior:collision];}/** * Test properties of gravity behavior*/-(void) testgravityandcollsion2{//1. Gravity BehaviorUigravitybehavior *gravity=[[Uigravitybehavior alloc]init]; //(1) Set the direction of gravity (is an angle)//gravity.angle= (m_pi_2-m_pi_4); //(2) Set the acceleration of gravity, the greater the acceleration of gravity, the greater the collisionGravity.magnitude= -; //(3) set the direction of gravity (is a two-dimensional vector)Gravity.gravitydirection=cgvectormake (0,1);        [Gravity AddItem:self.redView]; //2 Collision Detection BehaviorUicollisionbehavior *collision=[[Uicollisionbehavior alloc]init];    [Collision AddItem:self.redView];    [Collision AddItem:self.block1];        [Collision AddItem:self.block2]; //make the bounding rectangle of the reference view the boundary of the collision detectioncollision.translatesreferenceboundsintoboundary=YES; //3. Perform the simulation[Self.animator addbehavior:gravity];    [Self.animator addbehavior:collision]; }/** Use circles as boundaries*/- (void) testgravityandcollision4{//1. Gravity BehaviorUigravitybehavior *gravity =[[Uigravitybehavior alloc] init];        [Gravity AddItem:self.redView]; //2. Collision Detection BehaviorUicollisionbehavior *collision =[[Uicollisionbehavior alloc] init];        [Collision AddItem:self.redView]; //add an ellipse as a collision boundaryUibezierpath *path = [Uibezierpath bezierpathwithovalinrect:cgrectmake (0,0, the, the)]; [Collision Addboundarywithidentifier:@"Circle"Forpath:path]; //3. Start Simulation[Self.animator addbehavior:gravity]; [Self.animator addbehavior:collision];}/** * with 2 lines as the boundary*/- (void) testgravityandcollision3{//1. Gravity BehaviorUigravitybehavior *gravity =[[Uigravitybehavior alloc] init];        [Gravity AddItem:self.redView]; //2. Collision Detection BehaviorUicollisionbehavior *collision =[[Uicollisionbehavior alloc] init];    [Collision AddItem:self.redView]; Cgpoint STARTP= Cgpointmake (0, the); Cgpoint ENDP= Cgpointmake ( the, -); [Collision Addboundarywithidentifier:@"line1"FROMPOINT:STARTP TOPOINT:ENDP]; Cgpoint startP1= Cgpointmake ( the,0); [Collision Addboundarywithidentifier:@"line2"FROMPOINT:STARTP1 TOPOINT:ENDP];//collision.translatesreferenceboundsintoboundary = YES; //3. Start Simulation[Self.animator addbehavior:gravity]; [Self.animator addbehavior:collision];} @end

Some test results:

Code Supplement Description:

(1) Acceleration

Speed: point/s

Acceleration: point/s²

1\2 * Acceleration * t²

Gravity.magnitude = 1000; The greater the gravitational acceleration, the greater the collision.

(2) Direction of gravity

The coordinates are as follows:

Gravity direction (two-dimensional vector)

Description : A point within a given coordinate plane. Then the origin (0,0) is used to connect it, which forms a vector.

Note : In iOS, the upper-left corner is the coordinate origin, the right x is increased, and the lower y is larger.

1     // (3) set the direction of gravity (is a two-dimensional vector) 2     Gravity.gravitydirection=cgvectormake (-11);

The gravity direction is the lower left (southwest) direction

(3) Collision detection behavior

Uicollisionbehavior *collision =[[Uicollisionbehavior alloc] init];    [Collision AddItem:self.redView]; Cgpoint STARTP= Cgpointmake (0, the); Cgpoint ENDP= Cgpointmake ( the, -); [Collision Addboundarywithidentifier:@"line1"FROMPOINT:STARTP TOPOINT:ENDP]; Cgpoint startP1= Cgpointmake ( the,0); [Collision Addboundarywithidentifier:@"line2"FROMPOINT:STARTP1 TOPOINT:ENDP];

Note : Identifiers cannot be written empty. You can write a string because the identifier needs to conform to the Nscopying protocol, and the string satisfies the requirement.

(4) Bezier curve

Tip: Here the path is a circle, setting the width of the height is not the same, then it is an ellipse.

iOS Development expanded Chapter-uidynamic (Gravity Behavior + collision detection)

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.