"iOS Dev-112" uidynamic physical simulations such as gravity behavior, collision behavior, collision behavior, and other

Source: Internet
Author: User

(1) The uidynammic use is divided into three steps:

--Create an emulator (by the way define the simulation scope) (lazy load definition available)

--Create simulation behavior (by adding simulation elements by the way)

--Add simulation behavior to the emulator


(2) Here are examples of the combination of gravity behavior and collision behavior


#import "ViewController.h" @interface Viewcontroller () @property (weak, nonatomic) Iboutlet UIView *rectview; @property ( Nonatomic,strong) Uidynamicanimator *ani; @property (weak, nonatomic) Iboutlet UIView *blueview; @end @implementation viewcontroller-(Uidynamicanimator *) ani{if (!_ani) {//Create emulator (by the way define simulation range) _ani=[[uidynamicanimator Alloc]in    ItWithReferenceView:self.view]; } return _ani;} -(void) viewdidload {[Super viewdidload];} Blueview only participates in collision simulation, not in the simulation of gravity drop-(void) touchesended: (Nsset *) touches withevent: (uievent *) event{//Create emulator (by the way, define the simulation range),    //uidynamicanimator *ani=[[uidynamicanimator Alloc]initwithreferenceview:self.view] in lazy loading;    Create simulation behavior (incidentally add simulation elements) Uigravitybehavior *behave=[[uigravitybehavior Alloc]initwithitems:@[self.rectview]];    Define direction Behave.gravitydirection=cgvectormake (1, 1) according to vectors;    Define direction//behave.angle=-m_pi_4 by angle;            Defines the gravitational acceleration behave.magnitude=1; Collision detection, set the simulation range to the boundary Uicollisionbehavior *collisionbehave=[[uicollisionbehavior Alloc]initwithitems:@[self.rectview,self.blueview]];        Collisionbehave.translatesreferenceboundsintoboundary=yes;    The simulation behavior is added to the emulator [Self.ani Addbehavior:behave]; [Self.ani Addbehavior:collisionbehave];} @end

--3 important properties of gravity behavior, two directional attributesgravitydirectionAndAngle, an acceleration attributemagnitude。

The more important properties of collisions are the several ways to define boundaries.

such as the translatesreferenceboundsintoboundary property above. There are two other main types of:

    Collision detection, set the simulation range to the boundary    Uicollisionbehavior *collisionbehave=[[uicollisionbehavior alloc]initwithitems:@[ Self.rectview,self.blueview]];    Collisionbehave.translatesreferenceboundsintoboundary=yes;    Add a left and right oblique line as the collision boundary    [Collisionbehave addboundarywithidentifier:@ "lines" frompoint:cgpointmake (0) Topoint: Cgpointmake (+ +)];    Add a circle to do the border    uibezierpath *path=[uibezierpath bezierpathwithovalinrect:cgrectmake (0, 0, (), +)];    [Collisionbehave addboundarywithidentifier:@ "Circle" Forpath:path];

(3) Snapping behavior, the main property is the damping property damping.

#import "ViewController.h" @interface Viewcontroller () @property (weak, nonatomic) Iboutlet UIView *pinkview; @property ( Nonatomic,strong) Uidynamicanimator *ani, @end @implementation viewcontroller-(uidynamicanimator *) ani{    if (_ani= =nil) {        _ani=[[uidynamicanimator alloc]initwithreferenceview:self.view];    }    return _ani;} -(void) viewdidload {    [Super viewdidload];} -(void) touchesended: (Nsset *) touches withevent: (uievent *) event{    uitouch *touch=[touches anyobject];    Cgpoint Point=[touch LocationInView:touch.view];    Create snapping behavior    uisnapbehavior *snap=[[uisnapbehavior alloc]initwithitem:self.pinkview snaptopoint:point];    Set snapping properties    //Damping settings, 0~1, the larger the value, the better the damping, the smaller the vibration amplitude.    snap.damping=0.5;    Delete all behaviors (to ensure that each click is valid, or the second time after the click, no response)    [Self.ani removeallbehaviors];    Add Behavior    [Self.ani Addbehavior:snap];}

(4) In addition to the above 3 acts, there are other acts:

--uipushbehavior: Driving behavior

--uiattachmentbehavior: Adhesion behavior

--uidynamicitembehavior: Dynamic Elemental Behavior


"iOS Dev-112" uidynamic physical simulations such as gravity behavior, collision behavior, collision behavior, and other

Related Article

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.