在平时的iOS开发中,我们进行界面跳转时一般都是采用系统默认的转场动画,而下面这篇文章主要给大家介绍了关于iOS利用swift实现转场动画的方法示例,文中通过示例代码介绍的非常详细,需要的朋友可以参考借鉴,下面来一起看看吧。
转场动画介绍
转场动画在我们日常开发中是经常遇到的,所谓转场动画,就是一个控制器的view切到另一个控制器的view上过程中过的动画效果。本例子是实现了在导航控制器的titleView边上慢慢弹出一个控制器。下面话不多说,来一起看看详细的介绍:
效果图:

专场前

专场后
示例代码
首先自定义一个animator类。在需要转场的控制器内,设置代理
//需要设置转场动画的控制器titleViewVc.transitioningDelegate = aniamator//这里的animator是animator的实例
下面是animator类中的代码
class animatorTool: NSObject {
lazy var isPresent = false
var callBack : ((isPresented:Bool)->())?//向外界传递动画是否正在显示
init(callBack : ((isPresented:Bool)->())) {
self.callBack = callBack
}//自定义构造方法,便于给闭包赋值
}
extension animatorTool:UIViewControllerTransitioningDelegate{
func presentationControllerForPresentedViewController(presented: UIViewController, presentingViewController presenting: UIViewController, sourceViewController source: UIViewController) -> UIPresentationController? {
return AWYPresentationController(presentedViewController: presented, presentingViewController: presenting)//AWYPresentationController是自定义继承自UIPresentationController的类,是为了设置modal出来的vc的view的大小
}
func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
isPresent = true
self.callBack!(isPresented: isPresent)
return self
}
func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
isPresent = false
self.callBack!(isPresented: isPresent)
return self
}
}
extension animatorTool:UIViewControllerAnimatedTransitioning{
func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
return 0.5//动画时长
}
func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
isPresent ?animatetransitionForPresented(transitionContext) : animatetransitionForDismissed(transitionContext)
}
func animatetransitionForPresented(transitonContext:UIViewControllerContextTransitioning){
let aimView = transitonContext.viewForKey(UITransitionContextToViewKey)!
transitonContext.containerView()?.addSubview(aimView)
aimView.transform = CGAffineTransformMakeScale(1.0, 0.0)
UIView.animateWithDuration(transitionDuration(transitonContext), animations: {
aimView.layer.anchorPoint = CGPointMake(0.5, 0.0)
aimView.transform = CGAffineTransformIdentity
}) { (_) in
transitonContext.completeTransition(true)
}
}
func animatetransitionForDismissed(transitonContext:UIViewControllerContextTransitioning){
let aimView = transitonContext.viewForKey(UITransitionContextFromViewKey)!
transitonContext.containerView()?.addSubview(aimView)
UIView.animateWithDuration(transitionDuration(transitonContext), animations: {
aimView.layer.anchorPoint = CGPointMake(0.5, 0.0)
aimView.transform = CGAffineTransformMakeScale(1.0, 0.001)//留一点值,这样会有动画效果
}) { (_) in
transitonContext.completeTransition(true)
}
}
}
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对编程学习网的支持。
织梦狗教程
本文标题为:iOS swift实现转场动画的方法示例
基础教程推荐
猜你喜欢
- IOS 播放系统提示音使用总结(AudioToolbox) 2023-03-01
- Flutter手势密码的实现示例(附demo) 2023-04-11
- IOS应用内跳转系统设置相关界面的方法 2022-11-20
- Android多返回栈技术 2023-04-15
- 解决Android Studio突然不显示logcat日志的问题 2023-02-04
- android studio按钮监听的5种方法实例详解 2023-01-12
- Android中的webview监听每次URL变化实例 2023-01-23
- Android开发使用RecyclerView添加点击事件实例详解 2023-06-15
- iOS开发教程之XLForm的基本使用方法 2023-05-01
- Flutter绘图组件之CustomPaint使用详解 2023-05-12
