IOS Core Animation learning Summary (3) -- basic Animation type, iosanimation
1. CABasicAnimation (basic animation)
Shift:
CABasicAnimation * animation = [CABasicAnimation animation]; // keyPath specifies the animation category, and position indicates the shift of animation. keyPath = @ "position"; // move to the animation position x = 200, y = 200. toValue = [NSValue valueWithCGPoint: CGPointMake (200,200)]; animation. duration = 2; // The animation is not deleted after the animation is executed. removedOnCompletion = NO; // keep the animation status up to date. fillMode = @ "forwards"; // Add an animation [self. layer addAnimation: animation forKey: nil];
Scaling:
CABasicAnimation * animation = [CABasicAnimation animation]; // keyPath specifies the animation category. bounds indicates scaling the animation. keyPath = @ "bounds"; // scale to width = 50, height = 50 animation. toValue = [NSValue valueWithCGRect: CGRectMake (0, 0, 50, 50)]; animation. duration = 2; // animation not deleted after the animation is completed. removedOnCompletion = NO; // keep the animation status up to date. fillMode = @ "forwards"; [self. layer addAnimation: animation forKey: nil];
Rotation:
CABasicAnimation * animation = [CABasicAnimation animation]; // keyPath specifies the animation category. transform indicates rotation of the animation. keyPath = @ "transform"; // rotate 45 degrees animation clockwise along x and Y axes. toValue = [NSValue valueWithCATransform3D: CATransform3DMakeRotation (M_PI_4, 1, 1, 0)]; animation. duration = 2; animation. removedOnCompletion = NO; animation. fillMode = @ "forwards"; [self. layer addAnimation: animation forKey: nil];
Ii. CAKeyframeAnimation (Key Frame Animation)
CAKeyframeAnimation * anim = [CAKeyframeAnimation animation]; anim. keyPath = @ "position"; anim. removedOnCompletion = NO; anim. fillMode = kCAFillModeForwards; anim. duration = 2.0; // set the circular trajectory and move around the circular trajectory CGMutablePathRef path = CGPathCreateMutable (); CGPathAddEllipseInRect (path, NULL, CGRectMake (100,100,200,200); anim. path = path; CGPathRelease (path); // set the animation execution rhythm // kCAMediaTimingFunctionEaseInEaseOut: the process starts slowly and accelerates in the middle. anim is slowed down when the animation ends. timingFunction = [CAMediaTimingFunction functionWithName: kCAMediaTimingFunctionEaseInEaseOut]; anim. delegate = self; [anim setValue: @ "aaa" forKey: @ "TAG"]; [self. layer addAnimation: anim forKey: nil];
Sets the callback method of the proxy. A prompt is displayed after the animation ends.
-(Void) animationDidStop :( CAAnimation *) anim finished :( BOOL) flag {NSString * strTag = [anim valueForKey: @ "TAG"]; if ([strTag isinclutostring: @ "aaa"]) {UIAlertView * alert = [[UIAlertView alloc] initWithTitle: @ "Animation Done" message: @ "action completed" delegate: self cancelButtonTitle: nil otherButtonTitles: @ "OK", nil]; [alert show] ;}}
3. CATransition (Transfer animation)
(1) view jump
_newView = [[UIView alloc] init]; _newView.frame = CGRectMake(100, 100, 100, 100); [self.view addSubview:_newView]; UIView *view1 = [[UIView alloc] init]; view1.frame = CGRectMake(0, 0, 100, 100); view1.backgroundColor = [UIColor yellowColor]; [_newView addSubview:view1]; UIView *view2 = [[UIView alloc] init]; view2.frame = CGRectMake(0, 0, 100, 100); view2.backgroundColor = [UIColor greenColor]; [_newView addSubview:view2];
Add transfer button event handling:
-(IBAction) exchangeView {// transfer animation CATransition * transition = [CATransition animation]; transition. timingFunction = [CAMediaTimingFunction functionWithName: kCAMediaTimingFunctionEaseInEaseOut]; transition. type = @ "pageCurl"; transition. subtype = kCATransitionFromRight; transition. duration = 1; [_ newView exchangeSubviewAtIndex: 0 withSubviewAtIndex: 1]; [_ newView. layer addAnimation: transition forKey: @ "myAnimation"];}
(2) controller jump
-(IBAction) pushView {CATransition * transition = [CATransition animation]; transition. timingFunction = [CAMediaTimingFunction functionWithName: kCAMediaTimingFunctionEaseInEaseOut]; // three-dimensional animation effect transition. type = @ "cube"; [self. navigationController. view. layer addAnimation: transition forKey: @ "navAnimation"]; TestViewController * testVC = [[TestViewController alloc] init]; [self. navigationController showViewController: testVC sender: nil];}
4. CAAnimationGroup (composite animation)
// Add image UIImageView * imgView = [[UIImageView alloc] initWithImage: [UIImage imageNamed: @ "curry.jpg"]; imgView. frame = CGRectMake (100,100, imgView. frame. size. width, imgView. frame. size. height); [self. view addSubview: imgView]; // besserl curve path * movePath = [describezierpath]; [movePath moveToPoint: CGPointMake (10.0, 10.0)]; [movePath addQuadCurveToPoint: CGPointMake (100,300) controlPoint: CGPointMake (300,100)]; // The following must be imported into the QuartzCore package // Key Frame Animation (location) CAKeyframeAnimation * posAnim = [CAKeyframeAnimation animationWithKeyPath: @ "position"]; posAnim. path = movePath. CGPath; posAnim. removedOnCompletion = YES; // scaling animation CABasicAnimation * scaleAnim = [CABasicAnimation animationWithKeyPath: @ "transform"]; scaleAnim. fromValue = [NSValue valueWithCATransform3D: CATransform3DIdentity]; scaleAnim. toValue = [NSValue valueWithCATransform3D: CATransform3DMakeScale (0.1, 0.1, 1.0)]; scaleAnim. removedOnCompletion = YES; // transparent animation CABasicAnimation * opacityAnim = [CABasicAnimation animationWithKeyPath: @ "alpha"]; opacityAnim. fromValue = [NSNumber numberWithFloat: 1.0]; opacityAnim. toValue = [NSNumber numberWithFloat: 0.1]; opacityAnim. removedOnCompletion = YES; // animation group CAAnimationGroup * animGroup = [CAAnimationGroup animation]; animGroup. animations = [NSArray arrayWithObjects: posAnim, scaleAnim, opacityAnim, nil]; animGroup. duration = 1; [imgView. layer addAnimation: animGroup forKey: nil];