RootView 컨트롤러 스위치 전환 애니메이션
기존 뷰 컨트롤러를 루트뷰 컨트롤러로 교체하면서 전환/애니메이션 효과를 발휘할 수 있는 방법이 appDelegate에서 새 뷰 컨트롤러로 교체할 수 있습니까?
의 전환을 마무리할 수 있습니다.rootViewController전환 애니메이션 블록에서:
[UIView transitionWithView:self.window
                  duration:0.5
                   options:UIViewAnimationOptionTransitionFlipFromLeft
                animations:^{ self.window.rootViewController = newViewController; }
                completion:nil];
저는 이것을 발견했고 완벽하게 작동합니다.
앱에서 딜러:
- (void)changeRootViewController:(UIViewController*)viewController {
    if (!self.window.rootViewController) {
        self.window.rootViewController = viewController;
        return;
    }
    UIView *snapShot = [self.window snapshotViewAfterScreenUpdates:YES];
    [viewController.view addSubview:snapShot];
    self.window.rootViewController = viewController;
    [UIView animateWithDuration:0.5 animations:^{
        snapShot.layer.opacity = 0;
        snapShot.layer.transform = CATransform3DMakeScale(1.5, 1.5, 1.5);
    } completion:^(BOOL finished) {
        [snapShot removeFromSuperview];
    }];
}
 
앱에서
 if (!app) { app = (AppDelegate *)[[UIApplication sharedApplication] delegate]; }
        [app changeRootViewController:newViewController];
 
학점:
신속하게 구현된 예수님의 답변을 게시합니다.뷰 컨트롤러의 식별자를 인수로 사용하고 스토리보드에서 원하는 뷰 컨트롤러를 로드하며 애니메이션으로 루트뷰 컨트롤러를 변경합니다.
Swift 3.0 업데이트:
  func changeRootViewController(with identifier:String!) {
    let storyboard = self.window?.rootViewController?.storyboard
    let desiredViewController = storyboard?.instantiateViewController(withIdentifier: identifier);
    let snapshot:UIView = (self.window?.snapshotView(afterScreenUpdates: true))!
    desiredViewController?.view.addSubview(snapshot);
    self.window?.rootViewController = desiredViewController;
    UIView.animate(withDuration: 0.3, animations: {() in
      snapshot.layer.opacity = 0;
      snapshot.layer.transform = CATransform3DMakeScale(1.5, 1.5, 1.5);
      }, completion: {
        (value: Bool) in
        snapshot.removeFromSuperview();
    });
  }
 
Swift 2.2 업데이트:
  func changeRootViewControllerWithIdentifier(identifier:String!) {
    let storyboard = self.window?.rootViewController?.storyboard
    let desiredViewController = storyboard?.instantiateViewControllerWithIdentifier(identifier);
    let snapshot:UIView = (self.window?.snapshotViewAfterScreenUpdates(true))!
    desiredViewController?.view.addSubview(snapshot);
    self.window?.rootViewController = desiredViewController;
    UIView.animateWithDuration(0.3, animations: {() in
      snapshot.layer.opacity = 0;
      snapshot.layer.transform = CATransform3DMakeScale(1.5, 1.5, 1.5);
      }, completion: {
        (value: Bool) in
        snapshot.removeFromSuperview();
    });
  }
  class func sharedAppDelegate() -> AppDelegate? {
    return UIApplication.sharedApplication().delegate as? AppDelegate;
  }
 
이후에는 어디서나 매우 간편하게 사용할 수 있습니다.
let appDelegate = AppDelegate.sharedAppDelegate()
appDelegate?.changeRootViewControllerWithIdentifier("YourViewControllerID")
 
신속한 3.0 업데이트
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.changeRootViewController(with: "listenViewController")
스위프트 2
UIView.transitionWithView(self.window!, duration: 0.5, options: UIViewAnimationOptions.TransitionFlipFromLeft, animations: {
  self.window?.rootViewController = anyViewController
}, completion: nil)
 
스위프트 3, 4, 5
UIView.transition(with: self.window!, duration: 0.5, options: UIView.AnimationOptions.transitionFlipFromLeft, animations: {
  self.window?.rootViewController = anyViewController
}, completion: nil)
이것만 드셔보세요.저한테는 잘 맞습니다.
BOOL oldState = [UIView areAnimationsEnabled];
[UIView setAnimationsEnabled:NO];
self.window.rootViewController = viewController;
[UIView transitionWithView:self.window duration:0.5 options:transition animations:^{
    //
} completion:^(BOOL finished) {
    [UIView setAnimationsEnabled:oldState];
}];
 
편집:
이것이 더 좋습니다.
- (void)setRootViewController:(UIViewController *)viewController
               withTransition:(UIViewAnimationOptions)transition
                   completion:(void (^)(BOOL finished))completion {
    UIViewController *oldViewController = self.window.rootViewController;
    [UIView transitionFromView:oldViewController.view 
                        toView:viewController.view
                      duration:0.5f
                       options:(UIViewAnimationOptions)(transition|UIViewAnimationOptionAllowAnimatedContent|UIViewAnimationOptionLayoutSubviews)
                    completion:^(BOOL finished) {
        self.window.rootViewController = viewController;
        if (completion) {
            completion(finished);
        }
    }];
}
앱에서 나중에 전환 플립에 문제가 발생하지 않도록 하려면 스택에서 이전 보기를 지우는 것도 좋습니다.
UIViewController *oldController=self.window.rootViewController;
[UIView transitionWithView:self.window
                  duration:0.5
                   options:UIViewAnimationOptionTransitionCrossDissolve
                animations:^{ self.window.rootViewController = nav; }
                completion:^(BOOL finished) {
                    if(oldController!=nil)
                        [oldController.view removeFromSuperview];
                }];
