What is the power effect?

Source: Internet
Author: User

What is the power effect?

First, a UIDynamicAnimator animated painter will give him a UIDynamicBehavior power effect, and add the UIDynamicItem (UIView) power element to the power effect.

Dynamic elements: all elements that comply with the UIDynamicItem protocol can be regarded as dynamic elements.

The painter of the UIDynamicAnimator animation needs a range ()

Their link structure is as follows:

Dynamic Behavior:

1. Introduction: belongs to UIKit

(1) What is dynamic behavior? -> Simulate mechanics-related animation and Interaction Systems in the real world

(2) Possible effects:-> gravity, collision, adsorption, push, capture, and combination

(3) iOS9 UIDynamicItemGroup can uniformly Add dynamic behavior to a group of elements

(4) iOS9 can be defined as a spherical contact with another border or element.

2. Class Name introduction:

(1) UIDynamicAnimator: animation creator of the Dynamic Effect

Attribute introduction:

① InitWithReferenceView: initialize a power effect consumer and create a reference View

② AddBehavior: Add dynamic behavior

③ RemoveBehavior: Remove a dynamic behavior.

④ RemoveAllBehaviors remove all dynamic behaviors

⑤ Delegate proxy

(2) UIDynamicBehavior: the animation action of the dynamic effect. The animation action of the dynamic effect will affect the attribute of the dynamic element (frame)

There are six major dynamic behaviors:

1 UIGravityBehavior gravity effect Behavior

① InitWithItems: initializes the gravity effect behavior and specifies the target object

② AddItem: add the target object of gravity effect

③ RemoveItem: remove the target object.

④ GravityDirection direction of gravity

CGVector: struct value of the vector: 0-1

X: horizontal gravity

Y: vertical gravity

The number of pixels dropped by 1000 per second due to acceleration and radians

⑤ Angle: change the angle of gravity effect

6 magn133: The acceleration level is 1.0, which indicates that the acceleration is 1000 points/second 2.

7. setAngle: magnangle: Set the angle and speed of the gravity direction.

2 UICollisionBehavior collision Behavior

① InitWithItems: initialization

② CollisionMode: collision style (Mode)

UICollisionBehaviorModeItems element collision

UICollisionBehaviorModeBoundaries border collision

UICollisionBehaviorModeEverything

③ CollisionDelegate: proxy

④ Proxy method called during element collision

-(Void) collisionBehavior :( UICollisionBehavior *) behavior beganContactForItem :( id <UIDynamicItem>) item1 withItem :( id <UIDynamicItem>) item2 atPoint :( CGPoint) p;

-(Void) collisionBehavior :( UICollisionBehavior *) behavior endedContactForItem :( id <UIDynamicItem>) item1 withItem :( id <UIDynamicItem>) item2;

⑤ Proxy method called when the elements collide with the border

-(Void) collisionBehavior :( UICollisionBehavior *) behavior beganContactForItem :( id <UIDynamicItem>) item identifier :( nullable id <NSCopying>) identifier atPoint :( CGPoint) p;

-(Void) collisionBehavior :( UICollisionBehavior *) behavior endedContactForItem :( id <UIDynamicItem>) item withBoundaryIdentifier :( nullable id <NSCopying>) identifier;

⑥ AddBoundaryWithIdentifier: forPath: Add path as border

7-(nullable UIBezierPath *) boundaryWithIdentifier :( id <NSCopying>) identifier

⑧ AddBoundaryWithIdentifier: fromPoint: toPoint: add a line as the border

⑨ RemoveBoundaryWithIdentifier

Whether ⑩ translatesReferenceBoundsIntoBoundary uses the reference view as the Boundary

⑪ SetTranslatesReferenceBoundsIntoBoundaryWithInsets: Set the inner distance of the reference view.

3 uipus?havior promotes Behavior

4 UISnapBehavior moved fast

5 UIAttachmentBehavior appendix and Behavior

6 UIDynamicItemBehavior Element Behavior

(3) UIDynamicItem dynamic element

The above is an introduction to dynamic behaviors. The specific operations are as follows:

First, create a UIImageView in Main. sroryBoard, set the full screen attribute, allow user interaction, and image. Drag the ImageView to the ViewController. m file.

@ Interface

