目前我正在使用[self presentModalViewController :newVC animated:YES]
。我想通过推送效果从左/右/上/下呈现newViewcontroller。我尝试使用CATransition但它在转换之间显示黑屏。
在场时:
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];
解雇时:
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];
我有同样的问题。假设您要从视图控制器1呈现视图控制器2.在第一个视图控制器中使用
[self presentModalViewController: controller animated: NO]];
在第二个视图控制器中,在viewWillAppear:方法中添加代码
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];
它会工作正常。如果再次出现黑屏,如果您使用的是导航控制器,请更换
[self.view.layer addAnimation:animation forKey:kCATransition];
同
[self.navigationController.view.layer addAnimation:animation forKey:kCATransition];
在X> = 4小时的工作之后,这可以在没有“撕裂”或其他背景工件的情况下工作:
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
}
})
}
}
然后在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
}
}
这假定应用程序正在使用默认故事板,初始VC为UINavigationController
更新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
}
})
}
}
并且推送/解除了VC的transitioniningDelegate实现:
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)
}
}
它看起来很棘手,但只需几行代码即可完成。
首先,以模态方式呈现LeftSlideViewController。您需要为.overCurrentContext指定modalPresentationStyle。
if let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "LeftSlideViewController") as? LeftSlideViewController {
vc.modalPresentationStyle = .overCurrentContext
self.present(vc, animated: false, completion: nil)
}
然后,打开LeftSlideViewController。
从左侧进入(使用CATransition)
self.view.isHidden = true
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)
向左移出(使用UIView动画)
只有四个UIModalTransitionStyle:
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)
}
例如:
UIModalTransitionStyleCoverVertical
UIModalTransitionStyleFlipHorizontal
UIModalTransitionStyleCrossDissolve
UIModalTransitionStylePartialCurl
您可以在要显示的视图控制器上设置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];
。您必须符合transitioningDelegate
和UIViewControllerTransitioningDelegate
协议。通过实现UIViewControllerAnimatedTransitioning
,您可以为视图控制器子视图设置动画。
执行转换的功能
animateTransition(transitionContext: UIViewControllerContextTransitioning)
然后TransitionManager看起来像:
class fromViewController : UIViewController {
let transitionManager = TransitionManager()
func transitionToViewController() {
toController.transitioningDelegate = transitionManager
presentViewController(toController, animated: true, completion: nil)
}
}