uinacafe,IOS中如何模拟UINavigationController的Slide动画

在IOS中,UINavigationController在添加(push)与删除(pop)View的时候,会加入Slide的动画。有时候,在需要自己进行模拟这个动画的时候,会比较麻烦。
通常,可以使用CATransition来使用Push效果,基本代码如下:
CATransition *transition = [CATransition animation];
transition.duration = 0.3f;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = kCATransitionPush;
transition.subtype = kCATransitionFromRight;
transition.fillMode = kCAFillModeBackwards;
transition.speed = 0.5f ;
transition.removedOnCompletion = YES;
[self.view.layer removeAllAnimations];
[self.view.layer addAnimation:transition forKey:nil];
上面是Push的代码,Pop的时候,只要将 subtype改为 kCATransitionFromLeft。
但是,使用CATransition的Push的时候,ios会将旧的view在推进中,进行Fade的处理。由于有这样的一个过程,会造成屏幕的闪白,而且与原来的UINavigationController的动画不一致。
要解决这个问题,只有使用暴力的方法,这个方法就是,先将原来的View进行截屏,生成一个UIImageView,再将这个UIImageView与新的View进行横向的平移动画,在动画结束之后,再将新的UIImageView进行删除。
截屏的代码:
UIGraphicsBeginImageContext(viewController.view.bounds.size);
CALayer *layer = viewController.view.layer;
[layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image ;
再使用UIView的animation来实现,
[UIViewbeginAnimations:nilcontext:nil];
[UIViewsetAnimationDuration:0.35f];
[UIViewsetAnimationDelegate:self];
[UIViewsetAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
[UIViewsetAnimationCurve:UIViewAnimationCurveEaseInOut];
imageView.origin = CGPointMake(imageView.origin.x-screenRect.size.width, imageView.origin.y) ;
maskImageView.origin = CGPointMake(maskImageView.origin.x-screenRect.size.width, maskImageView.origin.y);
[UIViewcommitAnimations];
这个方法的缺点就是太过于暴力,在截屏的时候,会造成性能问题。
Tags:  uinacafe

延伸阅读

最新评论

发表评论