1.
#import "ViewController.h"@interfaceViewcontroller () <UICollisionBehaviorDelegate>{uidynamicanimator*_animator;//Mechanics AnimationUigravitybehavior*_gravity;//Gravity BehaviorUicollisionbehavior *_collision;//Collision Behavior//BOOL _firstcontact;} @property (nonatomic,strong) UIView*Squareview, @property (nonatomic,strong) Uipushbehavior*pushbehavior;//Lapse Behavior@property (Nonatomic,strong) Uisnapbehavior *snapbehavior;//Adsorption Behavior@property (Nonatomic,strong) Uidynamicanimator *danimator;//Mechanics Animation@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; UIView*square = [[UIView alloc]initwithframe:cgrectmake ( -, -, -, -)]; Square.backgroundcolor=[Uicolor Graycolor]; [Self.view Addsubview:square]; _animator=[[Uidynamicanimator Alloc]initwithreferenceview:self.view]; //Add weight behavior_gravity =[[Uigravitybehavior Alloc]initwithitems:@[square]]; [_animator addbehavior:_gravity]; //Add collision Behavior_collision =[[Uicollisionbehavior Alloc]initwithitems:@[square]]; _collision.translatesreferenceboundsintoboundary=YES; [_animator addbehavior:_collision]; //UIView *barrier = [[UIView alloc]initwithframe:cgrectmake (0, -, the, -)]; Barrier.backgroundcolor=[Uicolor Redcolor]; [Self.view Addsubview:barrier]; //Add collision Behavior//_collision = [[Uicollisionbehavior alloc]initwithitems:@[square,barrier]];//_collision.translatesreferenceboundsintoboundary = YES;//[_animator addbehavior:_collision]; //add a collision boundary to a collision behaviorCgpoint Rightedge = Cgpointmake (barrier.frame.origin.x+barrier.frame.size.width, BARRIER.FRAME.ORIGIN.Y); [_collision addboundarywithidentifier:@"Barrier"FromPoint:barrier.frame.origin Topoint:rightedge]; //************************************part2**************************************_collision.action = ^{//NSLog (@ "%@,%@", Nsstringfromcgaffinetransform (Square.transform), Nsstringfromcgpoint (Square.center)); }; //set up a collision behavior Agent_collision.collisiondelegate =Self ; //Helper Behavior//set their physical properties (such as mass or elasticity coefficients)//Uidynamicitembehavior *itembehaviour = [[Uidynamicitembehavior alloc]initwithitems:@[square]];//itembehaviour.elasticity = 0.6;//Elastic Factor Properties//itembehaviour.friction = 0.6;//Coefficient of friction//itembehaviour.density = 9.8;//density//itembehaviour.resistance = 0.6;//Resistance//itembehaviour.angularresistance = 0.1;//Rotational resistance//itembehaviour.allowsrotation = NO;//Allow rotation//[_animator Addbehavior:itembehaviour]; }#pragmaMark-collisiondelegate methods--(void) Collisionbehavior: (Uicollisionbehavior *) Behavior Begancontactforitem: (ID<UIDynamicItem>) Item Withboundaryidentifier: (ID<NSCopying>) identifier Atpoint: (cgpoint) p{NSLog (@"boundary Contact occurred-%@", identifier); UIView*view = (UIView *) Item; View.backgroundcolor=[Uicolor Yellowcolor]; [UIView animatewithduration:0.3animations:^{View.backgroundcolor=[Uicolor Graycolor]; }]; //Dynamic Add Behavior if(!_firstcontact) {_firstcontact=YES; UIView*square = [[UIView alloc]initwithframe:cgrectmake ( -,0, -, -)]; Square.backgroundcolor=[Uicolor Graycolor]; [Self.view Addsubview:square]; [_collision Additem:square]; [_gravity Additem:square]; //Attachment BehaviorUiattachmentbehavior *attach =[[Uiattachmentbehavior Alloc]initwithitem:view Attac Hedtoitem:square]; [_animator Addbehavior:attach]; }}//////////////////////////////////////////part3 behavior and adsorption behavior-(void) Viewdidappear: (BOOL) animated{[Super viewdidappear:animated]; [Self createsmallsquareview]; [Self creategesturerecognizer]; [Self createanimatorandbehaviors]; }- (void) createsmallsquareview{Self.squareview=[[uiview Alloc] Initwithframe:cgrectmake (0.0f,0.0f,80.0f,80.0f)]; Self.squareView.backgroundColor=[Uicolor Greencolor]; Self.squareView.center=Self.view.center; [Self.view AddSubview:self.squareView];}- (void) creategesturerecognizer{//Reconnaissance View ClickUITapGestureRecognizer *tapgesturerecognizer =[[UITapGestureRecognizer alloc] initwithtarget:self action: @selector (Handletap:)]; [Self.view Addgesturerecognizer:tapgesturerecognizer];}- (void) createanimatorandbehaviors{Self.danimator=[[Uidynamicanimator alloc] initWithReferenceView:self.view]; //Collision BehaviorUicollisionbehavior *collision =[[Uicollisionbehavior alloc] initwithitems:@[self.squareview]; Collision.translatesreferenceboundsintoboundary=YES; [Self.danimator addbehavior:collision]; //Lapse BehaviorSelf.pushbehavior =[[Uipushbehavior alloc] Initwithitems:@[self.squareview] Mode:uipushb Ehaviormodecontinuous];//Lapse mode[Self.danimator AddBehavior:self.pushBehavior]; }/*/ //Passing Behavior-(void) Handletap: (UITapGestureRecognizer *) paramtap{cgpoint tappoint = [Paramtap LocationInView:self.vie W]; Cgpoint squareviewcenterpoint = Self.squareView.center; Set the angle of the passage cgfloat deltax = tappoint.x-squareviewcenterpoint.x; CGFloat deltay = Tappoint.y-squareviewcenterpoint.y; CGFloat angle = atan2 (DeltaY, deltax); [Self.pushbehavior Setangle:angle]; Hook stock CGFloat distancebetweenpoints = sqrt (Pow (tappoint.x-squareviewcenterpoint.x, 2.0) + POW (TAPPOINT.Y- SQUAREVIEWCENTERPOINT.Y, 2.0)); Double pow (double x, double y), computes the Y-power of the base x//double sqrt (double); open Square [Self.pushbehavior setmagnitude:distance BETWEENPOINTS/200.0F]; The size of the thrust (movement speed)//1 magnigude will cause an acceleration of 100/squared seconds, the larger the denominator, the lower the speed}*////Adsorption Behavior- (void) Handletap: (UITapGestureRecognizer *) paramtap{Cgpoint tappoint=[Paramtap LocationInView:self.view]; if(Self.snapbehavior! =Nil) {[Self.danimator removeBehavior:self.snapBehavior]; } Self.snapbehavior=[[Uisnapbehavior alloc] InitWithItem:self.squareView Snaptopoint:tapp OINT]; Self.snapBehavior.damping=0.5f;//The extent of the play list[Self.danimator AddBehavior:self.snapBehavior];}@end
IOS7 Power System