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

标签 swift xcode uiview stack

每次向右滑动我都想添加一个新的 subview ,但是如果我不破坏之前的 subview ,它会堆叠很多次。我知道如何删除它,但我正在为如何在添加新的之后才删除它的逻辑而苦苦挣扎。

我试过这个:

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

最佳答案

嗯,我不太擅长 swift,但在我理解你的问题之前,这应该可以解决它。 如果没有,我敢肯定会有更好的 swift 人以更好的方式回答它。

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

您将 subview 添加到现有 View 。

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

首先你初始化它,

view1 = UIView(frame: view.bounds)

现在让它出现在屏幕上,点赞

view.addSubview(view1)

此行会将 view1 添加为 view 的 subview 。

此外,现在 view1 位于 view 之上。所以现在当你触摸你的屏幕时,它就是你正在访问的 view1。 所以下次你向右滑动时,手势识别器应该在 view1 上。

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

view1.addGestureRecognizer(swipeRight)

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

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

您可以从 super View 中删除此 view1,例如,

view1.removeFromSuperView()

现在 view1 不见了,现在您可以通过与添加 view1 类似的过程添加 view2

请看下面的代码,它可能是相关的。

//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 添加了一个 View ,您没有引用要添加的 View 。您应该首先存储您添加的 View ,如 view3 = CustomView(frame: self.view.bounds) 以便稍后您可以删除 view3。此外,您的 view1 实际上永远不会被添加 如果我是对的,因为要添加 view1 您需要先添加 view.addSubview(view1)

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

关于swift - 添加新的 UIView 后如何删除以前的堆栈?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56310964/

相关文章:

ios - 使用 Quartzcore/layer 向 UIView 添加顶部边框?

ios - 为什么我在 setValue :forUndefinedKey:? 中得到 NSUnknownKeyException

ios - 将数据从一个类传递到另一个类

ios - Facebook SDK 和 Firebase

swift - xcode swift 如何在图像周围环绕文字

ios - React Native 标签栏应用倒置不旋转

ios - 如何在 viewWillAppear 中将 UIScrollView 滚动到底部,当使用自动布局时,没有视觉动画

swift - 为什么在 Swift 中对泛型协议(protocol)的元组进行类型别名允许我将其视为非泛型协议(protocol)?

ios - 从 UIButton 持续发送消息

ios - 如何将一个单元格加载到另一个单元格的 View 中。?