IOS常用的转场方式:
1:UINavigationController的pushViewController打开,popViewController关闭;
2:UIViewController的presentViewController打开,dismissViewController关闭;
当然你也可以直接使用rootViewController赋值的方式…(本文暂不介绍此方法)
1:首先看一下pushViewController
//定义一个ViewController
let testViewController = self.storyboard?.instantiateViewControllerWithIdentifier("TestViewController")
//通过push打开vc
@IBAction func onPushTappend() {
navigationController?.pushViewController(testViewController,animated: true)
}
//通过push方式打开的vc,只能通过pop关闭
navigationController?.popViewControllerAnimated(true)
2:使用presentViewController方式打开
@IBAction func onPresentTappend() {
self.presentViewController(testViewController,animated: true,completion: nil)
}
//必须使用对应的方法,才能关闭
self.dismissViewControllerAnimated(true,completion: nil)
以上2中方式,打开的ViewController都是使用系统默认的动画,接下来定义动画
3:Push方式的自定义动画
push方法是UINavigationController提供的,所以…
//通过设置UINavigationController的delegate代理,返回一个动画对象.
navigationController?.delegate = self
实现代理:
//MARK: 实现push/pop的动画
extension ViewController: UINavigationControllerDelegate {
func navigationController(navigationController: UINavigationController,animationControllerForOperation operation: UINavigationControllerOperation,fromViewController fromVC: UIViewController,toViewController toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
if operation == .Push {
return CustomPushAnimation() //push时,使用此动画
}
if operation == .Pop {
return CustomPopAnimation() //pop时,使用此动画
}
return nil
}
}
实现动画:
//MARK: Push 动画
class CustomPushAnimation: NSObject,UIViewControllerAnimatedTransitioning {
func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
print("\(#function)")
transitionContext.containerView()?.backgroundColor = UIColor.whiteColor() // 修改过渡时的背景颜色
let toViewController = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey)!
let fromViewController = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey)!
transitionContext.containerView()?.insertSubview(toViewController.view,aboveSubview: fromViewController.view)
toViewController.view.transform = CGAffineTransformMakeTranslation(width,height)
//通过以下方式,可以组合2个动画效果
// toViewController.view.transform = CGAffineTransformConcat(CGAffineTransformMakeTranslation(-100,-100),CGAffineTransformMakeScale(2,2))
UIView.animateWithDuration(self.transitionDuration(transitionContext),animations: {
toViewController.view.transform = CGAffineTransformIdentity
fromViewController.view.transform = CGAffineTransformMakeTranslation(-width,-height)
}) { (completion) in
fromViewController.view.transform = CGAffineTransformIdentity
transitionContext.completeTransition(!transitionContext.transitionWasCancelled())
}
}
func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
return 0.3
}
}
//MARK: Pop动画
class CustomPopAnimation: NSObject,UIViewControllerAnimatedTransitioning {
func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
transitionContext.containerView()?.backgroundColor = UIColor.whiteColor() // 修改过渡时的背景颜色
let toViewController = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey)!
let fromViewController = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey)!
transitionContext.containerView()?.insertSubview(toViewController.view,belowSubview: fromViewController.view)
toViewController.view.transform = CGAffineTransformMakeTranslation(-width,-height)
UIView.animateWithDuration(self.transitionDuration(transitionContext),animations: {
toViewController.view.transform = CGAffineTransformIdentity
fromViewController.view.transform = CGAffineTransformMakeTranslation(width,height)
}) { (completion) in
fromViewController.view.transform = CGAffineTransformIdentity
transitionContext.completeTransition(!transitionContext.transitionWasCancelled())
}
}
func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
return 0.3
}
}
4:Present方式的自定义动画
这种方式只需要实现ViewController的transitioningDelegate代理即可
//设置目标VC的代理
testViewController.transitioningDelegate = self
通过代理,返回动画对象(和Push的动画,是一样的):
//MARK: 实现Present动画
extension ViewController: UIViewControllerTransitioningDelegate {
func animationControllerForPresentedController(presented: UIViewController,presentingController presenting: UIViewController,sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return CustomPresentAnimation() //打开时的动画
}
func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return CustomDismissAnimation() //关闭时的动画
}
}
动画对象,和直接使用上面的:效果是一样的:你也可以使用一下代码,作为动画使用的参考:
//MARK: Present动画
class CustomPresentAnimation: NSObject,belowSubview: fromViewController.view)
// 1:
toViewController.view.transform = CGAffineTransformMakeTranslation(0,height)
UIView.animateWithDuration(self.transitionDuration(transitionContext),animations: {
toViewController.view.transform = CGAffineTransformIdentity
fromViewController.view.transform = CGAffineTransformMakeTranslation(0,-height)
}) { (completion) in
fromViewController.view.transform = CGAffineTransformIdentity
transitionContext.completeTransition(completion)
}
// 2:
// toViewController.view.frame.offsetInPlace(dx: 0,dy: height)
// UIView.animateWithDuration(transitionDuration(transitionContext),animations: {
// toViewController.view.frame.offsetInPlace(dx: 0,dy: -height)
// fromViewController.view.frame.offsetInPlace(dx: 0,dy: -height)
// }) { completion in
// fromViewController.view.frame.offsetInPlace(dx: 0,dy: height)
// transitionContext.completeTransition(completion)
// }
// 3:
// toViewController.view.center.y = 3 * height / 2
// UIView.animateWithDuration(transitionDuration(transitionContext),animations: {
// toViewController.view.center.y = height / 2
// fromViewController.view.center.y = -height / 2
// }) { completion in
// transitionContext.completeTransition(completion)
// }
}
func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
return 0.3
}
}
//MARK: Dismiss动画
class CustomDismissAnimation: NSObject,height)
}) { (completion) in
fromViewController.view.transform = CGAffineTransformIdentity
transitionContext.completeTransition(completion)
}
// 2:
// toViewController.view.frame.offsetInPlace(dx: 0,dy: -height)
// UIView.animateWithDuration(transitionDuration(transitionContext),dy: height)
// fromViewController.view.frame.offsetInPlace(dx: 0,dy: height)
// }) { completion in
// fromViewController.view.frame.offsetInPlace(dx: 0,dy: -height)
// transitionContext.completeTransition(completion)
// }
// 3:
// toViewController.view.center.y = -height / 2
// UIView.animateWithDuration(transitionDuration(transitionContext),animations: {
// toViewController.view.center.y = height / 2
// fromViewController.view.center.y = 3 * height / 2
// }) { completion in
// transitionContext.completeTransition(completion)
// }
}
func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
return 0.3
}
}
源码: https://github.com/angcyo/ViewControllerDemo
参考: http://www.xiaoyaoli.com/?p=1271
至此: 文章就结束了,如有疑问: QQ群 Android:274306954 Swift:399799363 欢迎您的加入.