34

最近我更新了我的 xcode 项目以使用 iOS 7,但我遇到了一个大问题。因为我的整个应用程序只有一个背景图片(UIImageView添加到关键窗口)并且所有视图都是透明的,所以我在推送UIViewController时遇到了问题,因为推送的视图控制器与之前的视图重叠(您可以在图片中看到它:http:/ /grab.by/qp0k)。我可以预测这是因为在 iOS 7 中推送过渡已经改变,因为现在它滑动了半个屏幕。也许有人知道如何解决这个问题?

这就是我设置关键窗口的方式

  self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
 UIImageView *background = [[UIImageView alloc]initWithFrame:[[UIScreen mainScreen] bounds]];
 background.image = [UIImage imageNamed:@"background.png"]; 
UINavigationController *navi = [[UINavigationController alloc]initWithRootViewController:self.viewController];
self.window.rootViewContro‌​ller = navi;
 [self.window makeKeyAndVisible];

之后,当用户单击“开始锻炼”按钮时,我会像往常一样推送下一个视图:

workoutView *w = [[workoutView alloc]initWithNibName:@"workoutView" bundle:nil];
        [self.navigationController pushViewController:w animated:YES];
4

9 回答 9

19

我这样做了。

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.view setAlpha:0];
}

回来时不要忘记重新设置alpha。

- (void) viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.view setAlpha:1];
}
于 2013-09-23T15:54:33.700 回答
15

UINavigationControllerDelegate我通过实施新的Method解决了这个问题animationControllerForOperation

例如:

- (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController     *)navigationController
                              animationControllerForOperation:(UINavigationControllerOperation)operation
                                           fromViewController:(UIViewController *)fromVC
                                             toViewController:(UIViewController *)toVC
{

PushTransition* transition = [PushTransition new];
[transition setNavigationControllerOperation: operation];

return transition;
}

PushTransition 是一个实现UIViewControllerAnimatedTransitioning协议的类以及该协议的两个方法 transitionDuration 和 animateTransition。此外,我添加了一个属性来传递操作(告诉我它是推送还是弹出转换)。

只需将用于移动视图的动画代码放入 animateTransition 中,如下所示:

// the containerView is the superview during the animation process.
UIView *container = transitionContext.containerView;

UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];

UIView *fromView = fromVC.view;
UIView *toView = toVC.view;
CGFloat containerWidth = container.frame.size.width;

// Set the needed frames to animate.

CGRect toInitialFrame = [container frame];
CGRect fromDestinationFrame = fromView.frame;

if ([self navigationControllerOperation] == UINavigationControllerOperationPush)
{
    toInitialFrame.origin.x = containerWidth;
    toView.frame = toInitialFrame;
    fromDestinationFrame.origin.x = -containerWidth;
}
else if ([self navigationControllerOperation] == UINavigationControllerOperationPop)
{
    toInitialFrame.origin.x = -containerWidth;
    toView.frame = toInitialFrame;
    fromDestinationFrame.origin.x = containerWidth;
}

// Create a screenshot of the toView.
UIView *move = [toView snapshotViewAfterScreenUpdates:YES];
move.frame = toView.frame;
[container addSubview:move];

[UIView animateWithDuration:TRANSITION_DURATION delay:0
     usingSpringWithDamping:1000 initialSpringVelocity:1
                    options:0 animations:^{
                        move.frame = container.frame;
                        fromView.frame = fromDestinationFrame;
                    }
                 completion:^(BOOL finished) {
                     if (![[container subviews] containsObject:toView])
                     {
                         [container addSubview:toView];
                     }

                     toView.frame = container.frame;
                     [fromView removeFromSuperview];
                     [move removeFromSuperview];
                     [transitionContext completeTransition: YES];
                 }];

描述了它,你可以完成。此外,您可以制作任何您喜欢的推送或弹出动画。

于 2013-10-22T08:41:08.397 回答
6

我在初始化视图时通过这样做来修复它:

