Some basic animation effects of layers and layer animation Effects
# Define kRadianToDegrees (radian) (radian * 180.0)/(M_PI)
// Flashing
[Self. testView. layer addAnimation: [self opacityForever_Animation: 0.5] forKey: nil];
// Move
[Self. testView. layer addAnimation: [self duration: 3 move: [NSNumber numberWithInteger: 200] forKey: nil];
// Zoom
[Self. testView. layer addAnimation: [self scale: [NSNumber numberWithInteger: 1] orgin: [NSNumber numberWithInteger: 3] durTimes: 1 Rep: MAXFLOAT] forKey: nil];
// Combination
NSArray * myArray = [NSArray arrayWithObjects: [self increment: 0.5], [self duration: 1.0f move: [NSNumber numberWithFloat: 2001_f], [self scale: [NSNumber numberWithFloat: 1.0f] orgin: [NSNumber numberWithFloat: 3.0f] durTimes: 2.0f Rep: MAXFLOAT], nil];
[Self. testView. layer addAnimation: [self groupAnimation: myArray durTimes: 3.0f Rep: MAXFLOAT] forKey: nil];
// Path
CGMutablePathRef mypha = CGPathCreateMutable ();
CGPathMoveToPoint (mypha, nil, 30, 77 );
CGPathAddCurveToPoint (myp2, nil, 50, 50, 60,200,200,200); // here is the control point.
[Self. testView. layer addAnimation: [self keyframeAnimation: mypa durTimes: 5 Rep: MAXFLOAT] forKey: nil];
// Rotate
[Self. testView. layer addAnimation: [self rotation: 2 degree: kRadianToDegrees (90) direction: 1 repeatCount: MAXFLOAT] forKey: nil];
// Remove
[Self. testView. layer removeAllAnimations];
# Pragma mark ==== blinking ======
-(CABasicAnimation *) opacityForever_Animation :( float) time
{
CABasicAnimation * animation = [CABasicAnimation animationWithKeyPath: @ "opacity"]; // you must write opacity.
Animation. fromValue = [NSNumber numberWithFloat: 1.0f];
Animation. toValue = [NSNumber numberWithFloat: 0.0f]; // This is transparency.
Animation. autoreverses = YES;
Animation. duration = time;
Animation. repeatCount = MAXFLOAT;
Animation. removedOnCompletion = NO;
Animation. fillMode = kCAFillModeForwards;
Animation. timingFunction = [CAMediaTimingFunction functionWithName: kCAMediaTimingFunctionEaseIn]; // If not, the animation is even.
Return animation;
}
# Pragma mark ==== horizontal and vertical movement ================
-(CABasicAnimation *) duration :( float) time move :( NSNumber *) x
{
CABasicAnimation * animation = [CABasicAnimation animationWithKeyPath: @ "transform. translation. x"]; //. y moves down.
Animation. toValue = x;
Animation. duration = time;
Animation. removedOnCompletion = NO; // If yes, the original position is returned.
Animation. repeatCount = MAXFLOAT;
Animation. fillMode = kCAFillModeForwards;
Return animation;
}
# Pragma mark ====== zoom-==================
-(CABasicAnimation *) scale :( NSNumber *) Multiple orgin :( NSNumber *) orginMultiple durTimes :( float) time Rep :( float) repertTimes
{
CABasicAnimation * animation = [CABasicAnimation animationWithKeyPath: @ "transform. scale"];
Animation. fromValue = Multiple;
Animation. toValue = orginMultiple;
Animation. autoreverses = YES;
Animation. repeatCount = repertTimes;
Animation. duration = time; // if not set, there is a default zoom time.
Animation. removedOnCompletion = NO;
Animation. fillMode = kCAFillModeForwards;
Return animation;
}
# Pragma mark ===== composite animation-================
-(CAAnimationGroup *) groupAnimation :( NSArray *) animationAry durTimes :( float) time Rep :( float) repeatTimes
{
CAAnimationGroup * animation = [CAAnimationGroup animation];
Animation. animations = animationAry;
Animation. duration = time;
Animation. removedOnCompletion = NO;
Animation. repeatCount = repeatTimes;
Animation. fillMode = kCAFillModeForwards;
Return animation;
}
# Pragma mark ===== path animation-==================
-(CAKeyframeAnimation *) keyframeAnimation :( CGMutablePathRef) path durTimes :( float) time Rep :( float) repeatTimes
{
CAKeyframeAnimation * animation = [CAKeyframeAnimation animationWithKeyPath: @ "position"];
Animation. path = path;
Animation. removedOnCompletion = NO;
Animation. fillMode = kCAFillModeForwards;
Animation. timingFunction = [CAMediaTimingFunction functionWithName: kCAMediaTimingFunctionEaseIn];
Animation. autoreverses = NO;
Animation. duration = time;
Animation. repeatCount = repeatTimes;
Return animation;
}
# Pragma mark ==== rotating animation ======
-(CABasicAnimation *) rotation :( float) dur degree :( float) degree direction :( int) direction repeatCount :( int) repeatCount
{
CATransform3D rotationTransform = CATransform3DMakeRotation (degree, 0, 0, direction );
CABasicAnimation * animation = [CABasicAnimation animationWithKeyPath: @ "transform"];
Animation. toValue = [NSValue valueWithCATransform3D: rotationTransform];
Animation. duration = dur;
Animation. autoreverses = NO;
Animation. cumulative = NO;
Animation. fillMode = kCAFillModeForwards;
Animation. repeatCount = repeatCount;
Animation. delegate = self;
Return animation;
}