# Import "ViewController. h "@ interface ViewController () <UIDynamicAnimatorDelegate, UICollisionBehaviorDelegate> // The Force effect element @ property (nonatomic, strong) UIImageView * ballItem; @ property (weak, nonatomic) IBOutlet UIImageView * bgView; @ property (nonatomic, strong) UIDynamicAnimator * animatior; // the image that appears when it hits the border @ property (nonatomic, strong) UIImageView * dungView; @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad]; [self. bgView addSubview: self. ballItem];}-(void) touchesBegan :( NSSet <UITouch *> *) touches withEvent :( UIEvent *) event {self. ballItem. center = [[touches anyObject] locationInView: self. view];}-(void) touchesMoved :( NSSet <UITouch *> *) touches withEvent :( UIEvent *) event {self. ballItem. center = [[touches anyObject] locationInView: self. view];} // After dragging, let the ball drop-(void) touchesEnded :( NSSet <UITouch *> *) touches withEvent :( UIEvent *) event {NSLog (@ "Y: % f ", self. ballItem. frame. origin. y); [self gravityBehavior]; [self collisionBehavior];} # pragma mark ------- gravity effect-(void) gravityBehavior {// remove the previous effect [self. animatior removeAllBehaviors]; UIGravityBehavior * gravityBehavior = [[UIGravityBehavior alloc] initWithItems: @ [self. ballItem]; gravityBehavior. gravityDirection = CGVectorMake (0.0, 1.0); // gravity direction // gravityBehavior. angle = 30 * M_PI/180; // acceleration will affect the descent speed gravityBehavior. magn133 = 1.5; // Add the gravity effect to the operator of the dynamic effect [self. animatior addBehavior: gravityBehavior] ;}# pragma mark ----- collision detection behavior-(void) collisionBehavior {comment * collisionBehavior = [[UICollisionBehavior alloc] initWithItems: @ [self. ballItem]; // set the collision style (pattern) collisionBehavior. collisionMode = UICollisionBehaviorModeEverything; // whether to use the reference view as the boundary // collisionBehavior. inline = YES; // circle // UIBezierPath * path = [UIBezierPath paths: CGRectMake (20,300,300,300) cornerRadius: 150]; // [collisionBehavior addBoundaryWithIdentifier: @ "round" forPath: path]; // draw line [collisionBehavior addBoundaryWithIdentifier: @ "line" fromPoint: CGPointMake (0,680) toPoint: CGPointMake (414,680)]; collisionBehavior. collisionDelegate = self; [self. animatior addBehavior: collisionBehavior] ;}# pragma mark -------- collision behavior proxy method // collision between elements-(void) collisionBehavior :( behavior *) behavior beganContactForItem :( id <UIDynamicItem>) item1 withItem :( id <UIDynamicItem>) item2 atPoint :( CGPoint) p {}-(void) collisionBehavior :( UICollisionBehavior *) behavior endedContactForItem :( id <UIDynamicItem>) item1 withItem :( id <UIDynamicItem>) item2 {}// collision between the element and the border-(void) collisionBehavior :( UICollisionBehavior *) behavior beganContactForItem :( id <UIDynamicItem>) item withBoundaryIdentifier :( nullable id <NSCopying>) identifier atPoint :( CGPoint) p {NSLog (@ ""); self. dungView. center = CGPointMake (p. x-20, p. y-20); // [self. dungView addSubview: self. ballItem]; [self. bgView exchangeSubviewAtIndex: 1 withSubviewAtIndex: 0];}-(void) collisionBehavior :( behavior *) behavior :( id <UIDynamicItem>) item identifier :( nullable id <NSCopying>) identifier {NSLog (@ "end contact border"); [behavior removeBoundaryWithIdentifier: @ "line"]; [self collisionBehavior] ;}# pragma mark -------- operator (Power Effect operator) -(void) dynamicAnimatorWillResume :( UIDynamicAnimator *) animator {NSLog (@ "");}-(void) dynamicAnimatorDidPause :( UIDynamicAnimator *) animator {NSLog (@ "pause");}-(UIImageView *) ballItem {if (_ ballItem) {return _ ballItem;} _ ballItem = [[UIImageView alloc] initWithFrame: CGRectMake (100, 50, 50, 50)]; _ ballItem. image = [UIImage imageNamed: @ ""]; return _ ballItem;}-(UIDynamicAnimator *) animatior {if (_ animatior) {return _ animatior ;} _ animatior = [[UIDynamicAnimator alloc] initWithReferenceView: self. view]; _ animatior. delegate = self; return _ animatior;}-(UIImageView *) dungView {if (_ dungView) {return _ dungView;} UIImage * image = [UIImage imageNamed: @ "keng"]; _ dungView = [[UIImageView alloc] initWithFrame: CGRectMake (0,680, image. size. width/2.5, image. size. height/2.5)]; _ dungView. image = image; [self. bgView addSubview: _ dungView]; return _ dungView;} @ end

The running effect is that after you drag the ball, a pitfall will appear. ,

The effect is as follows:

Initial Location

Status of Drop completed

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.