如何使用左推效果制作自定义segue?

时间:2013-10-16 07:22:08

标签: iphone ios objective-c storyboard uistoryboardsegue

我的应用程序正在显示阿拉伯文字,其中我希望从左侧推送动画,而不是默认情况下ios提供的右侧。

enter image description here

我正在使用故事板。我创建了自定义segue
我已经完成了所有选项cocoacontrols,但找不到合适的选项。

我找到了以下几个选项:

1)CATransition
2)动画
3)维护视图控制器阵列 - 这会造成很多混乱

下面的代码提供了完美的效果但下面显示的黑色补丁是不可接受的 -

enter image description here

-(void)perform {


UIViewController *sourceViewController = (UIViewController*)[self sourceViewController];
UIViewController *destinationController = (UIViewController*)[self destinationViewController];


CATransition* transition = [CATransition animation];
transition.duration = .25;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = kCATransitionPush; //kCATransitionMoveIn; //, kCATransitionPush, kCATransitionReveal, kCATransitionFade
transition.subtype = kCATransitionFromLeft; //kCATransitionFromLeft, kCATransitionFromRight, kCATransitionFromTop, kCATransitionFromBottom




[sourceViewController.navigationController.view.layer addAnimation:transition
                                                            forKey:kCATransition];

[sourceViewController.navigationController pushViewController:destinationController animated:NO];

[destinationController.view setBackgroundColor:[UIColor whiteColor]];

}

在下面的代码中实现了没有黑色补丁 - 但不是所需的动画

  -(void)perform {

 UIViewController *sourceViewController = (UIViewController*)[self sourceViewController];
 UIViewController *destinationViewController = (UIViewController*)[self destinationViewController];



[sourceViewController.view addSubview:destinationViewController.view];
[destinationViewController.view setFrame:sourceViewController.view.window.frame];
[destinationViewController.view setTransform:CGAffineTransformMakeScale(0.5,0.5)];
[destinationViewController.view setAlpha:1.0];

[UIView animateWithDuration:0.5
                      delay:0.0
                    options:UIViewAnimationCurveEaseOut
                 animations:^{
                     [destinationViewController.view setTransform:CGAffineTransformMakeScale(1.0,1.0)];
                     [destinationViewController.view setAlpha:1.0];
                 }
                 completion:^(BOOL finished){
                     [destinationViewController.view removeFromSuperview];
                     [sourceViewController.navigationController pushViewController:destinationViewController animated:NO];
                 }];

   }

通过更改上述代码 - 但我没有得到理想的结果

-(void)perform{

UIViewController *sourceViewController = (UIViewController*)[self sourceViewController];
UIViewController *destinationViewController = (UIViewController*)[self   destinationViewController];



   [sourceViewController.view addSubview:destinationViewController.view];
   [destinationViewController.view setFrame:sourceViewController.view.window.frame];
   [destinationViewController.view setAlpha:1.0];

[UIView animateWithDuration:0.5
                      delay:0.0
                    options:UIViewAnimationCurveEaseOut
                 animations:^{

                     sourceViewController.view.frame =      CGRectMake(sourceViewController.view.frame.size.width*2, 0, sourceViewController.view.frame.size.width, sourceViewController.view.frame.size.height);



                 }
                 completion:^(BOOL finished){


                     [sourceViewController.navigationController pushViewController:destinationViewController animated:NO];
                 }];

}

2 个答案:

答案 0 :(得分:1)

在appDelegate.m中使用以下代码 - applicationDidFinishLaunchingWithOptions

self.window.backgroundColor = [UIColor whitecolor];

答案 1 :(得分:0)

这是swift中的自定义segue,使用凌乱的方式,操纵视图控制器列表..无论如何,我想推送到视图控制器,但让它看起来像是一个流行音乐,我试图这样做有各种各样的动画,但最终 - 对我来说,这是获得我想要的最简单的方法,因为我不需要维护我的代码,如果iOS样式改变等等,因为它使用一般流行音乐 - 它应该保持与系统中的默认弹出相同..无论如何,代码:

class ReversePushSegue : UIStoryboardSegue {

    override func perform() {
        let sourceViewController = self.sourceViewController as UIViewController
        let destinationViewController = self.destinationViewController as UIViewController

        var vcs : NSMutableArray = NSMutableArray(array: sourceViewController.navigationController.viewControllers)
        vcs.insertObject(destinationViewController, atIndex: vcs.count - 1)
        sourceViewController.navigationController.viewControllers = vcs

        sourceViewController.navigationController.popViewControllerAnimated(true)

    }

}

它的作用 - 它将destinationViewController作为倒数第二个添加到列表中然后弹出它..