Ios UIView幻灯片过渡,需要帮助&;近工作代码

Ios UIView幻灯片过渡,需要帮助&;近工作代码,ios,swift,uiview,subview,animatewithduration,Ios,Swift,Uiview,Subview,Animatewithduration,我希望以完全编程的方式完成以下工作,并且有一些代码几乎可以做到这一点 目标 两个主视图之间的转换,viewOne和viewTwoviewOne'是初始全屏视图 具有不同方向和类型的滑块的过渡 a。专门使用左/右或上/下 b。能够选择方向(例如“向上”或“向下”) 观察到的代码响应 [Good]下面的代码成功地在视图之间转换 [问题]下面的代码只使用相同的转换,每次左->右 核心代码 (其余代码段在下面第二个区块中发布) 剩余代码段 回想一下,这是100%编程的。因此,我觉得这里的代码非常有价值,

我希望以完全编程的方式完成以下工作,并且有一些代码几乎可以做到这一点

目标

  • 两个主视图之间的转换,viewOneviewTwoviewOne'是初始全屏视图

  • 具有不同方向和类型的滑块的过渡

    a。专门使用左/右或上/下

    b。能够选择方向(例如“向上”或“向下”)

  • 观察到的代码响应

  • [Good]下面的代码成功地在视图之间转换

  • [问题]下面的代码只使用相同的转换,每次左->右

  • 核心代码

    (其余代码段在下面第二个区块中发布)

    剩余代码段

    回想一下,这是100%编程的。因此,我觉得这里的代码非常有价值,能够响应:)


    解决了!谢谢你,扎克B

    以下是对解决方案的原始代码所做的关键更改

    • (转换类型)UIViewAnimationOptions.TransitionCrossBluse用于“选项:”
    • (过渡方向)由视图的初始屏幕外位置设置!有关清晰的示例,请参见拾取视图()中的X删除
    就这样

    为确保未来检查的清晰度和清洁度,这里提供了最终工作解决方案:

    class ViewController: UIViewController {
    
        var viewOne : UIView!;
        var viewTwo : UIView!;
    
    override func viewDidLoad() {
        super.viewDidLoad();
    
        gen_views();
        pick_view(true);
    
        return;
    }
    
    func gen_views() {
    
        viewOne = UIView();
        viewOne.backgroundColor = UIColor.redColor();
        viewOne.frame = self.view.frame;
    
        addSecondUIView(viewOne, dispStr: "4:30 PM");
        addSubviewButton(viewOne, return_msg: "Launch View #1", action_fcn:  "press_launch:");
    
        viewTwo = UIView();
        viewTwo.backgroundColor = UIColor.blueColor();
        viewTwo.frame = self.view.frame;
    
        addSecondUIView(viewTwo, dispStr: "8:00 PM");
        addSubviewButton(viewTwo, return_msg: "Return from View #2", action_fcn:  "press_return:");
    
        viewOne.frame = self.view.frame;
        viewTwo.frame = CGRectMake(self.view.frame.width, 0, self.view.frame.width, self.view.frame.height);       //init off-screen
    
        self.view.addSubview(viewOne);                                    //add em both!
            self.view.addSubview(viewTwo);
    
            return;
    }
    
        /**********************************************************************************************************************/
        /* @url    http://www.raywenderlich.com/86521/how-to-make-a-view-controller-transition-animation-like-in-the-ping-app */
        /**********************************************************************************************************************/
        func pick_view(showFirst: Bool) {
    
            let addedView   = (showFirst) ? viewOne : viewTwo;
            let removedView = (showFirst) ? viewTwo : viewOne;
    
            let xRemoved    = (showFirst) ? self.view.frame.width : -self.view.frame.width; // sets which dir it slides into view from
    
            addedView.alpha = 0;
    
            self.view.addSubview(addedView);
    
            print("old view off!");
    
            UIView.animateWithDuration(0.5, delay: 0.5, options: UIViewAnimationOptions.TransitionCrossDissolve, animations: {
    
                print("new view added");
    
                addedView.alpha = 1.0;
    
                addedView.frame = self.view.frame;
    
                }, completion: { (finished: Bool) -> Void in
    
                    print("old view removed");
    
                    removedView.frame = CGRectMake(xRemoved, 0, self.view.frame.width, self.view.frame.height);
    
                    removedView.removeFromSuperview();
            })
    
            return;
        }
    
    
    func addSecondUIView(aView: UIView, dispStr : String) {        
        //see above post!
    }
    
    
    func addSubviewButton(aView: UIView, return_msg: String, action_fcn : Selector) {
        //see above post!
    }
    
    
    func press_launch(sender: UIButton!) {
        //see above post!
    }
    
    
    func press_return(sender: UIButton!) {
        //see above post!
    }
    
    override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning(); }
    

    }

    你不应该把问题的某些部分作为答案发布,因为它肯定不是答案。Losiowaty,那么你的建议是什么:)?我认为在核心问题中贴出所有这些“垃圾”是丑陋和卑鄙的意思。在我看来,当前的post方法更干净!:)你的问题到底是什么?您尝试了哪些不同的滑动方向?代码中只有一个“问题”-动画选项(即
    TransitionCurlUp
    TransitionFlipFromLeft
    )仅在使用方法时使用。我将“UIView.animateWithDuration”(UIView.animateWithDuration)更改为“UIView.transitionWithView()”,但它仍然不可修改!代码现在做了一个不同的动画,从屏幕外扩展到新视图。还要注意的是,我已经尝试过diff动画-取消注释'animationType'并尝试:)!对于这个问题,如您所问,请参阅“观察到的代码响应->[问题]”…:)
    import UIKit
    
    class ViewController: UIViewController {
    
        var viewOne : UIView!;
        var viewTwo : UIView!;
    
        override func viewDidLoad() {
            super.viewDidLoad();
    
            gen_views();
            pick_view(true);
    
            return;
        }
    
        func gen_views() {
    
            viewOne = UIView();
            viewOne.backgroundColor = UIColor.redColor();
            viewOne.frame = self.view.frame;
    
            addSecondUIView(viewOne, dispStr: "4:30 PM");
            addSubviewButton(viewOne, return_msg: "Launch View #2", action_fcn:  "press_launch:");
    
            viewTwo = UIView();
            viewTwo.backgroundColor = UIColor.blueColor();
            viewTwo.frame = self.view.frame;
    
            addSecondUIView(viewTwo, dispStr: "8:00 PM");
            addSubviewButton(viewTwo, return_msg: "Return from View #2", action_fcn:  "press_return:");
    
            viewTwo.frame = CGRectMake(self.view.frame.width, 0, 0, 0);       //init off-screen
    
            self.view.addSubview(viewOne);                                    //add em both!
            self.view.addSubview(viewTwo);
    
            return;
        }
    
    
        func pick_view(showFirst: Bool) {        
            //see above :)
        }
    
    
        func addSecondUIView(aView: UIView, dispStr : String) {
    
            let anotherView : UIView = UIView(frame: CGRect(x: 20, y:60, width: 100, height: 50));
    
            anotherView.backgroundColor = UIColor.grayColor();
    
            anotherView.layer.cornerRadius = 15;
    
            let someLabel : UILabel = UILabel(frame: CGRect(x:5, y: 0, width: anotherView.frame.width, height:  anotherView.frame.height));
    
            someLabel.font  =   UIFont(name: "HelveticaNeue", size: 23);
            someLabel.text  =   dispStr;
            someLabel.textColor     = UIColor.whiteColor();
            someLabel.textAlignment = NSTextAlignment.Center;
    
            anotherView.addSubview(someLabel);
    
            aView.addSubview(anotherView);
    
            return;
        }
    
    
        func addSubviewButton(aView: UIView, return_msg: String, action_fcn : Selector) {
    
            let newButton : UIButton = UIButton(type: UIButtonType.RoundedRect);
    
            newButton.translatesAutoresizingMaskIntoConstraints = true;                     //must be true for center to work
    
            newButton.setTitle(return_msg,      forState: UIControlState.Normal);
            newButton.backgroundColor = UIColor.whiteColor();
    
            //newButton.frame = CGRectMake(0, 0, 144, 30);                                  // or just use sizeToFit(), it's easier!
            newButton.sizeToFit();
            newButton.center = CGPointMake((UIScreen.mainScreen().bounds.width)/2, 250);     //must call after it's sized or won't work!
    
            //actions
            newButton.addTarget(self, action: action_fcn, forControlEvents:  .TouchUpInside);
    
            //add!
            aView.addSubview(newButton);
    
            return;
        }
    
    
        func press_launch(sender: UIButton!) {
    
            print("\(sender.titleLabel!.text!) was pressed and press_launch called");
    
            pick_view(false);
    
            return;
        }
    
    
        func press_return(sender: UIButton!) {
            print("\(sender.titleLabel!.text!) was pressed and press_return called");
    
            pick_view(true);
    
            return;
        }
    
    
        override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning(); }
    }
    
    class ViewController: UIViewController {
    
        var viewOne : UIView!;
        var viewTwo : UIView!;
    
    override func viewDidLoad() {
        super.viewDidLoad();
    
        gen_views();
        pick_view(true);
    
        return;
    }
    
    func gen_views() {
    
        viewOne = UIView();
        viewOne.backgroundColor = UIColor.redColor();
        viewOne.frame = self.view.frame;
    
        addSecondUIView(viewOne, dispStr: "4:30 PM");
        addSubviewButton(viewOne, return_msg: "Launch View #1", action_fcn:  "press_launch:");
    
        viewTwo = UIView();
        viewTwo.backgroundColor = UIColor.blueColor();
        viewTwo.frame = self.view.frame;
    
        addSecondUIView(viewTwo, dispStr: "8:00 PM");
        addSubviewButton(viewTwo, return_msg: "Return from View #2", action_fcn:  "press_return:");
    
        viewOne.frame = self.view.frame;
        viewTwo.frame = CGRectMake(self.view.frame.width, 0, self.view.frame.width, self.view.frame.height);       //init off-screen
    
        self.view.addSubview(viewOne);                                    //add em both!
            self.view.addSubview(viewTwo);
    
            return;
    }
    
        /**********************************************************************************************************************/
        /* @url    http://www.raywenderlich.com/86521/how-to-make-a-view-controller-transition-animation-like-in-the-ping-app */
        /**********************************************************************************************************************/
        func pick_view(showFirst: Bool) {
    
            let addedView   = (showFirst) ? viewOne : viewTwo;
            let removedView = (showFirst) ? viewTwo : viewOne;
    
            let xRemoved    = (showFirst) ? self.view.frame.width : -self.view.frame.width; // sets which dir it slides into view from
    
            addedView.alpha = 0;
    
            self.view.addSubview(addedView);
    
            print("old view off!");
    
            UIView.animateWithDuration(0.5, delay: 0.5, options: UIViewAnimationOptions.TransitionCrossDissolve, animations: {
    
                print("new view added");
    
                addedView.alpha = 1.0;
    
                addedView.frame = self.view.frame;
    
                }, completion: { (finished: Bool) -> Void in
    
                    print("old view removed");
    
                    removedView.frame = CGRectMake(xRemoved, 0, self.view.frame.width, self.view.frame.height);
    
                    removedView.removeFromSuperview();
            })
    
            return;
        }
    
    
    func addSecondUIView(aView: UIView, dispStr : String) {        
        //see above post!
    }
    
    
    func addSubviewButton(aView: UIView, return_msg: String, action_fcn : Selector) {
        //see above post!
    }
    
    
    func press_launch(sender: UIButton!) {
        //see above post!
    }
    
    
    func press_return(sender: UIButton!) {
        //see above post!
    }
    
    override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning(); }