The Uidynamicanimator class, which implements some dynamic characteristics through the different behaviors in this class.
Uiattachmentbehavior (adsorption), Uicollisionbehavior (collision), Uigravitybehavior (Gravity), Uipushbehavior (push), Uisnapbehavior (Capture). There is also an auxiliary behavior Uidynamicitembehavior, which is used to set parameters at the item level, such as the item's friction, resistance, angular resistance, elastic density, permissible rotation, and so on.
1.Uiattachmentbehavior(adsorption):
- (void) viewdidload {[Super viewdidload]; Self.dynamicanimation=[[Uidynamicanimator Alloc]initwithreferenceview:self.view]; //ImageSelf.imgview = [[Uiimageview alloc]initwithframe:cgrectmake ( -, -, -, -)]; Self.imgView.image= [UIImage imagenamed:@"Dizhijieyong_icon"]; [Self.view AddSubview:self.imgView]; Self.imgView.userInteractionEnabled=YES; //Add gesturesUipangesturerecognizer *pangest =[[Uipangesturerecognizer alloc]initwithtarget:self Action: @selector (pangestaction:)]; [Self.imgview Addgesturerecognizer:pangest]; //adsorption offset allows you to set the offsets of the adsorption, anchor is the set anchor pointSelf.attachment = [[Uiattachmentbehavior alloc]initwithitem:self.imgview Offsetfromcenter:uioffsetmake ( -, -) Attachedtoanchor:cgpointmake ( -, -)];}//implement a pan gesture and let an image follow the gesture- (void) Pangestaction: (Uipangesturerecognizer *) pangest{NSLog (@"gesture triggered!!!"); //Access LocationCgpoint location =[Pangest LocationInView:self.view]; Cgpoint boxlocation=[Pangest LocationInView:self.imgView]; Switch(pangest.state) {//Start dragging CaseUigesturerecognizerstatebegan:nslog (@"You touch started position%@", Nsstringfromcgpoint (location)); NSLog (@"Location on image started is%@", Nsstringfromcgpoint (boxlocation)); //removed from[Self.dynamicanimation removeallbehaviors]; Uioffset OffSet= Uioffsetmake (Boxlocation.x-Cgrectgetmidx (self.imgView.bounds), BOXLOCATION.Y-Cgrectgetmidy (self.imgView.bounds)); Self.attachment=[[Uiattachmentbehavior Alloc]initwithitem:self.imgview offsetfromcenter:offset attachedtoanchor:location]; Self.attachment.damping=0.5; Self.attachment.frequency=0.8; [Self.dynamicanimation addBehavior:self.attachment]; Break; Caseuigesturerecognizerstateended: [Self.dynamicanimation removeallbehaviors]; default: [Self.attachment setanchorpoint:location]; Break; }}
2.uigravitybehavior(Gravity):
//implement code that drops a picture on the screen- (void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (Uievent *)Event{self.gravity=[[Uigravitybehavior Alloc]initwithitems:nil]; [Self.dynamicanimation addBehavior:self.gravity]; Uiimageview*gravimagview = [[Uiimageview alloc]initwithframe:cgrectmake ( -, -, -, -)]; Gravimagview.image= [UIImage imagenamed:@"Zichanlingyong_icon"]; [Self.view Addsubview:gravimagview]; Gravimagview.frame= CGRectMake ( -, -, -, -); [Self.gravity Additem:gravimagview]; }
3.Uisnapbehavior(capture):
// Uisnapbehavior UIView by animation to a point -(void) Handletap: (UITapGestureRecognizer *) paramtap{ = [Paramtap locationInView:self.view]; if (Self.snap! = nil) { [self.dynamicanimation removeBehavior:self.snap] ; } = [[Uisnapbehavior alloc] InitWithItem:self.imgView snaptopoint:tappoint]; 0.5f; // The extent of the play list [Self.dynamicanimation addBehavior:self.snap];}
Some have not tried, have time to add it!!! Hee ~~~~~
iOS Development Uikit Dynamics Uidynamicanimator a series of animations