如何用swift实现自定义页面间的切换动画
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何用swift实现自定义页面间的切换动画相关的知识,希望对你有一定的参考价值。
参考技术A 先实现页面之间切换的Controllerimport UIKit
class CustomAnimationController: NSObject, UIViewControllerAnimatedTransitioning
func transitionDuration(transitionContext: UIViewControllerContextTransitioning) -> NSTimeInterval
return 0.5
func animateTransition(transitionContext: UIViewControllerContextTransitioning)
let toViewController = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey)
let fromViewController = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey)
let finalFrame = transitionContext.finalFrameForViewController(toViewController!)
let containerView = transitionContext.containerView()
let screenBounds = UIScreen.mainScreen().bounds
toViewController?.view.frame = CGRectOffset(finalFrame, 0, screenBounds.size.height)
let toViewView = toViewController?.view
containerView.addSubview(toViewView!)
//动画
let duration = self.transitionDuration(transitionContext)
UIView.animateWithDuration(duration, animations:
fromViewController?.view.alpha = 0.5
toViewController?.view.frame = finalFrame
, completion: (Bool finished) in
fromViewController?.view.alpha = 1.0
transitionContext.completeTransition(true)
)
在view controller的class里添加delegate
class FirstViewController: UIViewController,UIViewControllerTransitioningDelegate
添加一个属性
var customAnimationController:CustomAnimationController
初始化
required init(coder aDecoder: NSCoder)
customAnimationController = CustomAnimationController()
super.init(coder: aDecoder)
在切换页面前segue里指定delegate为self
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
//
if segue.identifier == "ShowAuthor"
let toVC = segue.destinationViewController as UIViewController
toVC.transitioningDelegate = self
实现delegate
//MARK: TransitionDelegate
func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning?
return customAnimationController
本回答被提问者和网友采纳
如何用 Navigator 2.0 替换动画页面?
【中文标题】如何用 Navigator 2.0 替换动画页面?【英文标题】:How to replace page animated with Navigator 2.0? 【发布时间】:2021-10-25 23:32:00 【问题描述】:我正在通过更改 pages
集合中的项目导航到另一个页面。
final page = ...
return Scaffold(
body: Navigator(
onPopPage: (Route<dynamic> route, dynamic result)
return route.didPop(result);
,
pages: [
MaterialPage(child: page),
],
),
...
并且在这种情况下没有过渡动画(至少在 IOS 上)。当我向集合中添加页面时 - 动画出现。但我不需要页面堆栈。如何解决?
【问题讨论】:
【参考方案1】:更改页面时更改key
参数。
例子:
final page = condition
? MaterialPage(key: ValueKey('page1'), child: page1)
: MaterialPage(key: ValueKey('page2'), child: page2);
return Scaffold(
body: Navigator(
onPopPage: (Route<dynamic> route, dynamic result)
return route.didPop(result);
,
pages: [
page,
],
),
【讨论】:
以上是关于如何用swift实现自定义页面间的切换动画的主要内容,如果未能解决你的问题,请参考以下文章