IOS動畫+轉(zhuǎn)場動畫
#import "ViewController.h"
#import "secondViewController.h"
#define kScreenWidth ([[UIScreen mainScreen] bounds].size.width)
#define kScreenHeight ([[UIScreen mainScreen] bounds].size.height)
@interface ViewController ()
@property (nonatomic,strong)UIImageView * img;
@end@implementation ViewController//如果fillMode=kCAFillModeForwards和removedOnComletion=NO,那么在動畫執(zhí)行完畢后瓦盛,圖層會保持顯示動畫執(zhí)行后的狀態(tài)原环。但在實質(zhì)上嘱吗,圖層的屬性值還是動畫執(zhí)行前的初始值谒麦,并沒有真正被改變绕德。//anima.fillMode = kCAFillModeForwards;//anima.removedOnCompletion = NO;
- (void)viewDidLoad {? ??
[super viewDidLoad];? ??
self.img = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 200, 200)];? ??
self.img.center =self.view.center;??
?// self.img.layer.masksToBounds =YES;? ??
self.img.layer.shadowColor = [UIColor lightGrayColor].CGColor;? ??
self.img.layer.shadowOffset = CGSizeMake(7, 7);? ??
self.img.layer.shadowOpacity = 0.70;? ?
?[self.view addSubview:self.img ];? ??
self.img.image = [UIImage imageNamed:@"img1.jpg"];? ? }
//position動畫
- (void)positionup{? ??
CABasicAnimation * anima = [CABasicAnimation animationWithKeyPath:@"position"];? ? anima.fromValue = [NSValue valueWithCGPoint:CGPointMake(kScreenWidth/2, kScreenHeight/2)];? ??
anima.toValue? = [NSValue valueWithCGPoint:CGPointMake(kScreenWidth/2, kScreenHeight/2-300)];? ?
?anima.duration =1.0f;? ??
anima.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];? ?
?[self.img.layer addAnimation:anima forKey:@"positionAnimation"];}
-(void)positiondown{? ??
CABasicAnimation * anima = [CABasicAnimation animationWithKeyPath:@"position"];? ? anima.fromValue = [NSValue valueWithCGPoint:CGPointMake(kScreenWidth/2, kScreenHeight/2)];? ?
?anima.toValue? = [NSValue valueWithCGPoint:CGPointMake(kScreenWidth/2, kScreenHeight/2+300)];? ??
anima.duration = 2.0f;? ?
?anima.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];??
? [self.img.layer addAnimation:anima forKey:@"positionAnimation"];? ? }
- (void)positionleft{? ? ? ??
CABasicAnimation? * anima = [CABasicAnimation animationWithKeyPath:@"position"];? ? anima.fromValue = [NSValue valueWithCGPoint:CGPointMake(kScreenWidth/2, kScreenHeight/2)];? ??
anima.toValue? = [NSValue valueWithCGPoint:CGPointMake(kScreenWidth/2-200, kScreenHeight/2)];? ??
anima.duration =3.0f;? ??
anima.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];? ??
[self.img.layer addAnimation:anima forKey:@"position"];? ? }
- (void)positionright{? ??
CABasicAnimation * anima = [CABasicAnimation animationWithKeyPath:@"position"];? ? anima.fromValue? = [NSValue valueWithCGPoint:CGPointMake(kScreenWidth/2, kScreenHeight/2)];? ?
?anima.toValue? ? = [NSValue valueWithCGPoint:CGPointMake(kScreenWidth/2+200, kScreenHeight/2)];? ?
?anima.duration? = 3.0f;??
? [self.img.layer addAnimation:anima forKey:@"position"];? ? }
-(void)positionzheng{? ??
CAKeyframeAnimation * anima =? [CAKeyframeAnimation animationWithKeyPath:@"position"];? ? NSValue? * value1 = [NSValue valueWithCGPoint:CGPointMake(0, 0)];??
? NSValue? * value2 = [NSValue valueWithCGPoint:CGPointMake(200, 200)];??
? NSValue? * value3 = [NSValue valueWithCGPoint:CGPointMake(200, 400)];? ?
?NSValue? * value4 = [NSValue valueWithCGPoint:CGPointMake(0, 200)];? ? anima.values = @[value1,value2,value3,value4];? ??
anima.duration = 2.0f;? ? ??
anima.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];? ?
?[self.img.layer addAnimation:anima forKey:@"positionAnimation"];? ? }
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{? ??
self.img.hidden = YES;}
- (void)positionbezierpath{? ??
CAKeyframeAnimation * anima = [CAKeyframeAnimation animationWithKeyPath:@"position"];? ? UIBezierPath? ? * path = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, 200, 500)];? ? anima.path = path.CGPath;? ??
anima.duration = 4.0f;? ?
?anima.fillMode = kCAFillModeForwards;? ??
anima.removedOnCompletion = NO;??
? anima.delegate = self;??
? [self.img.layer addAnimation:anima forKey:@"position"];}
//opacity動畫
- (void)opacity{? ? CABasicAnimation * anima = [CABasicAnimation animationWithKeyPath:@"opacity"];? ?
?anima.fromValue = [NSNumber numberWithFloat:1.0f]; ?
?anima.toValue? = [NSNumber numberWithFloat:0.2f];? ??
anima.fillMode = kCAFillModeForwards;? ??
anima.removedOnCompletion = NO;? ?
?anima.duration =1.0f;? ??
[self.img.layer addAnimation:anima forKey:@"opacity"];? ? }
//transfrom.scale動畫
- (void)transformscale{? ??
CABasicAnimation * anima = [CABasicAnimation animationWithKeyPath:@"transform.scale"];? ? anima.toValue? = [NSNumber numberWithFloat:2.0f];? ?
?anima.duration = 3.0f;? ??
anima.fillMode = kCAFillModeForwards;? ??
anima.removedOnCompletion = NO;? ?
?[self.img.layer addAnimation:anima forKey:@"transform.scale"];}
- (void)transformscalex{? ?
?CABasicAnimation * anima = [CABasicAnimation animationWithKeyPath:@"transform.scale.x"];? ? anima.toValue = [NSNumber numberWithFloat:1.0f];? ??
anima.duration = 2.0f;??
? [self.img.layer addAnimation:anima forKey:@"transform.scale.x"];}
- (void)transformscaley{? ?
?CABasicAnimation * anima = [CABasicAnimation? animationWithKeyPath:@"transform.scale.y"];? ? anima.toValue = [NSNumber numberWithFloat:1.0f];??
? anima.duration = 2.0f;? ??
[self.img.layer addAnimation:anima forKey:@"transform.scale.y"];? ? }
//transform.rotation動畫
- (void)rotationz{? ?
?CABasicAnimation * anima = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];??
? anima.toValue = [NSNumber numberWithFloat:M_PI*2];? ??
anima.duration =2.0f;??
? [self.img.layer addAnimation:anima forKey:@"transform.rotation.z"];? ? }
- (void)rotationx{? ?
?CABasicAnimation * anima = [CABasicAnimation animationWithKeyPath:@"transform.rotation.x"];? ??
anima.toValue = [NSNumber numberWithFloat:M_PI*2];? ??
anima.duration = 2.0f;? ?
?[self.img.layer addAnimation:anima forKey:@"transform.rotation.x"];}
- (void)rotationy{? ?
?CABasicAnimation? * anima? = [CABasicAnimation animationWithKeyPath:@"transform.rotation.y"];? ??
anima.toValue = [NSNumber numberWithFloat:M_PI*2];? ??
anima.duration = 2.0f;? ?
?[self.img.layer addAnimation:anima forKey:@"transform.scale.y"];}
- (void)shake{? ??
CAKeyframeAnimation? * anima = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation"];? ??
NSValue * value1 = [NSNumber numberWithFloat:-M_PI/180*4];? ??
NSValue * value2 = [NSNumber numberWithFloat:M_PI/180*4];? ??
NSValue * value3 = [NSNumber numberWithFloat:-M_PI/180*4];? ?
?anima.values = @[value1,value2,value3];? ??
anima.repeatCount = MAXFLOAT;? ?
?[self.img.layer addAnimation:anima forKey:@"transform.rotation"];}
//backgroundColor動畫
- (void)backgroundColor{??
? CABasicAnimation * anima = [CABasicAnimation? animationWithKeyPath:@"backgroundColor"];? ? anima.toValue = (id)[UIColor grayColor].CGColor;? ??
anima.duration =2.0f;? ?
?[self.img.layer addAnimation:anima forKey:@"backgroundColor"];}
//cornerRadius動畫
- (void)cornerRadius{? ??
CABasicAnimation? * anima =? [CABasicAnimation animationWithKeyPath:@"cornerRadius"];? ? anima.toValue? = [NSNumber numberWithFloat:50.0f];??
? anima.duration = 1.0f;? ??
anima.fillMode = kCAFillModeForwards;? ?
?anima.removedOnCompletion = NO;? ?
?[self.img.layer addAnimation:anima forKey:@"cornerRadius"];}
- (void)cornerRadiusl{? ?
?CABasicAnimation * anima = [CABasicAnimation animationWithKeyPath:@"cornerRadius"];? ? anima.toValue = [NSNumber? numberWithFloat:0];? ??
anima.duration =2.0;? ??
anima.fillMode = kCAFillModeForwards;? ?
?anima.removedOnCompletion = NO;?
?? [self.img.layer addAnimation:anima forKey:@"cornerRadius"];}
//圖品切換動畫
- (void)changeImageAnimatedWithView :(UIImageView *)imageV AndImage:(UIImage*)image{? ? CATransition * transition? = [CATransition? animation];? ?
?transition.duration = 2;? ??
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];? ?
?transition.type = @"cameraIrisHollowClose";//kCATransitionReveal;??
? [imageV.layer addAnimation:transition forKey:@"transition"]; ?
?[imageV setImage:image];}
//borderWidth動畫
- (void)borderWidth{? ??
CABasicAnimation * anima = [CABasicAnimation animationWithKeyPath:@"borderWidth"];? ? anima.toValue = [NSNumber numberWithFloat:20.0f];?
?? anima.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault];??
? anima.duration = 1.0f;? ? anima.fillMode = kCAFillModeForwards;? ??
anima.removedOnCompletion = NO;??
? [self.img.layer addAnimation:anima forKey:@"borderWidth"];}
// shadowColor動畫
- (void)shadowColor{? ?
?CABasicAnimation * anima = [CABasicAnimation animationWithKeyPath:@"shadowColor"];? ? anima.toValue = (id)[UIColor redColor].CGColor;? ?
?anima.fillMode= kCAFillModeForwards;? ??
anima.removedOnCompletion =NO;? ??
anima.duration = 2.0f;? ?
?[self.img.layer addAnimation:anima forKey:@"shadowColor"];}
- (void)groupAnimation{? ??
CAKeyframeAnimation * anima = [CAKeyframeAnimation? animationWithKeyPath:@"position"];? ? NSValue * value = [NSValue valueWithCGPoint:CGPointMake(0, 0)];?
?? NSValue * value1 = [NSValue valueWithCGPoint:CGPointMake(20, 20)];? ??
anima.values = @[value,value1];? ? ? ?
?CABasicAnimation * anima1 = [CABasicAnimation animationWithKeyPath:@"opacity"];? ? anima1.fromValue = [NSNumber numberWithFloat:1.0f];? ??
anima1.toValue = [NSNumber numberWithFloat:0.2f];? ? ? ?
?CABasicAnimation? * anima2 = [CABasicAnimation? animationWithKeyPath:@"transform.rotation"];? ?
?anima2.toValue = [NSNumber numberWithFloat:M_PI*4];? ? ? ?
?CAAnimationGroup * group = [CAAnimationGroup animation];??
? group.animations = @[anima,anima1,anima2];? ?
?group.duration = 6.0f;? ? [self.img.layer addAnimation:group forKey:@"animation"];??
? //-如下蚊惯,使用三個animation不分裝成group截型,只是把他們添加到layer宦焦,也有組動畫的效果波闹。----------? ? /**? ??
?CAKeyframeAnimation *anima1 = [CAKeyframeAnimation animationWithKeyPath:@"position"];? ?
?NSValue * value = [NSValue valueWithCGPoint:CGPointMake(0, 0)];??
? NSValue * value1 = [NSValue valueWithCGPoint:CGPointMake(20, 20)];? ??
? ? ? anima1.values = @[value,value1];? ??
?anima1.duration = 4.0f;? ?
?[_wsView.layer addAnimation:anima1 forKey:@"aa"];? ? ??
? ? //縮放動畫??
? CABasicAnimation *anima2 = [CABasicAnimation animationWithKeyPath:@"transform.scale"];? ??
?anima2.fromValue = [NSNumber numberWithFloat:0.8f];? ?
?anima2.toValue = [NSNumber numberWithFloat:2.0f];? ??
?anima2.duration = 4.0f;??
? [_wsView.layer addAnimation:anima2 forKey:@"bb"];? ? ? ? ?
?//旋轉(zhuǎn)動畫? ?
?CABasicAnimation *anima3 = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];? ? anima3.toValue = [NSNumber numberWithFloat:M_PI*4];??
? anima3.duration = 4.0f;? ? [_wsView.layer addAnimation:anima3 forKey:@"cc"];? ? ? ? ? */? ? }
-? (void)timeGroupAnimation{? ?
?CFTimeInterval currentTime = CACurrentMediaTime();? ??
CFTimeInterval time = CACurrentMediaTime();? ??
NSLog(@"%f",time);? ? //位移動畫? ? //位移動畫??
? CAKeyframeAnimation *anima1 = [CAKeyframeAnimation animationWithKeyPath:@"position"];? ? NSValue *value0 = [NSValue valueWithCGPoint:CGPointMake(0, kScreenHeight/2-50)];? ? NSValue *value1 = [NSValue valueWithCGPoint:CGPointMake(kScreenWidth/4, kScreenHeight/2-50)];? ?
?NSValue *value2 = [NSValue valueWithCGPoint:CGPointMake(kScreenWidth/4, kScreenHeight/2+50)];? ?
?NSValue *value3 = [NSValue valueWithCGPoint:CGPointMake(kScreenWidth/2, kScreenHeight/2+50)];? ?
?NSValue *value4 = [NSValue valueWithCGPoint:CGPointMake(kScreenWidth/2, kScreenHeight/2-50)];? ??
anima1.values = [NSArray arrayWithObjects:value0,value1,value2,value3,value4, nil];? ? anima1.beginTime = currentTime;? ??
anima1.duration = 2.0f;? ?
?anima1.fillMode = kCAFillModeForwards;??
? anima1.removedOnCompletion = NO;? ?
?[self.img.layer addAnimation:anima1 forKey:@"aa"];? ? ??
? //縮放動畫? ? CABasicAnimation *anima2 = [CABasicAnimation animationWithKeyPath:@"transform.scale"];? ?
?anima2.fromValue = [NSNumber numberWithFloat:0.8f];??
? anima2.toValue = [NSNumber numberWithFloat:2.0f];? ?
?anima2.beginTime = currentTime+2.0f;??
? anima2.duration = 1.0f;? ??
anima2.fillMode = kCAFillModeForwards;? ??
anima2.removedOnCompletion = NO;??
? [self.img.layer addAnimation:anima2 forKey:@"bb"];? ? ? ?
?//旋轉(zhuǎn)動畫? ?
?CABasicAnimation *anima3 = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];? ?
?anima3.toValue = [NSNumber numberWithFloat:M_PI*4];? ?
?anima3.beginTime = currentTime+3.0f;? ??
anima3.duration = 1.0f;? ?
?anima3.fillMode = kCAFillModeForwards;??
? anima3.removedOnCompletion = NO;? ?
?[self.img.layer addAnimation:anima3 forKey:@"cc"];}
-(void)CATransition{? ?
?CATransition? * anima = [CATransition? animation];? ??
anima.type = kCAAnimationCubic;? ??
anima.duration? = 2.0f;? ? //? ?
?anima.subtype = kCATransitionFromRight; //設(shè)置動畫的方向??
? [self.img setImage:[UIImage imageNamed:@"img2.jpg"]];? ?
?[self.img.layer addAnimation:anima forKey:@"catransition"];}
-(void)privateCATransition{? ??
CATransition *anima = [CATransition animation];? ??
anima.type = @"cube";//設(shè)置動畫的類型? ??
anima.subtype = kCATransitionFromRight; //設(shè)置動畫的方向? ??
anima.duration = 1.0f;? ? ? ??
[self.img.layer addAnimation:anima forKey:@"revealAnimation"];}
//私有api/*fade交叉淡化 過度的效果push新視圖把舊視圖推出去moveIn新視圖移到舊視圖上面reveal將舊視圖移開歹篓,顯示新視圖cube立方體翻滾的效果oglFlip上下左右翻轉(zhuǎn)的效果suckEffect收縮效果庄撮,如一塊布被抽走rippleEffect水滴效果(像是在屏幕上滴了一滴水洞斯,然后泛起一下漣漪)pageCurl向上翻頁效果pageUnCurl向下翻頁效果cameraIrisHollowOpen相機鏡頭打開效果cameraIrisHollowClose相機鏡頭關(guān)閉效果*/
- (void)viewanimation{? ? self.img.transform = CGAffineTransformIdentity;?
?? [UIView animateWithDuration:2.0f animations:^{? ? ? ?
?//移動? ? ? ? self.img.transform = CGAffineTransformMakeTranslation(100, 0);? ? ??
? //旋轉(zhuǎn)? ? ? ? self.img.transform = CGAffineTransformMakeRotation(M_PI);? ??
? ? //縮放? ? ? ? self.img.transform = CGAffineTransformMakeScale(1, 2);? ? ? ? ? ? }];??
? //? ? [UIView animateWithDuration:2.0f animations:^{
//? ? ? ??
//? ? } completion:^(BOOL finished) {
//? ? ? ?
?//? ? }];//??
? //? ? [UIView animateWithDuration:2.0f
//? ? ? ? ? ? ? ? ? ? ? ? ? delay:2.0f
//? ? ? ? ? ? ? ? ? ? ? ? options:UIViewAnimationOptionCurveEaseInOut
// ? ? ? ? ? ? ? ? ? ?animations:^{
//? ? ??
? //? ? } completion:^(BOOL finished) {
//? ? ? ?
?//? ? }];
// ? ?
//? ? [UIView animateKeyframesWithDuration:2.0f
//? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? delay:2.0f
//? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? options:UIViewKeyframeAnimationOptionAutoreverse//? ? ? ? ? ? ? ? ? ? ? ? ? ? ? animations:^{
//? ? ? ??
//? ? } completion:^(BOOL finished) {
//? ? ? ?
?//? ? }];}//動畫組
- (void)viewGroup{? ??
self.img.transform = CGAffineTransformIdentity;??
? [UIView animateWithDuration:2.0 animations:^{? ? ? ?
?CGAffineTransform form = CGAffineTransformMakeScale(2, 2);? ? ? ??
CGAffineTransform form1 = CGAffineTransformRotate(form,M_PI);? ? ? ? ? ? ??
? self.img.transform = CGAffineTransformInvert(form1);? ? }];}
- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent *)event{
[self changeImageAnimatedWithView:self.img AndImage:[UIImage imageNamed:@"img2.jpg"]];
//[self timeGroupAnimation];
//[self shadowColor];
//[self CATransition];
//[self viewanimation];
// [self viewGroup];
}
//轉(zhuǎn)場動畫
- (void)viewCheak{
secondViewController * vic = [[secondViewController alloc]init];
dispatch_group_t group = dispatch_group_create();
dispatch_group_enter(group);
CATransition *transition = [[CATransition alloc]init];
transition.duration = .5;
transition.type =? @"rippleEffect";
transition.subtype = kCATransitionFromRight;
[self.navigationController.view.layer addAnimation:transition forKey:nil];
dispatch_group_leave(group);
dispatch_async(dispatch_get_main_queue(), ^{
[self.navigationController pushViewController:vic animated:NO];
});
}
@end