Android仿appstore转场动画,AppStore首页today动画转场(Swift)

本文详细介绍了如何实现自定义导航栏转场动画,包括手势识别、动画时长控制以及核心动画的执行过程。通过代码示例展示了如何在UIScrollView上设置手势识别,并在转场过程中对视图进行截屏、截边角、添加过渡效果等操作,最终实现平滑的页面切换动画。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

func navigationController(_ navigationController: UINavigationController, animationControllerFor operation: UINavigationController.Operation, from fromVC: UIViewController, to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning?{

return self

}

//MARK: - -手势只有在UIScrollView上识别

funcgestureRecognizer(_gestureRecognizer:UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer:UIGestureRecognizer) ->Bool{

if let onView  = otherGestureRecognizer.view, onView.isKind(of:UIScrollView.self){

return true

}

return false

}

//MARK: - -动画时长

functransitionDuration(using transitionContext:UIViewControllerContextTransitioning?) ->TimeInterval{

return1.0

}

//MARK: - -核心动画

funcanimateTransition(using transitionContext:UIViewControllerContextTransitioning) {

// 从这里

letfromVC = transitionContext.viewController(forKey: .from)as!ZHViewController

//到这里

lettoVC = transitionContext.viewController(forKey: .to)as!HomePageVC

//从这个vc

letfromView =headImageView

//临时容器 这里有个重要的概念containerView,要做转场动画的视图就必须要加入containerView上才能进行,可以理解containerView管理着所有做转场动画的视图

letcontainerView = transitionContext.containerView

var  cell :HomePageCell?

ifletindex =selectIndexPath{

cell = toVC.myTableView.cellForRow(at: index)as?HomePageCell

}

lettoView = cell?.header

//截屏

letsnapShotView = fromView?.snapshotView(afterScreenUpdates:false)

snapShotView?.layer.masksToBounds=true

snapShotView?.layer.cornerRadius=15

snapShotView?.frame  = containerView.convert(fromView!.frame, from: fromView?.superview)

// 加载到临时容器 让跳转动画有痕迹

containerView.insertSubview(toVC.view, belowSubview: fromVC.view)

containerView.addSubview(snapShotView!)

//title

snapShotView?.addSubview(titleLabel!)

snapShotView?.addSubview(titleTwoLabel!)

toView?.isHidden=true

fromView?.isHidden=true

UIView.animate(withDuration:self.transitionDuration(using: transitionContext), delay:0, usingSpringWithDamping:0.5, initialSpringVelocity:0.8, options: .curveEaseInOut, animations: {

containerView.layoutIfNeeded()

self.myScrollView.layoutIfNeeded()

fromVC.view.alpha=0.0

snapShotView?.layer.cornerRadius=15

self.myScrollView.frame=CGRect(x:  self.myScrollView.frame.origin.x, y:  self.myScrollView.frame.origin.y, width:  self.myScrollView.frame.size.width, height:SCREEN_WIDTH*1.5*0.8)

self.myScrollView.layer.cornerRadius=15

snapShotView?.frame= containerView.convert(toView!.frame, from: toView?.superview)//临时的

}) { (finish)in

fromView?.isHidden=true

snapShotView?.removeFromSuperview()

toView?.isHidden=false

//动画结束告诉上下文

transitionContext.completeTransition(!transitionContext.transitionWasCancelled)

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值