A Preliminary Study on iOS7 UIKit Dynamics

Source: Internet
Author: User

This time I started to learn about iOS7's new UIKit Dynamics. I think some people translate it into UIKit mechanics. I think it is quite appropriate, so I borrowed it for a usage.

This UIKit Department of mechanics has four things to design: 1. UIDynamicAnimator: This is mainly used
  • Context that powers xing
  • Determine Coordinate System Based on ref view
  • Control Power Engine
  • Maintain the status of Dynamic Behaviors
2. UIDynamicBehavior: Mainly used to describe dynamic behaviors. iOS7 provides the following behaviors by default.
  • UIAttachmentBehavior
  • UICollisionBehavior
  • UIDynamicItemBehavior
  • UIGravityBehavior
  • UIPushBehavior
  • UISnapBehavior
3. Reference View: This is used to initialize Animator and provide Reference coordinate system for dynamic behavior. 4. dynamic item: dynamic item is any object that complies with the UIDynamicItem protocol. After talking about so many theories, I can't figure it out myself. Just go to the code. First, let's take a simple demo, create a project, select single VIew Application, and declare several variables in ViewController. m.
 UIDynamicAnimator * UIGravityBehavior *_gravity;

 

Then write a small method:
 - (           UIView *testView = [[UIView alloc] initWithFrame:CGRectMake(, , ,             _animator =     _gravity =  }

 

Use this method in the viewDidLoad method. Run the program again and you will see a blue ball on the simulator. Then he drops down and then disappears =. Boring... It doesn't matter if you are bored now. Let's take a look at how this boring program code is written later. In fact, it's very easy to create a new UIView instance named testView, make it look like a blue ball, initialize _ animator, and then give him reference coordinates. Basically, all the operations in UIKit mechanics need to be done by him, then initialize _ gravity, and give him testView. This _ gravity, as its name implies, is to give testView a similar gravity effect that will fall down, and the downgrading acceleration is by default the same as the actual gravity acceleration. Add the _ gravity action to _ animator! Okay. Next, let's look at something else. Although the name of _ gravity is gravity, can we change the acceleration? Yes. Let's look at how to change it. Looking at the UIGravityBehavior text block, we found a property named magn.pdf, which is obviously a change in acceleration. After _ gravity initialization, add
    [_gravity setMagnitude:];

 

Run the program and you will find the blue square flying out. That's right. In addition, there are several attributes in UIGravityBehavior.
Well, we can control how the object floats with these attributes. But the square suddenly floated out. How can we keep it in the simulator view? This requires UICollisionBehavior. As its name implies, it is a collision behavior. It is also a collision behavior that makes it hit the edge of the view. In ViewController. m, the name of a UICollisionBehavior instance is also declared as _ collision.
 UICollisionBehavior * _collision;

 

Initialize It In The startAnimator method, and write it at the end.
     _collision =      [_animator addBehavior:_collision];

 

Then we can see that when the square falls to the bottom of the view, it stops after a few clicks. The implementation is also very simple. initialize it, give it testView, change the translatesreferencebounds=boundary attribute to YES, and add this behavior to _ animator. The attribute TranslatesReferenceBoundsIntoBoundary is also intuitive, that is, it tells him to treat the view as a boundary. In addition to the view edge collision boundary, we can also customize a boundary. UICollisionBehavior also provides a method to define the boundary-addBoundaryWithIdentifier: fromPoint: toPoint: The usage is as follows:
     [_collision addBoundaryWithIdentifier: fromPoint:CGPointMake(, ) toPoint:CGPointMake(, )];

 

That is to say, give him a name for a string, and then define the boundary between the two points from this point to that point. However, this line is invisible, but you can draw one yourself. For example.
UICollisionBehavior can also set Delegate so that a callback method can be sent when a square collision occurs. In this case, you can do something else during the collision. For example, follow the protocol <UICollisionBehaviorDelegate> In ViewController. m and write a line after _ collision sets the custom boundary.
     [_collision setCollisionDelegate:self];

 

In this way, you can set the Delegate as ViewController and then rewrite the method in the write protocol.
 - ()collisionBehavior:(UICollisionBehavior *)behavior beganContactForItem:(<UIDynamicItem>)item withBoundaryIdentifier:(<NSCopying>      UIView *v = (UIView *      [UIView animateWithDuration: animations:^    }

 

In the above method, the red color will flash when the square is collided. In addition, there are several methods in the UICollisionBehaviorDelegate protocol.
  • – collisionBehavior:beganContactForItem:withBoundaryIdentifier:atPoint:
  • – collisionBehavior:beganContactForItem:withItem:atPoint:
  • – collisionBehavior:endedContactForItem:withBoundaryIdentifier:
  • – collisionBehavior:endedContactForItem:withItem:
You can divide events by object or by boundary at the beginning and end respectively. You can also set the collision mode in UICollisionBehavior. You can modify this attribute by using the attribute CollisionMode. The CollisionMode has three enumeration options:
  • UICollisionBehaviorModeItems
  • UICollisionBehaviorModeEverything
  • UICollisionBehaviorModeBoundaries
In fact, you can choose to collide with something. For example, when you choose UICollisionBehaviorModeItems, it means that you point to another object and not the border. The collision is almost done. Let's study something else. For example, if UIAttachmentBehavior is literally understood as an adsorption Behavior, we can create another square to try this Behavior.
     UIView *testView2 = [[UIView alloc] initWithFrame:CGRectMake(, , ,       [self.view addSubview:testView2];
Place it under the first square and the color is green. Then, when _ gravity is initialized, the second block is added to the block. Otherwise, the second block will not be dropped.
     _gravity = [[UIGravityBehavior alloc] initWithItems:@[testView,testView2]];

 

At this time, the program runs and finds that the first block remains on the boundary we defined earlier, and the second block disappears from the boundary, this is because we didn't add the second block to _ collision, but we don't care. This is exactly used to study UIAttachmentBehavior. Add the two lines of code at the end of the startAnimator method, initialize _ attachment, and add two blocks. This initialization indicates that the two blocks are adsorbed together.
     _attachment =      [_animator addBehavior:_attachment];

 

Then run the program and find that the first square remains on the boundary as before, but the second square is under the boundary. The two squares seem to have been connected with the rope in the middle, this is the role of UIAttachmentBehavior. Similarly, UIAttachmentBehavior also provides several attributes for various effects:
  • AnchorPoint: attached point
  • AttachedBehaviorType: type of attachment
  • Damping: the damping coefficient between two objects, which is actually the extent to which the two objects are affected when the distance is reduced.
  • Frequency
  • Length: adsorption length

This behavior provides rotation and linear motion. To implement an instance, first clear all the strange Behavior and leave _ collision to implement a boundary. Declare a UIDynamicItemBehavior instance:
     UIDynamicItemBehavior *_dynamicItem;

 

Then add the following code:
     _dynamicItem =     [_dynamicItem addAngularVelocity:     [_animator addBehavior:_dynamicItem];

 

At this time, the square will rotate clockwise, and then slowly stop, that is, to give him a initial speed of rotation, and then stop under the action of friction and so on. Remove the second line of code above and replace it with the following code to implement another behavior, linear motion, and LinearVelocity is to give him the initial speed of x and y, then it will move in a straight line based on the overlapping direction of the vector.
     [_dynamicItem addLinearVelocity:CGPointMake(, ) forItem:testView];

 

Based on these two actions, there are several attributes to match these two actions:
  • AllowsRotation: whether to allow rotation
  • AngularResistance: sets the rotation resistance.
  • Density: object density
  • Elasticity: the elasticity of an object directly affects how an object can play after it hits something.
  • Friction: friction of an object
  • Resistance: resistance to an object
 
There is also a PushBehavior, which is well understood. Let's look at the Code directly, delete all the items in the Code except for the square, _ animator, and _ collision, and add the following code at the end:
     _push =     [_push setAngle:     [_push setMagnitude:     [_animator addBehavior:_push];

 

After running the program, we can see that the square seems to have been pushed, and then the code is very easy to implement. initialize _ push and then set a mode for the square, here, the modes are uipus?haviormodeinstantaneous and mode: uipus=haviormodecontinuous. The difference between them is that one is push, and the other is continuous push. After initialization, you can set the angle and initial speed for it, which is simple and crude. Finally, there is a UISnapBehavior. In fact, this is simpler, and the code is directly written according to the Convention.
    _snap = [[UISnapBehavior alloc] initWithItem:testView snapToPoint:CGPointMake(,      [_animator addBehavior:_snap];

 

Run the program and you will find that the square suddenly seems to be pinned somewhere. That's right. This is the effect of UISnapBehavior. No need to explain more. It also has only one property damping, directly adding this line:
     [_snap setDamping:];

 

Block Movement slows down. This is a damping attribute. After research, the implementation is actually very simple. This series of Apis really help us to simply implement these interesting effects. The materials are here, and you can simply take a look at what you cook!

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.