UIView 幻灯片转换,需要帮助和接近工作的代码

UIView slide transitions, help needed & near-working code

我希望完全以编程方式执行以下操作,并且有一些代码几乎可以做到!

目标

  1. 两个主视图之间的转换,viewOneviewTwo。 'viewOne' 是初始全屏视图。

  2. 使用不同方向和类型的幻灯片进行过渡

    一个。具体使用 left/right 或 up/down

    b。能够select方向(例如'up'或'down')

观察到的代码响应

  1. [好]下面的代码在视图之间成功转换

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

核心代码

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

    func pick_view(showFirst: Bool) {

        let addedView   = (showFirst) ? viewOne : viewTwo;
        let removedView = (showFirst) ? viewTwo : viewOne;

        print("picking a new view");

        //let animationType = (showFirst) ? UIViewAnimationOptions.TransitionFlipFromLeft : .TransitionFlipFromRight; //flip L->R or R-> depending

        addedView.alpha = 0;

        self.view.addSubview(addedView);

        print("old view off!");


        UIView.animateWithDuration(0.5, delay: 0.5, options: .TransitionCurlUp, animations: {  //.CurveLinear is an option too

            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(self.view.frame.width, 0, 0, 0);

                removedView.removeFromSuperview();
        });

        UIView.commitAnimations();

        return;
    }

剩余代码段

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

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(); }
}

已解决!谢谢扎克 B!!!!

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

  • (转换类型) UIViewAnimationOptions.TransitionCrossDissolve 用于 'options:'
  • (Transition Direction) 由视图的初始离屏位置设置!请参阅 pick_view() 中的 xRemoved 以获得清晰示例

就这样了!!!

为了未来检查的清晰和整洁,这里是最终工作解决方案:

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(); }

}