Overview
Some simple animation agent learning examples, including display, implicit, keyframe, keyframe path four types of animation. (I feel this animation is too complicated to learn a simple example has not continued)
results show
Process Overview
See Code
Main code
viewcontroller.m//animation////Created by Arbboter on 14/12/20.//Copyright (c) 2014 Arbboter. All rights reserved.//#import "ViewController.h" @interface Viewcontroller () @property (nonatomic, retain) uiimageview* Head; @property (nonatomic, retain) uibutton* implicitanimation; @property (nonatomic, retain) uibutton* Explicitanimation, @property (nonatomic, retain) uibutton* keyframeanimation; @property (nonatomic, retain) uibutton* resetheatstate; @end @implementation viewcontroller-(void) resethead{[_head Removefromsuperview]; [_head release]; _head = nil; _head = [[Uiimageview alloc] init]; _head.image = [UIImage imagenamed:@ "Head.png"]; _head.contentmode = Uiviewcontentmodescaleaspectfit; [Self.view Addsubview:_head]; /** set initial value small and translucent */_head.frame = CGRectMake (Ten, _head.image.size.width, _head.image.size.height); _head.transform = Cgaffinetransformmakescale (0.1, 0.1); _head.center = Cgpointmake (self.view.center.x, _head.frame.size. height/2+20); _head.alpha = 0.03;} -(void) viewdidload{[Super Viewdidload]; [Self resethead]; uibutton* btn = nil; /** reset button */btn = [[UIButton alloc] Initwithframe:cgrectmake (Ten, self.view.frame.size.height-40, 80, 30)]; [btn Settitlecolor:[uicolor Blackcolor] forstate:uicontrolstatenormal]; [btn settitle:@ "reset animation" forstate:uicontrolstatenormal]; Btn.layer.borderWidth = 1; Btn.layer.cornerRadius = 10; [Btn addtarget:self Action: @selector (onanimate:) forcontrolevents:uicontroleventtouchupinside]; [Self.view ADDSUBVIEW:BTN]; _resetheatstate = BTN; /** Implicit animation button */btn = [[UIButton alloc] Initwithframe:cgrectmake (_resetheatstate.frame.origin.x + _resetheatstate.frame.s Ize.width + ten, self.view.frame.size.height-40, 80, 30)]; [btn Settitlecolor:[uicolor Blackcolor] forstate:uicontrolstatenormal]; [Btn settitle:@ "implicit animation" forstate:uicontrolstatenormal]; Btn.layer.borderWidth = 1; Btn.layer.cornerRadius = 10; [Btn Addtarget:seLF Action: @selector (onanimate:) forcontrolevents:uicontroleventtouchupinside]; [Self.view ADDSUBVIEW:BTN]; _implicitanimation = BTN; /** Display Animation button */btn = [[UIButton alloc] Initwithframe:cgrectmake (_implicitanimation.frame.origin.x + _implicitanimation.f Rame.size.width + ten, self.view.frame.size.height-40, 80, 30)]; [btn Settitlecolor:[uicolor Blackcolor] forstate:uicontrolstatenormal]; [BTN settitle:@ "Display animation" forstate:uicontrolstatenormal]; Btn.layer.borderWidth = 1; Btn.layer.cornerRadius = 10; [Btn addtarget:self Action: @selector (onanimate:) forcontrolevents:uicontroleventtouchupinside]; [Self.view ADDSUBVIEW:BTN]; _explicitanimation = BTN; /** keyframe Animation button */btn = [[UIButton alloc] Initwithframe:cgrectmake (_explicitanimation.frame.origin.x + _explicitanimation. Frame.size.width + ten, self.view.frame.size.height-40, 80, 30)]; [btn Settitlecolor:[uicolor Blackcolor] forstate:uicontrolstatenormal]; [Btn settitle:@ "Keyframe" Forstate:uicontrolstatEnormal]; Btn.layer.borderWidth = 1; Btn.layer.cornerRadius = 10; [Btn addtarget:self Action: @selector (onanimate:) forcontrolevents:uicontroleventtouchupinside]; [Self.view ADDSUBVIEW:BTN]; _keyframeanimation = BTN;} -(void) Onanimate: (ID) sender{cgpoint headcenter = _head.center; Cgpoint viewcenter = Self.view.center; if (sender = = _implicitanimation) {/** implicit animation */[UIView beginanimations:nil context:null]; _head.layer.affinetransform = cgaffinetransformmaketranslation (0, VIEWCENTER.Y*3/2-HEADCENTER.Y); _head.layer.affinetransform = Cgaffinetransformscale (_head.layer.affinetransform, 1, 1); _head.layer.opacity = 1; [UIView commitanimations]; } else if (sender = = _explicitanimation) {/** display animation *//** Add the first animation "from transparent gradient to clear" use built-in animation effect: Opacity * /Cabasicanimation *opanim = [cabasicanimation animationwithkeypath:@ "opacity"]; Opanim.duration = 5.0; Opanim.fromvalue = [NSNumber numberwithfloat:0.1]; opanim.tovalue= [NSNumber numberwithfloat:1.0]; Opanim.cumulative = YES; Opanim.repeatcount = 1; [_head.layer addanimation:opanim forkey:@ "animateopacity"]; /** Add a second animation pan and grow larger using the built-in animation effect: Transform */Cgaffinetransform t = cgaffinetransformmaketranslation (0, VIEWCENTER.Y*3/2 -HEADCENTER.Y); t = Cgaffinetransformscale (T, 1.5, 1.5); Cabasicanimation *animatation = [cabasicanimation animationwithkeypath:@ "transform"]; Animatation.duration = opanim.duration; animatation.tovalue= [Nsvalue valuewithcatransform3d:catransform3dmakeaffinetransform (t)]; [_head.layer addanimation:animatation forkey:@ "Animatetransform"]; } else if (sender = = _keyframeanimation) {/** keyframe animation */if (arc4random ()%2) { Cakeyframeanimation *opanim = [cakeyframeanimation animationwithkeypath:@ "opacity"]; opanim.duration = 6.0; Opanim.values =[nsarray arraywithobjects: [NSNumber numberwithfloat:0.0], [NSNumber numberwithfloat:0.1], [NSNumber numberwithfloat:0.3], [NSNumber numberwithfloat:1.0], nil]; Opanim.keytimes = [Nsarray arraywithobjects: [NSNumber numberwithfloat:0.0], [NSNumber numberwithfloat:0.3], [NSNumber numberwithfloat:0.6], [NSNumber numberwithfloat:1.0], nil]; [_head.layer addanimation:opanim forkey:@ "keyanimateopacity"]; Cgaffinetransform t = cgaffinetransformmaketranslation (0, VIEWCENTER.Y*3/2-HEADCENTER.Y); t = Cgaffinetransformscale (T, 1.5, 1.5); Cabasicanimation *animatation = [cabasicanimation animationwithkeypath:@ "transform"]; Animatation.duration = opanim.duration; animatation.tovalue= [Nsvalue valuewithcatransform3d:catransform3dmakeaffinetransform (t) ]; [_head.layer addanimation:animatation forkey:@ "Keyanimatetransform"]; } else {_head.alpha = 1.0; /** keyframe Path Animation */Nsinteger x = self.view.frame.size.width; Nsinteger y = self.view.frame.size.height; int n = 4; Cgmutablepathref path = cgpathcreatemutable (); Cgpathmovetopoint (Path, NULL, headcenter.x, HEADCENTER.Y); for (int i=0; i<n; i++) {cgpathaddlinetopoint (path, NULL, 0, Arc4random ()%y); Cgpathaddlinetopoint (Path, NULL, X, Arc4random ()%y); } cgpathclosesubpath (path); Cakeyframeanimation *animation = nil; Animation = [Cakeyframeanimation animationwithkeypath:@ "position"]; [Animation setduration:n*5]; [Animation setdelegate:self]; [Animation Setpath:path]; Cfrelease (path); Path = nil; [[_head layer] addanimation:animation forkey:@ "position"]; }} else if (sender = = _resetheatstate) {[Self resethead]; }}-(void) dealloc{[_head release]; [_implicitanimation release]; [_explicitanimation release]; [_resetheatstate release]; [Super Dealloc];} @end
Project Engineering
Show animations, implicit animations, key-frame animations