J'utilise actuellement [self presentModalViewController :newVC animated:YES]
. Je veux présenter newViewcontroller de gauche/droite/haut/bas avec un effet Push. J'ai essayé d'utiliser CATransition mais il affiche un écran noir entre les transitions.
Lorsqu'il est présent:
CATransition *transition = [CATransition animation];
transition.duration = 0.3;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = kCATransitionPush;
transition.subtype = kCATransitionFromRight;
[self.view.window.layer addAnimation:transition forKey:nil];
[self presentModalViewController:viewCtrl animated:NO];
En cas de licenciement:
CATransition *transition = [CATransition animation];
transition.duration = 0.3;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = kCATransitionPush;
transition.subtype = kCATransitionFromLeft;
[self.view.window.layer addAnimation:transition forKey:nil];
[self dismissModalViewControllerAnimated:NO];
J'ai eu le même problème. Supposons que vous souhaitiez présenter un contrôleur de vue 2 à partir du contrôleur de vue 1. Dans le premier contrôleur de vue, utilisez
[self presentModalViewController: controller animated: NO]];
Dans le deuxième contrôleur de vue, dans viewWillAppear: méthode, ajoutez le code
CATransition *animation = [CATransition animation];
[animation setDelegate:self];
[animation setType:kCATransitionPush];
[animation setSubtype:kCATransitionFromRight];
[animation setDuration:0.40];
[animation setTimingFunction:
[CAMediaTimingFunction functionWithName:
kCAMediaTimingFunctionEaseInEaseOut]];
[self.view.layer addAnimation:animation forKey:kCATransition];
Cela fonctionnera bien. Si l'écran noir réapparaît, si vous utilisez un contrôleur de navigation, remplacez
[self.view.layer addAnimation:animation forKey:kCATransition];
avec
[self.navigationController.view.layer addAnimation:animation forKey:kCATransition];
Après X> = 4 heures de travail, cela fonctionne sans "déchirure" ou autres artefacts d'arrière-plan:
class AboutTransition: NSObject, UIViewControllerAnimatedTransitioning {
let presenting: Bool
let duration: NSTimeInterval
init(presenting: Bool, duration: NSTimeInterval = 0.25) {
self.presenting = presenting
self.duration = duration
}
@objc func transitionDuration(ctx: UIViewControllerContextTransitioning) -> NSTimeInterval {
return duration
}
@objc func animateTransition(ctx: UIViewControllerContextTransitioning) {
let duration = transitionDuration(ctx)
let containerView = ctx.containerView()
let fromViewController = ctx.viewControllerForKey(UITransitionContextFromViewControllerKey)!
let toViewController = ctx.viewControllerForKey(UITransitionContextToViewControllerKey)!
let fromView = fromViewController.view // 7.0 & 8.0 compatible vs. viewForKey:
let toView = toViewController.view // 7.0 & 8.0 compatible vs. viewForKey:
containerView.addSubview(fromView)
containerView.addSubview(toView)
let offScreenRight = CGAffineTransformMakeTranslation(containerView.frame.width, 0)
let offScreenLeft = CGAffineTransformMakeTranslation(-containerView.frame.width, 0)
fromView.transform = CGAffineTransformIdentity
toView.transform = self.presenting ? offScreenRight : offScreenLeft
UIView.animateWithDuration(duration, delay:0, options:UIViewAnimationOptions(0), animations: {
fromView.transform = self.presenting ? offScreenLeft : offScreenRight
toView.transform = CGAffineTransformIdentity
}, completion: { (finished: Bool) in
ctx.completeTransition(finished)
if finished {
fromView.removeFromSuperview()
fromView.frame = toView.frame // reset the frame for reuse, otherwise frame transforms will accumulate
}
})
}
}
Puis dans AppDelegate.Swift
:
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UINavigationControllerDelegate {
lazy var window: UIWindow? = {
return UIWindow(frame: UIScreen.mainScreen().bounds)
}()
lazy var storyboard = UIStoryboard(name: "Main", bundle: nil)
lazy var nav: UINavigationController = {
var r = self.storyboard.instantiateInitialViewController() as! UINavigationController
r.delegate = self
return r
}()
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
self.window!.rootViewController = nav
self.window!.makeKeyAndVisible()
// .. other setup
return true
}
// ...
// MARK: - UINavigationControllerDelegate
func navigationController(navigationController: UINavigationController, animationControllerForOperation operation: UINavigationControllerOperation, fromViewController fromVC: UIViewController, toViewController toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
// Example of a SettingsViewController which pushes AboutViewController
if fromVC is SettingsViewController && toVC is AboutViewController { // Push to About
return AboutTransition(presenting: true)
} else if fromVC is AboutViewController && toVC is SettingsViewController { // Pop to Settings
return AboutTransition(presenting: false)
}
return nil
}
}
Cela suppose qu'une application utilise le storyboard par défaut avec une initiale VC comme UINavigationController
Mise à jour pour Swift 3/4
class LTRTransition: NSObject, UIViewControllerAnimatedTransitioning
{
let presenting: Bool
let duration: TimeInterval
init(presenting: Bool, duration: TimeInterval = Theme.currentTheme.animationDuration) {
self.presenting = presenting
self.duration = duration
}
@objc func transitionDuration(using ctx: UIViewControllerContextTransitioning?) -> TimeInterval {
return duration
}
@objc func animateTransition(using ctx: UIViewControllerContextTransitioning) {
let duration = transitionDuration(using: ctx)
let containerView = ctx.containerView
let fromViewController = ctx.viewController(forKey: UITransitionContextViewControllerKey.from)!
let toViewController = ctx.viewController(forKey: UITransitionContextViewControllerKey.to)!
guard let fromView = fromViewController.view, // 7.0 & 8.0 compatible vs. viewForKey:
let toView = toViewController.view // 7.0 & 8.0 compatible vs. viewForKey:
else {
assertionFailure("fix this")
return
}
containerView.addSubview(fromView)
containerView.addSubview(toView)
let offScreenRight = CGAffineTransform(translationX: containerView.frame.width, y: 0)
let offScreenLeft = CGAffineTransform(translationX: -containerView.frame.width, y: 0)
fromView.transform = CGAffineTransform.identity
toView.transform = self.presenting ? offScreenRight : offScreenLeft
UIView.animate(withDuration: duration, delay:0, options:UIViewAnimationOptions(rawValue: 0), animations: {
fromView.transform = self.presenting ? offScreenLeft : offScreenRight
toView.transform = CGAffineTransform.identity
}, completion: { (finished: Bool) in
ctx.completeTransition(finished)
if finished {
fromView.removeFromSuperview()
// this screws up dismissal. at least on ios10 it does fromView.frame = toView.frame // reset the frame for reuse, otherwise frame transforms will accumulate
}
})
}
}
Et l'implémentation transitioniningDelegate pour le VC étant poussé/rejeté:
extension WhateverVCyouArePresentingFrom: UIViewControllerTransitioningDelegate
{
public func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning?
{
return LTRTransition(presenting: true)
}
public func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning?
{
return LTRTransition(presenting: false)
}
}
Cela semble difficile, mais faites-le avec seulement plusieurs lignes de code.
Tout d'abord, présentez LeftSlideViewController de manière modale. Vous devez spécifier modalPresentationStyle à .overCurrentContext.
if let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "LeftSlideViewController") as? LeftSlideViewController {
vc.modalPresentationStyle = .overCurrentContext
self.present(vc, animated: false, completion: nil)
}
Et puis, ouvrez LeftSlideViewController.
Pour se déplacer de gauche à droite (Utilisation de CATransition)
Masquer une vue dans loadView.
self.view.isHidden = true
Ajouter une animation de transition de déplacement vers la gauche à une vue dans viewDidAppear
self.view.isHidden = false
let transition = CATransition.init()
transition.duration = 0.3
transition.timingFunction = CAMediaTimingFunction.init(name: .easeInEaseOut)
transition.type = .moveIn
transition.subtype = .fromLeft
self.view.layer.add(transition, forKey: nil)
Pour déplacer vers la gauche (Utilisation de l'animation UIView)
Animez le cadre de la vue en utilisant l'animation d'UIView et fermez ViewController lorsque l'animation est terminée
UIView.animate(withDuration: 0.3, delay: 0, options: .curveEaseIn, animations: {
self.view.frame = CGRect(x: self.view.frame.width * -1, y: 0, width: self.view.frame.width, height: self.view.frame.height)
}) { (finished) in
self.dismiss(animated: false, completion: nil)
}
Il n'y a que quatre UIModalTransitionStyles:
UIModalTransitionStyleCoverVertical
UIModalTransitionStyleFlipHorizontal
UIModalTransitionStyleCrossDissolve
UIModalTransitionStylePartialCurl
Par exemple:
UIViewController *controller = [[[MyViewController alloc] init] autorelease];
UIModalTransitionStyle trans = UIModalTransitionStyleFlipHorizontal;
[UIView beginAnimations: nil context: nil];
[UIView setAnimationTransition: trans forView: [self window] cache: YES];
[navController presentModalViewController: controller animated: NO];
[UIView commitAnimations];