정답은 교체할 필요가 없다는 것입니다.rootViewController당신의 창문에.대신 사용자 지정 만들기UIViewController한 번 할당하고 한 번에 하나의 하위 컨트롤러를 표시하고 필요한 경우 애니메이션으로 교체합니다.다음 코드를 시작점으로 사용할 수 있습니다.
스위프트 3.0
import Foundation
import UIKit
/// Displays a single child controller at a time.
/// Replaces the current child controller optionally with animation.
class FrameViewController: UIViewController {
    private(set) var displayedViewController: UIViewController?
    func display(_ viewController: UIViewController, animated: Bool = false) {
        addChildViewController(viewController)
        let oldViewController = displayedViewController
        view.addSubview(viewController.view)
        viewController.view.layoutIfNeeded()
        let finishDisplay: (Bool) -> Void = {
            [weak self] finished in
            if !finished { return }
            oldViewController?.view.removeFromSuperview()
            oldViewController?.removeFromParentViewController()
            viewController.didMove(toParentViewController: self)
        }
        if (animated) {
            viewController.view.alpha = 0
            UIView.animate(
                withDuration: 0.5,
                animations: { viewController.view.alpha = 1; oldViewController?.view.alpha = 0 },
                completion: finishDisplay
            )
        }
        else {
            finishDisplay(true)
        }
        displayedViewController = viewController
    }
    override var preferredStatusBarStyle: UIStatusBarStyle {
        return displayedViewController?.preferredStatusBarStyle ?? .default
    }
}
 
사용 방법은 다음과 같습니다.
...
let rootController = FrameViewController()
rootController.display(UINavigationController(rootViewController: MyController()))
window.rootViewController = rootController
window.makeKeyAndVisible()
...
 
위의 예는 내포할 수 있음을 보여줍니다.UINavigationController안에서.FrameViewController그리고 그것은 잘 작동합니다.이 접근 방식은 높은 수준의 사용자 지정 및 제어를 제공합니다.그냥 전화하세요.FrameViewController.display(_)언제든지 창에서 루트 컨트롤러를 교체할 수 있습니다. 그러면 이 작업을 수행할 수 있습니다.
이것은 swift 3에 대한 업데이트입니다. 이 메서드는 앱 대리인에게 있어야 하며 앱 대리인의 공유 인스턴스를 통해 보기 컨트롤러에서 호출해야 합니다.
func logOutAnimation() {
    let storyBoard = UIStoryboard.init(name: "SignIn", bundle: nil)
    let viewController = storyBoard.instantiateViewController(withIdentifier: "signInVC")
    UIView.transition(with: self.window!, duration: 0.5, options: UIViewAnimationOptions.transitionFlipFromLeft, animations: {
        self.window?.rootViewController = viewController
        self.window?.makeKeyAndVisible()
    }, completion: nil)
}
 
위의 다양한 질문에서 빠진 부분은,
    self.window?.makeKeyAndVisible()
 
이것이 누군가에게 도움이 되기를 바랍니다.
앱 대표자에서.h:
#define ApplicationDelegate ((AppDelegate *)[UIApplication sharedApplication].delegate)]
 
컨트롤러에서 다음을(를) 참조하십시오.
[UIView transitionWithView:self.window
                  duration:0.5
                   options:UIViewAnimationOptionTransitionFlipFromLeft
                animations:^{
    ApplicationDelegate.window.rootViewController = newViewController;
    }
                completion:nil];
저는 제 프로젝트에서 잘 작동하는 저의 방식을 제안하고, 그것은 저에게 좋은 애니메이션을 제공합니다.저는 이 게시물에서 발견된 다른 제안들을 테스트해 보았지만, 그 중 일부는 예상대로 작동하지 않습니다.
- (void)transitionToViewController:(UIViewController *)viewController withTransition:(UIViewAnimationOptions)transition completion:(void (^)(BOOL finished))completion {
// Reset new RootViewController to be sure that it have not presented any controllers
[viewController dismissViewControllerAnimated:NO completion:nil];
[UIView transitionWithView:self.window
                  duration:0.5f
                   options:transition
                animations:^{
                    for (UIView *view in self.window.subviews) {
                        [view removeFromSuperview];
                    }
                    [self.window addSubview:viewController.view];
                    self.window.rootViewController = viewController;
                } completion:completion];
}
멋진 애니메이션(Swift 4.x로 테스트됨):
extension AppDelegate {
   public func present(viewController: UIViewController) {
        guard let window = window else { return }
        UIView.transition(with: window, duration: 0.5, options: .transitionFlipFromLeft, animations: {
            window.rootViewController = viewController
        }, completion: nil)
    }
}
 
통화 내용
guard let delegate = UIApplication.shared.delegate as? AppDelegate else { return }
delegate.present(viewController: UIViewController())
언급URL : https://stackoverflow.com/questions/7703806/rootviewcontroller-switch-transition-animation
'source' 카테고리의 다른 글
| .aspx vs.ashx 주 차이점 (0) | 2023.05.16 | 
|---|---|
| css 또는 jquery를 사용하여 포커스 시 자리 표시자 텍스트를 자동으로 숨기려면 어떻게 해야 합니까? (0) | 2023.05.16 | 
| Maven 3.3.1 ECLISP: -Dmaven.multiModuleProjectDirectory 시스템 속성이 설정되지 않았습니다. (0) | 2023.05.11 | 
| M2E 및 생성된 소스 폴더를 Eclipse 소스 폴더로 이동 (0) | 2023.05.11 | 
| 한 변수를 일정하게 유지하고 다른 변수는 excel의 행과 함께 변경하는 방법 (0) | 2023.05.11 |