IOS: The basic animation of the core animation cabasicanimation

Source: Internet
Author: User

Basic animation, a subclass of CapropertyanimationProperty Description:Fromvalue:keypath The initial value of the corresponding propertyTovalue:keypath The end value of the corresponding propertyAnimation Process Description:as the animation progresses, the value of the keypath corresponding property is gradually changed from Fromvalue to Tovalue during the duration of the duration length.KeyPath content is an animated animatable property of Calayerif Fillmode=kcafillmodeforwards is removedoncomletion=no at the same time, after the animation finishes, the layer remains displayed after the animation is executed. But in essence, the property value of the layer is still the initial value before the animation is executed, and it is not really changed. the specific demo examples are as follows:Feature Implementation: Click the button in the view, you can resume the controller animation process and run to stop, wait until the animation runs to the specified location, the animation will stop here, not return. Operation steps:1. Drag a button control into the view of the storyboard controller, name Play/stop, and then associate the button event (change the button's tag, call play or Pause method)2. Create a core animation sub-layer and add it to the parent core animation layer, then create a tap touch gesture and handle the touch event (the process of creating the animation)3, complete all previously defined events, and implement the animation protocol. the specific code is as follows://Drag controls, associating events  //In-(void) viewdidload{} method, create a child layer and add it to the parent layer
 //  Create child layer  Self.sublayer = [Calayer layer]; Self.subLayer.bounds  = CGRectMake (0 , 0 , 100 , 100         ); Self.subLayer.position  = cgpointmake (100 , 100         );        Self.subLayer.backgroundColor  = [[Uicolor Redcolor]cgcolor];        Self.subLayer.cornerRadius  = 50  ; [Self.view.layer AddSublayer:self.subLayer];  
//In-(void) viewdidload{} method, create gesture, add gesture event
    // Create a tap gesture    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initwithtarget:self Action: @selector (tap:)];     1 ;     1 ;    [Self.view Addgesturerecognizer:tap];
//handling gesture events, creating basic animations
#pragma mark-tap gesture processing-(voidTAP: (UITapGestureRecognizer *) sender{//get the location of the current clickCgpoint location =[Sender LocationInView:self.view]; //Create a basic animationCabasicanimation *basicanimation =[[Cabasicanimation alloc]init]; Basicanimation.duration=5.0f;
//Set the value of the animation change to position Basicanimation.keypath
=@"position"; //Basicanimation.fromvalue with current position, without settingBasicanimation.tovalue =[Nsvalue valuewithcgpoint:location]; //The animation finishes, stays down, does not return the original value, need to set the following two propertiesBasicanimation.removedoncompletion =NO; Basicanimation.fillmode=kcafillmodeforwards; //Set up proxyBasicanimation.Delegate=Self ; //sets a key to differentiate animation and adds the specified animation to the child layer[Self.sublayer addanimation:basicanimation Forkey:@"basicanimation"]; //without a specific implementation of the Proxy method, the sublayer has moved, but its true position has not changed//NSLog (@ "%@", Nsstringfromcgpoint (self.subLayer.position));}
//Implementing a button event
-(Ibaction) buttonclicked: (UIButton *) sender{ if  (Sender.tag = = 0   //   [self animationpause];    Sender.tag  = 1  ;   else   { //  animation recovery   [self animationresume];    Sender.tag  = 0  ; }}
//Custom Animation Pause method
 //  animation pause -( void  ) animationpause{ //  Get the current pause time  // cftimeinterval pausetime = [    Self.sublayer converttime:cacurrentmediatime () Fromlayer:nil];         Cftimeinterval pausetime = Cacurrentmediatime ();     //         Self.subLayer.speed = 0  < span> //  save pause time for easy recovery  Self.subLayer.timeOffset = Pausetime;}  
//Custom Animation recovery method
//Animation Recovery-(void) animationresume{//gets the time saved when pausedCftimeinterval Pausetime =Self.subLayer.timeOffset; Self.subLayer.timeOffset=0; //Set SpeedSelf.subLayer.speed =1.0; //Clear Start TimeSelf.subLayer.beginTime =0.0; //Calculate start TimeCftimeinterval beginTime = [Self.sublayer converttime:cacurrentmediatime () Fromlayer:nil]-Pausetime; //Reset Start TimeSelf.subLayer.beginTime =BeginTime;}
//Implement animation Agent method
how to #pragma mark-animation agent//the method that is triggered when the animation starts-(void) Animationdidstart: (Caanimation *) anim{//the current value at the beginning//NSLog (@ "animationdisstart:%@", ((Cabasicanimation *) anim). Fromvalue);}//method to trigger when animation stops-(void) Animationdidstop: (Caanimation *) Anim finished: (BOOL) flag{//end value at Stop//NSLog (@ "animationdidstop,%@", ((cabasicanimation*) anim). Tovalue); //The property value of the Sublayer property value is really changed when the animation stopsNsvalue *tovalue = ((cabasicanimation*). Anim). Tovalue; Cgpoint Point=[Tovalue Cgpointvalue]; Self.subLayer.position=Point ;}@end
//Display the interface as follows, (the operation process will not, you can write code validation): 

IOS: The basic animation of the core animation cabasicanimation

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.