IOS---UIKit Dynamics

Source: Internet
Author: User

IOS 7 adds Uikit Dynamics, allowing UIView to simulate realistic physical effects. By using the Uidynamicitem protocol and the dynamic object that supports it, the user experience can be greatly improved, now adding gravity , collisions , Springs and adsorption to the interface and other effects.

to implement dynamic behavior, you can create Uidynamicanimator (Mechanics animation producer). There are 6 classes available for custom uidynamicanimator:uiattachmentbehavior (connection),uicollisonbehavior (collision) , Uidynamicitembehavoir (Object properties) , Uigravitybehavor (Gravity) , Uipushbehavor (Thrust) , Uisnapbehavior (adsorption) .

1.Uigravitybehavor (Gravity)

    ImageView = [[Uiimageview alloc]initwithframe:cgrectmake (0, +, +)];    Imageview.backgroundcolor = [Uicolor redcolor];    [Self.view Addsubview:imageview];    animator = [[Uidynamicanimator Alloc]initwithreferenceview:self.view];        Uigravitybehavior *behavior = [[Uigravitybehavior Alloc]initwithitems:@[imageview]];    [Behavior Setgravitydirection:cgvectormake (0.0f, 1.0f)];    [Animator Addbehavior:behavior];
Assigns the gravity effect to ImageView, which is a child view of the Self.view. To do this, first create a uidynamicanimator, and designates the reference view as a top view (self.view) of the dynamic object, and then creates a Uigravitybehavior object that initializes it with a numeric value that includes a view that applies the gravity effect, and then sets the gravity behavior: Here is the force that goes down Sanga 1.0f along the y axis. After configuring the gravity behavior, use the method Addbehavior: add it to the Uidynamicanimator (Mechanics animation producer).

The 1.0f corresponds to the gravitational force of the Earth and brings the acceleration: 9.80655m/s2. To specify about 1/10 of the Earth's gravity, the force can be 0.1. When you change the parameter to a negative value, the gravity direction moves in the opposite direction.

2.UICollisionBehavior (collision)

Uicollisonbehavior has two properties that specifically illustrate: collisionmode and translatesreferenceboundsintoboundary. The Collisionmode has three values:

Uicollisionbehaviormodeitems (collisions between objects), uicollisionbehaviormodeboundaries (collisions between objects, but only at the boundary), Uicollisionbehaviormodeeverything (objects collide as a result of an object collision);Translatesreferenceboundsintoboundary (to allow the object to collide at the boundary, the boundary must be defined, The simplest way to do this is to set this value to Yes).

    Uicollisionbehavior *collison = [[Uicollisionbehavior Alloc]initwithitems:@[imageview]];    Collison.translatesreferenceboundsintoboundary = YES;    Collison.collisionmode = uicollisionbehaviormodeeverything;    Collison.collisiondelegate = self;    [Animator Addbehavior:collison];

Protocol Uicollisionbehaviordelegate defines 4 callback methods, two of which are called at the beginning of the collision and the other two at the end of the collision. In each set of callback methods, one indicates the collision boundary and one is not indicated. All methods provide an application that points to the object that caused the method to be called, and in the method called at the start of the collision, Cgpoint is provided, which accurately points to the point of contact.

3.Uisnapbehavior (adsorption)The adsorption behavior is simple, which allows the object to move dynamically to another part of the screen. In the example app, this behavior is triggered by a gesture that, regardless of where the user presses on the screen, Inageview jumps to the specified location.
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initwithtarget:self Action: @selector (Tapclick:)];    [Self.view Addgesturerecognizer:tap];
-(void) Tapclick: (UITapGestureRecognizer *) tap{    cgpoint point = [tap LocationInView:self.view];    animator = [[Uidynamicanimator Alloc]initwithreferenceview:self.view];        Uisnapbehavior *snap = [[Uisnapbehavior alloc]initwithitem:imageview2 snaptopoint:point];    snap.damping = 1.0f;        [Animator Addbehavior:snap];}

4.Uidynamicitembehavoir (Object properties) to modify an object's properties, you can create a Uidynamicitembehavior object and initialize the object with the master and apprentice to modify its properties, and in the example application below, make the imageview like a ball, more resilient to gravity and collisions.
 animator = [[        Uidynamicanimator Alloc]initwithreferenceview:self.view];    Uigravitybehavior *gravity = [[Uigravitybehavior Alloc]initwithitems:@[imageview]];        [Gravity Setgravitydirection:cgvectormake (0.0f, 1.0f)];    Uicollisionbehavior *collision = [[Uicollisionbehavior Alloc]initwithitems:@[imageview]];    Collision.translatesreferenceboundsintoboundary = YES;        Collision.collisionmode = uicollisionbehaviormodeeverything;        Uidynamicitembehavior *itembehavior = [[Uidynamicitembehavior Alloc]initwithitems:@[imageview]];    Itembehavior.allowsrotation = YES;    Itembehavior.angularresistance = 0.0f;    itembehavior.density = 2.0f;    itembehavior.elasticity = 1.0f;    Itembehavior.friction = 0.0f;        Itembehavior.resistance = 1.0f;    [Animator Addbehavior:gravity];    [Animator Addbehavior:collision]; [Animator Addbehavior:itembehavior]; 
attached: People with interest can follow my public number: Internet Geek .

attached: Properties of a dynamic object and its description

IOS---UIKit Dynamics

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.