iOS Development Uikit Dynamics Uidynamicanimator a series of animations

Source: Internet
Author: User

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

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.