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

Posted

技术标签:

【中文标题】UIView 幻灯片过渡,需要帮助和接近工作的代码【英文标题】:UIView slide transitions, help needed & near-working code 【发布时间】:2015-12-22 13:46:26 【问题描述】:

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

目标

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

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

    一个。专门使用左/右或上/下

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

观察到的代码响应

    [Good]以下代码在视图之间成功转换

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

核心代码

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

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

【问题讨论】:

您的问题到底是什么?您尝试过什么来实现不同的滑动方向?您的代码中只有一个“问题”——动画选项(即TransitionCurlUpTransitionFlipFromLeft)仅在使用transitionWithView:... or transitionFromView:... 方法时使用。 我从 'UIView.animateWithDuration'() 更改为 'UIView.transitionWithView()',但仍然无法修改!代码现在做了一个不同的动画,从屏幕外向右扩展新视图。另请注意,我尝试过差异动画 - 取消注释“animationType”并尝试:)! 对于这个问题,如你所问,请参阅“观察到的代码响应 -> [问题]”... :) 【参考方案1】:

解决了!谢谢扎克 B!!!!

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

(Transition Type) 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(); 

【讨论】:

您不应该将部分问题发布为答案,因为它肯定不是答案。 Losiowaty,那你有什么建议:)?通过在核心问题中发布所有“垃圾”,我认为这是丑陋和模糊的含义。在我看来,当前的发布方法更干净! :)

以上是关于UIView 幻灯片过渡,需要帮助和接近工作的代码的主要内容,如果未能解决你的问题,请参考以下文章

iPhone横向UIView幻灯片过渡

UIView 切换过渡?

Vue.js 过渡和过渡组动画不起作用

使用 CSS 过渡的滑动 + 淡入淡出效果

带有视图的 UIView 过渡和设置框架不再适用于 iOS 8

UIView 和 ScrollView 之间的 UIView 过渡翻转