(抄)页面转场动画

2016-06-22  本文已影响78人  MSG猿

http://www.devqinwei.com/2015/10/27/页面转场动画/

1.模糊放大fade away 效果

代码:

+(void)changeRootViewControllerWithStyleBlur:(UIViewController *)viewController inMainWindow:(UIWindow *)mainWindow{

if (!mainWindow.rootViewController) {

mainWindow.rootViewController = viewController;

return;

}

UIView *snapShot = [mainWindow snapshotViewAfterScreenUpdates:YES];

[viewController.view addSubview:snapShot];

mainWindow.rootViewController = viewController;

[UIView animateWithDuration:0.5 animations:^{

snapShot.layer.opacity = 0;

snapShot.layer.transform = CATransform3DMakeScale(1.5, 1.5, 1.5);

} completion:^(BOOL finished) {

[snapShot removeFromSuperview];

}];

}

2. 水平翻转效果

代码:

+ (void)changeRootViewControllerWithStyleFlip:(UIViewController*)viewController  inMainWindow:(UIWindow *)mainWindow{

if (!mainWindow.rootViewController) {

mainWindow.rootViewController = viewController;

return;

}

[UIView transitionWithView:mainWindow

duration:0.5f

options:UIViewAnimationOptionTransitionFlipFromLeft

animations:^{ mainWindow.rootViewController = viewController; }

completion:nil];

}

3. 仿twitter logo放大退隐动画

效果:

注:这个是写在navigationController上的,如果window的rootViewController是tabbarController,则需要做调整。(参考Kitten Yang的《A guide to iOS animation》)

代码:

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

// 设置navigationController 的 mask动画

self.navigationController.view.layer.mask = [CALayer layer];

self.navigationController.view.layer.mask.contents = (id)[UIImage imageNamed:@"star"].CGImage;

self.navigationController.view.layer.mask.position = self.view.center;

self.navigationController.view.layer.mask.bounds = CGRectMake(0.0f, 0.0f, 300.0f, 300.0f);

CAKeyframeAnimation *keyFrameAnimation = [CAKeyframeAnimation animationWithKeyPath:@"bounds"];

keyFrameAnimation.delegate = self;

keyFrameAnimation.duration = 1.0f;

keyFrameAnimation.beginTime = CACurrentMediaTime() + 1.0f;

NSValue *initialBounds = [NSValue valueWithCGRect: self.navigationController.view.layer.mask.bounds];

NSValue *secondBounds = [NSValue valueWithCGRect:CGRectMake(0.0f, 0.0f, self.navigationController.view.layer.mask.bounds.size.width - 40.0f , self.navigationController.view.layer.mask.bounds.size.height - 40.0f)];

NSValue *finalBounds = [NSValue valueWithCGRect:CGRectMake(0.0f, 0.0f, 2000.0f, 2000.0f)];

keyFrameAnimation.values = @[initialBounds,secondBounds,finalBounds];

keyFrameAnimation.keyTimes = @[@0, @0.5, @1];

keyFrameAnimation.timingFunctions = @[[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut],[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut] ];

keyFrameAnimation.removedOnCompletion = NO;

keyFrameAnimation.fillMode = kCAFillModeForwards;

[self.navigationController.view.layer.mask addAnimation:keyFrameAnimation forKey:@"maskAnimation"];

// 设置一个在navigationController的layer和mask之间的白色隔层(注意,view如果添加subView,那么这个subView是在mask与view之间)

__block UIView *whiteView = [[UIView alloc] initWithFrame:self.navigationController.view.frame];

whiteView.backgroundColor = [UIColor whiteColor];

[self.navigationController.view addSubview:whiteView];

[self.navigationController.view bringSubviewToFront:whiteView];

// 设置navigationController的动画

[UIView animateWithDuration:0.25 delay:1.3 options:0 animations:^{

self.navigationController.view.transform = CGAffineTransformMakeScale(1.05, 1.05);

whiteView.alpha = 0.8f;

} completion:^(BOOL finished) {

[UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{

self.navigationController.view.transform = CGAffineTransformIdentity;

whiteView.alpha = 0.0f;

} completion:nil];

}];

}

上一篇下一篇

猜你喜欢

热点阅读