[SWIFT] 사용자 정의로 자연스럽게 전환 애니메이션
SWIFT사용자 정의로 자연스럽게 전환 애니메이션
해결법
-
1.그것을 달성하는 가장 좋은 방법은 사용자 정의 푸시을 만들고 UIViewControllerAnimatedTransitioning 프로토콜을 준수하여 애니메이션을 팝업하는 것입니다 -
그것을 달성하는 가장 좋은 방법은 사용자 정의 푸시을 만들고 UIViewControllerAnimatedTransitioning 프로토콜을 준수하여 애니메이션을 팝업하는 것입니다 -
//CustomPushAnimation class import UIKit class CustomPushAnimation: NSObject, UIViewControllerAnimatedTransitioning { func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval { return 0.2 } func animateTransition(using transitionContext: UIViewControllerContextTransitioning) { let containerVw = transitionContext.containerView let fromViewController = transitionContext.viewController(forKey: .from) let toViewController = transitionContext.viewController(forKey: .to) guard let fromVc = fromViewController, let toVc = toViewController else { return } let finalFrame = transitionContext.finalFrame(for: toVc) //Below line will start from left toVc.view.frame = finalFrame.offsetBy(dx: -finalFrame.size.width/2, dy: 0) containerVw.addSubview(toVc.view) UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: { toVc.view.frame = finalFrame fromVc.view.alpha = 0.5 }, completion: {(finished) in transitionContext.completeTransition(finished) fromVc.view.alpha = 1.0 }) } } //CustomPopAnimation class import UIKit class CustomPopAnimation: NSObject, UIViewControllerAnimatedTransitioning { func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval { return 0.2 } func animateTransition(using transitionContext: UIViewControllerContextTransitioning) { let containerView = transitionContext.containerView let fromViewController = transitionContext.viewController(forKey: .from) let toViewController = transitionContext.viewController(forKey: .to) guard let fromVc = fromViewController, let toVc = toViewController, let snapshotView = fromVc.view.snapshotView(afterScreenUpdates: false) else { return } let finalFrame = transitionContext.finalFrame(for: fromVc) toVc.view.frame = finalFrame containerView.addSubview(toVc.view) containerView.sendSubview(toBack: toVc.view) snapshotView.frame = fromVc.view.frame containerView.addSubview(snapshotView) fromVc.view.removeFromSuperview() UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: { //Below line will start from right snapshotView.frame = finalFrame.offsetBy(dx: -finalFrame.size.width, dy: 0) }, completion: {(finished) in snapshotView.removeFromSuperview() transitionContext.completeTransition(finished) }) } }
//Below code should be inside YourViewController let vc = UIStoryboard(name: "storyboardName", bundle: nil).instantiateViewController(withIdentifier: "ViewControllerIdentifier") let navigationController = BaseNavigationController(rootViewController: vc) navigationController.transitioningDelegate = self present(navigationController, animated: true, completion: nil)
extension YourViewController: UIViewControllerTransitioningDelegate { func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? { return customPushAnimation } func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? { return customPopAnimation } }
from https://stackoverflow.com/questions/45384854/custom-segue-transition-animation by cc-by-sa and MIT license
'SWIFT' 카테고리의 다른 글
[SWIFT] 경로는 MKMapView에 표시되지? (0) | 2020.11.08 |
---|---|
[SWIFT] 일정 기간 동안 수동으로 이동 노드 (0) | 2020.11.08 |
[SWIFT] 스위프트 사용하는 TDD의 성능 테스트 (0) | 2020.11.08 |
[SWIFT] 쌍 어레이의 첫 번째 요소를 위젯 (SWIFT) (0) | 2020.11.07 |
[SWIFT] 앱은 jQuery과에 표시되고 업데이트 코어 데이터 모델 후 충돌 (0) | 2020.11.07 |