添加新的UIView后如何删除以前的UIView堆栈?

时间:2019-05-26 05:43:28

标签: swift xcode uiview stack

我想在每次向右滑动时添加一个新的子视图,但是如果我不破坏以前的子视图,它将堆叠很多次。我知道如何删除它,但是我一直在努力只有在添加新的之后才删除它的逻辑。

我已经尝试过了:

    var view1: UIView?
    var view2: UIView?
    var ctrl = false

    override func viewDidLoad() {
        super.viewDidLoad()

        view1?.addSubview(CustomView(frame: self.view.bounds))
    }

    func addView(){
        if let test1 = view1, let test2 = view2 {
            if ctrl == true {
                test1.addSubview(CustomView(frame: self.view.bounds))
                test2.removeFromSuperview()
                ctrl = false
            }else{
                test2.addSubview(CustomView(frame: self.view.bounds))
                test1.removeFromSuperview()
                ctrl = true
            }   
        }
    }

    @IBAction func swipeRight(_ sender: Any) {
        print("Right")
        UIView.animate(withDuration: 1, animations: {
            self.view.layer.backgroundColor = .black
        }){(isFinished) in
            self.addView()
            //I am hoping that after this the view stack before will be removed
        }
    }

CustomView类是这样的:

var primaryColors: [UIColor] = [
    .red,
    .yellow,
    .blue]

class CustomView: UIView {

    override func draw(_ rect: CGRect) {
        super.draw(rect)
        primaryColors.shuffle()

        let leftRect = CGRect(x: 0, y: 0, width: rect.size.width/2, height: rect.size.height)
        primaryColors[0].set()
        guard let leftContext = UIGraphicsGetCurrentContext() else { return }
        leftContext.fill(leftRect)

        let rightRect = CGRect(x: rect.size.width/2, y: 0, width: rect.size.width/2, height: rect.size.height)
        primaryColors[1].set()
        guard let rightContext = UIGraphicsGetCurrentContext() else { return }
        rightContext.fill(rightRect)
    }
}

2 个答案:

答案 0 :(得分:1)

我不是那么擅长快速,但是直到我了解您的问题,这才应该解决。 而且,如果没有,我很确定有人会很快地以更好的方式回答。

每个ViewController都有一个基础view,您可以像view.method()一样访问它。

您将子视图添加到现有视图。

假设您要添加一个名为view1的新UIView。

首先将其初始化,

view1 = UIView(frame: view.bounds)

现在将其显示在屏幕上,像这样

view.addSubview(view1)

此行会将view1添加为view的子视图。

现在view1也位于view的顶部。因此,当您触摸屏幕时,它就是您正在访问的view1。 因此,下次您向右滑动时,手势识别器应该在view1上。

let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(swipeRightAction))
        swipeRight.direction = UISwipeGestureRecognizer.Direction.right

view1.addGestureRecognizer(swipeRight)

这是将向右滑动手势添加到view1的方式。

现在,当您在屏幕上向右滑动时,swipeRightAction()将被呼叫。

您可以从超级视图中删除此view1,例如

view1.removeFromSuperView()

现在view1消失了,现在您可以通过与添加view2类似的过程来添加view1

请参阅以下代码,它可能是相关的。

//The old view that has to be replaced
    var viewToBeRemoved : UIView!

    //The new view that will replace the previous one
    var viewToBeAdded   : UIView!

    override func viewDidLoad() {
        super.viewDidLoad()

        replaceWithNewSwipableSubview()


    }


    func replaceWithNewSwipableSubview()
    {
        viewToBeAdded = UIView(frame: view.bounds)

        //New view being added should have a gesture so that next time
        //it need to be replaced we can swipe on it
        let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(swipeRightAction))
        swipeRight.direction = UISwipeGestureRecognizer.Direction.right

        viewToBeAdded.addGestureRecognizer(swipeRight)

        UIView.animate(withDuration: 1, animations: {

            //This will be animated

            if self.viewToBeRemoved != nil{

                //As there is no subview the very first time this func is called

                self.viewToBeRemoved.removeFromSuperview()

            }

            //Because next time it will be the view to be removed
            self.viewToBeRemoved = self.viewToBeAdded


        }) { (isFinished) in

            //This block gets executed only after
            //the block above has completed
            self.view.addSubview(self.viewToBeAdded)


            //view is the default base view of a ViewController

        }
    }



    @objc func swipeRightAction() {

        replaceWithNewSwipableSubview()

    }

在您的代码view1?.addSubview(CustomView(frame: self.view.bounds))中,此行将视图添加到view1,并且您没有对要添加的视图的引用。您应该首先像view3 = CustomView(frame: self.view.bounds)一样存储添加的视图,以便以后可以删除view3。另外,您的view1实际上永远不会添加。如果我是对的,因为要添加view1,则需要先添加view.addSubview(view1)

尽管如果您告诉我们您实际想要实现的目标,则可能会提出更好的解决方案?

答案 1 :(得分:0)

它可能无法正常工作。在addSubview方法的if条件中,您正在使用: if let test1 = view1, let test2 = view2,它检查view1view2都不是nil。 这一定是失败的,因为您仅在view1中初始化了viewDidLoad()view1?.addSubview(CustomView(frame: self.view.bounds))。因此,在上述情况中,如果条件view2必须为零,那么它将求值为false

尝试将您的if条件放置如下: if let test1 = view1 || let test2 = view2(检查语法是否给出错误,如果条件为let则ll将给出错误)。并且让我知道它是否不起作用。尝试以下代码:

if((self.view1 != nil) || (self.view2 != nil){
    if ctrl == true {
        self.view1?.addSubview(CustomView(frame: self.view.bounds))
        self.view2?.removeFromSuperview()
        ctrl = false
    }else{
        self.view2?.addSubview(CustomView(frame: self.view.bounds))
        self.view1?.removeFromSuperview()
        ctrl = true
    }   
 }