-(void)setAnimation:(NSIndexPath *) indexPath{
switch (indexPath.section) {
case 0:{
[self setLayerAnimation:indexPath.row];
}
break;
case 1:
{
[self setUIViewAnimation:indexPath.row];
}
break;
case 2:
{
[self setCATransition:indexPath.row];
}
break;
case 3:
{
[self setCABasicAnimation:indexPath.row];
}
break;
case 4:
{
[self setCAKeyframeAnimation:indexPath.row];
}
break;
case 5:
{
[self setCAAnimationGroup:indexPath.row];
}
break;
case 6:{
[self setCATransfrom3D:indexPath.row];
}
break;
default:
break;
}
}
-(void)setLayerAnimation:(NSInteger) row{
switch (row) {
case 0:
{
[CATransaction begin];
[CATransaction setDisableActions:!enableAnimation.isOn];
[CATransaction setAnimationDuration:animationDuration.value];
[_layer setCornerRadius:[_layer cornerRadius]==0?30:0];
[_layer setBorderWidth:[_layer borderWidth]==0?5:0];
[_layer setBorderColor:[UIColor redColor].CGColor];
[CATransaction commit];
}
break;
default:
break;
}
}
//基础动画
-(void)setUIViewAnimation:(NSInteger) row{
switch (row) {
case 0://CurlUp
{
[UIView beginAnimations:@"animationID" context:nil];
[UIView setAnimationDuration:animationDuration.value];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView setAnimationRepeatAutoreverses:enableAnimation.isOn];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:image cache:YES];
[UIView commitAnimations];
}
break;
case 1://FlipFromLeft
{
[UIView beginAnimations:@"animationID" context:nil];
[UIView setAnimationDuration:animationDuration.value];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView setAnimationRepeatAutoreverses:enableAnimation.isOn];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:image cache:YES];
[UIView commitAnimations];
}
break;
case 2://block CurlDown
{
[UIView animateWithDuration:animationDuration.value animations:^(void){
[UIView setAnimationRepeatAutoreverses:enableAnimation.isOn];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:image cache:YES];
}];
}
break;
case 3://block FlipFromRight
{
[UIView animateWithDuration:animationDuration.value delay:.5 options:UIViewAnimationOptionCurveEaseOut animations:^(void){
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:image cache:YES];
} completion:^(BOOL finish){
animationDurationLabel.text = @"动画结束";
}];
}
break;
default:
break;
}
}
-(void)setCATransition:(NSInteger) row{
switch (row) {
case 0://cube
{
CATransition *transtion = [CATransition animation];
// [transtion setStartProgress:0.5];
// [transtion setEndProgress:0.6];
transtion.duration = animationDuration.value;
[transtion setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];
[transtion setType:@"cube"];
[transtion setSubtype:kCATransitionFromTop];
[image.layer addAnimation:transtion forKey:@"transtionKey"];
}
break;
case 1://kCATransitionPush
{
CATransition *transtion = [CATransition animation];
transtion.duration = animationDuration.value;
[transtion setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];
[transtion setType:kCATransitionPush];
[transtion setSubtype:kCATransitionFromTop];
[image.layer addAnimation:transtion forKey:AnimationKey];
}
break;
case 2:
{
CATransition *transtion = [CATransition animation];
transtion.duration = animationDuration.value;
[transtion setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];
[transtion setType:kCATransitionReveal];
[transtion setSubtype:kCATransitionFromTop];
[image.layer addAnimation:transtion forKey:@"transtionKey"];
}
break;
case 3:
{
CATransition *transtion = [CATransition animation];
transtion.duration = animationDuration.value;
[transtion setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];
[transtion setType:kCATransitionMoveIn];
[transtion setSubtype:kCATransitionFromTop];
[transtion setDelegate:self];
[image.layer addAnimation:transtion forKey:@"transtionKey"];
}
break;
case 4://kCATransitionFade
{
CATransition *transtion = [CATransition animation];
transtion.duration = animationDuration.value;
[transtion setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
[transtion setType:kCATransitionFade];
[transtion setSubtype:kCATransitionFromTop];
[image setImage:[UIImage imageNamed:@"img.jpeg"]];
[image.layer addAnimation:transtion forKey:AnimationKey];
}
break;
case 5:
{
CATransition *transtion = [CATransition animation];
transtion.duration = animationDuration.value;
[transtion setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];
[transtion setType:@"suckEffect"];
[transtion setSubtype:kCATransitionFromTop];
[image.layer addAnimation:transtion forKey:@"transtionKey"];
}
break;
case 6:
{
CATransition *transtion = [CATransition animation];
transtion.duration = animationDuration.value;
[transtion setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];
[transtion setType:@"oglFlip"];
[transtion setSubtype:kCATransitionFromTop];
[image.layer addAnimation:transtion forKey:@"transtionKey"];
}
break;
case 7:
{
CATransition *transtion = [CATransition animation];
[transtion setStartProgress:.2];
[transtion setEndProgress:.8];
transtion.duration = animationDuration.value;
[transtion setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[transtion setType:@"rippleEffect"];
[transtion setSubtype:kCATransitionFromTop];
[image.layer addAnimation:transtion forKey:@"transtionKey"];
}
break;
case 8://cameraIrisHollowOpen
{
CATransition *transtion = [CATransition animation];
transtion.duration = animationDuration.value;
[transtion setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];
[transtion setType:@"cameraIrisHollowOpen"];
[transtion setSubtype:kCATransitionFromTop];
[image.layer addAnimation:transtion forKey:@"transtionKey"];
}
break;
case 9://cameraIrisHollowClose
{
CATransition *transtion = [CATransition animation];
transtion.duration = animationDuration.value;
[transtion setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];
[transtion setType:@"cameraIrisHollowClose"];
[transtion setSubtype:kCATransitionFromTop];
[image.layer addAnimation:transtion forKey:@"transtionKey"];
}
break;
default:
break;
}
}
-(void)setCABasicAnimation:(NSInteger) row{
switch (row) {
case 0:
{
CABasicAnimation *basic = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
[basic setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
[basic setFromValue:[NSNumber numberWithFloat:1]];
[basic setToValue:[NSNumber numberWithFloat:.3]];
[basic setDuration:animationDuration.value];
[basic setDelegate:self];
[image.layer addAnimation:basic forKey:AnimationKey];
}
break;
// case 1:
// {
// CABasicAnimation *basic = [CABasicAnimation animationWithKeyPath:@"opacity"];
// [basic setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
// [basic setFromValue:[NSNumber numberWithFloat:1]];
// [basic setToValue:[NSNumber numberWithFloat:.3]];
// [basic setDuration:animationDuration.value];
// [image.layer addAnimation:basic forKey:AnimationKey];
// }
// break;
case 1:
{
// CABasicAnimation *basic=[CABasicAnimation animationWithKeyPath:@"shadowColor"];
// basic.fromValue=(id)[UIColor redColor].CGColor;
// basic.toValue=(id)[UIColor greenColor].CGColor;
// basic.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
// basic.duration=animationDuration.value;
// [image.layer addAnimation:basic forKey:AnimationKey];
// self.view.layer.transform=CATransform3DMakeRotation(M_PI, 0,0,1);
CABasicAnimation *animation = [ CABasicAnimation
animationWithKeyPath: @"transform" ];
animation.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];
//围绕Z轴旋转,垂直与屏幕
animation.toValue = [ NSValue valueWithCATransform3D:
CATransform3DMakeRotation(45.0f * M_PI / 180.0f,1.0, 0,0) ];
animation.duration = 1;
// //旋转效果累计,先转180度,接着再旋转180度,从而实现360旋转
// animation.cumulative = YES;
// animation.repeatCount = 2;
//在图片边缘添加一个像素的透明区域,去图片锯齿
// CGRect imageRrect = CGRectMake(0, 0,image.frame.size.width, image.frame.size.height);
// UIGraphicsBeginImageContext(imageRrect.size);
// [image.image drawInRect:CGRectMake(1,1,image.frame.size.width-2,image.frame.size.height-2)];
// image.image = UIGraphicsGetImageFromCurrentImageContext();
// UIGraphicsEndImageContext();
animation.fillMode=kCAFillModeForwards;
animation.removedOnCompletion = NO;
[image.layer addAnimation:animation forKey:AnimationKey];
}
default:
break;
}
}
-(void)setCAKeyframeAnimation:(NSInteger) row{
switch (row) {
case 0:
{
CAKeyframeAnimation *keyframe = [CAKeyframeAnimation animationWithKeyPath:@"borderWidth"];
keyframe.values = [NSArray arrayWithObjects:[NSNumber numberWithFloat:0],
[NSNumber numberWithFloat:5],
[NSNumber numberWithFloat:10],
[NSNumber numberWithFloat:15],nil];
keyframe.repeatCount = MAXFLOAT;
keyframe.autoreverses = enableAnimation.isOn;
keyframe.duration = animationDuration.value;
[image.layer addAnimation:keyframe forKey:AnimationKey];
}
break;
case 1://position
{
UIBezierPath *path = [UIBezierPath bezierPath];
// [path moveToPoint:image.frame.origin];
[path moveToPoint:CGPointMake(image.frame.origin.x + image.frame.size.width/2, image.frame.origin.y + image.frame.size.height/2)];
[path addLineToPoint:CGPointMake(image.frame.origin.x + image.frame.size.width/2,400)];
[path addLineToPoint:CGPointMake(20, 400)];
CAKeyframeAnimation *keyframe = [CAKeyframeAnimation animationWithKeyPath:@"position"];
keyframe.path = path.CGPath;
keyframe.duration = animationDuration.value;
[image.layer addAnimation:keyframe forKey:AnimationKey];
}
break;
default:
break;
}
}
-(void)setCAAnimationGroup:(NSInteger) row{
CAAnimationGroup *group = [CAAnimationGroup animation];
CABasicAnimation *basic = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
[basic setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
[basic setFromValue:[NSNumber numberWithFloat:1]];
[basic setToValue:[NSNumber numberWithFloat:.3]];
CAKeyframeAnimation *keyframe = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation.x"];
keyframe.values = [NSArray arrayWithObjects:[NSNumber numberWithFloat:0],[NSNumber numberWithFloat:M_PI], nil];
[group setDuration:animationDuration.value];
[group setAnimations:[NSArray arrayWithObjects:basic,keyframe, nil]];
[image.layer addAnimation:group forKey:AnimationKey];
}
-(void)setCATransfrom3D:(NSInteger) row{
switch (row) {
case 0:
{
[UIView animateWithDuration:animationDuration.value animations:^{
[UIView setAnimationRepeatCount:MAXFLOAT];
[UIView setAnimationRepeatAutoreverses:enableAnimation.isOn];
CATransform3D transform = CATransform3DMakeTranslation(0, -150, 0);
CATransform3D trans = CATransform3DScale(transform, 1.5, 1.5, 10);
[label.layer setTransform:trans];
} completion:^(BOOL finished) {
animationDurationLabel.text = @"finished";
}];
}
break;
case 1:{
CABasicAnimation *basic = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
[basic setDuration:animationDuration.value];
[basic setRepeatCount:MAXFLOAT];
[basic setAutoreverses:enableAnimation.isOn];
NSValue *valueForm = [NSValue valueWithCATransform3D:CATransform3DIdentity];
CATransform3D transTo = CATransform3DMakeScale(.5, .5, 0);
NSValue *valueTo = [NSValue valueWithCATransform3D:transTo];
[basic setFromValue:valueForm];
[basic setToValue:valueTo];
[image.layer addAnimation:basic forKey:AnimationKey];
}
break;
case 2:{
CAKeyframeAnimation *keyframe = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
[keyframe setRepeatCount:MAXFLOAT];
[keyframe setDuration:animationDuration.value];
[keyframe setAutoreverses:enableAnimation.isOn];
CATransform3D transForm = CATransform3DIdentity;
CATransform3D transTo = CATransform3DMakeScale(.5, .5, 0);
NSValue *valueForm = [NSValue valueWithCATransform3D:transForm];
NSValue *valueTo = [NSValue valueWithCATransform3D:transTo];
[keyframe setValues:[NSArray arrayWithObjects:valueTo,valueForm,nil]];
[image.layer addAnimation:keyframe forKey:AnimationKey];
}
break;
case 3:{
CABasicAnimation *basic = [CABasicAnimation animationWithKeyPath:@"transform"];
[basic setRepeatCount:MAXFLOAT];
[basic setDuration:animationDuration.value];
[basic setAutoreverses:enableAnimation.isOn];
NSValue *valueForm = [NSValue valueWithCATransform3D:CATransform3DIdentity];
CGAffineTransform affine = CGAffineTransformMakeTranslation(0, -150);
CATransform3D t = CATransform3DMakeAffineTransform(affine);
CATransform3D trans = CATransform3DScale(t, 1.5, 1.5, 10);
NSValue *valueTo = [NSValue valueWithCATransform3D:trans];
[basic setFromValue:valueForm];
[basic setToValue:valueTo];
[label.layer addAnimation:basic forKey:AnimationKey];
}
break;
default:
break;
}
}
-(void)animationDidStart:(CAAnimation *)anim{
animationDurationLabel.text = @"开始动画";
}
-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
animationDurationLabel.text = @"动画完毕";
}
- (IBAction)animationDurationChanged:(id)sender {
[image.layer removeAnimationForKey:AnimationKey];
[label.layer removeAllAnimations];
animationDuration = (UISlider *)sender;
[animationDurationLabel setText:[NSString stringWithFormat:@"%1.1f", animationDuration.value]];
}