self.view.clipsToBounds = YES;
于 2014-11-01T07:47:00.670 回答
4

您可能想研究一个新的 iOS7 功能,该功能允许您定义自己的自定义 UIViewController 转换。在文档中查找 UIViewControllerTransitioningDelegate。另外,这里有一篇关于它的文章的链接:http: //www.doubleencore.com/2013/09/ios-7-custom-transitions/

于 2013-09-24T16:51:58.193 回答
1

啊,现在我明白了这个问题。你是对的,似乎是由于之前的 UIViewController 在转换后没有被隐藏(因为新的转换效果)。

似乎没有任何 SDK 方法来控制这种行为。如果不重新设计应用程序以不要求背景是静态的,您可能必须滚动自己的导航。OSNavigationController是 UINavigationController 的完整重新实现,可能会对您有所帮助。如果他们还没有更新到 iOS 7 过渡,你可能会很高兴。如果有,您可以随时使用旧版本。

于 2013-09-19T14:31:04.920 回答
1

我有同样的问题。尝试在 init 方法中加载背景图像。对我来说,它(有时)有效:例如:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.view.backgroundColor = [UIColor whiteColor];
        [self.imageBack setImage:[UIImage imageNamed:@"mayBack.png"]];
    }
    return self;
}

但是,您可以看到一瞥.. 我发现的最佳解决方案,除了实现新的 iOS7 转换协议之外,是实现一个类别,并在需要时使用该类别。你可以在这里找到答案

于 2013-10-08T20:15:24.093 回答
1

将图像设置为背景色解决了这个问题:

self.view.backgroundColor = 
            [UIColor colorWithPatternImage:[UIImage imageNamed:@"mainback.png"]];
于 2013-11-19T23:25:32.820 回答
0

看看那篇文章中的 UINavigationController 类别(它解决了我的问题):

https://stackoverflow.com/a/18882232/2826409

于 2013-09-28T15:35:22.363 回答
0

@snoersnoer 的道具。


这是 Swift 3 中的代码。

func navigationController(_ navigationController: UINavigationController, animationControllerFor operation: UINavigationControllerOperation, from fromVC: UIViewController, to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {

    let pushTransition = SUPushTransition()
    pushTransition.navigationControllerOperation = operation
    return pushTransition
}

func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {

    // the containerView is the superview during the animation process.
    let container = transitionContext.containerView

    let fromVC = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.from)
    let toVC = transitionContext.viewController(forKey:UITransitionContextViewControllerKey.to);

    if let from = fromVC,
        let fromView = from.view,
        let to = toVC,
        let toView = to.view {

        let containerWidth = container.frame.size.width

        // Set the needed frames to animate.

        var toInitialFrame = container.frame
        var fromDestinationFrame = fromView.frame

        if self.navigationControllerOperation == .push {
            toInitialFrame.origin.x = containerWidth;
            toView.frame = toInitialFrame;
            fromDestinationFrame.origin.x = -containerWidth;
        }
        else if self.navigationControllerOperation == .pop {
            toInitialFrame.origin.x = -containerWidth;
            toView.frame = toInitialFrame;
            fromDestinationFrame.origin.x = containerWidth;
        }

        // Create a screenshot of the toView.
        if let move = toView.snapshotView(afterScreenUpdates: true) {

            move.frame = toView.frame
            container.addSubview(move)

            UIView.animate(withDuration: Constants.MainPage.navControllerDuration, delay: 0.0, usingSpringWithDamping: 1000, initialSpringVelocity: 1, options: .curveEaseInOut, animations: {
                move.frame = container.frame;
                fromView.frame = fromDestinationFrame;
            }, completion: { (finished) in

                if finished {

                    if !container.subviews.contains(toView) {
                        container.addSubview(toView)
                    }

                    toView.frame = container.frame

                    fromView.removeFromSuperview()
                    move.removeFromSuperview()

                    transitionContext.completeTransition(true)
                }

            })

        }

    }

}

干杯。

于 2016-12-24T19:44:02.017 回答