xcode 以编程方式快速转场

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24535391/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 05:09:32  来源:igfitidea点击:

Segue programmatically in swift

iosxcodeswift

提问by Vladimir

I'm trying to do the following in my app: I made a file named "blur.swift", this file has code to blur the up side of a view so that everything that passes there gets "blurred". I have made this file the class to one view in Interface Builder. It worked perfectly, except for one detail: I cannot put a button via IB in the sae region that receives the blur, because the button also gets blurred and does not work, and I need to insert this button there because that is the button the user will press to return to the previous screen… so I decided to insert the button programmatically in the same file that has the code with the blur effect, now it worked! The blur is there and the button too!

我正在尝试在我的应用程序中执行以下操作:我创建了一个名为“blur.swift”的文件,该文件具有模糊视图上部的代码,以便通过那里的所有内容都变得“模糊”。我已将此文件作为 Interface Builder 中的一个视图的类。它工作得很好,除了一个细节:我不能通过 IB 在接收模糊的 sae 区域中放置一个按钮,因为按钮也变得模糊并且不起作用,我需要在那里插入这个按钮,因为那是按钮用户将按下返回上一屏幕......所以我决定以编程方式将按钮插入到具有模糊效果代码的同一个文件中,现在它起作用了!模糊在那里,按钮也在那里!

The problem that I cannot solve is how to make this button actually works, I tried using "performSegueWithIdentifier" but I'm certain I'm missing something here, because it simple does not work! So I decided to ask for help here, below is the entire code from my "blur.swift" file.

我无法解决的问题是如何使这个按钮真正起作用,我尝试使用“performSegueWithIdentifier”,但我确定我在这里遗漏了一些东西,因为它简单不起作用!所以我决定在这里寻求帮助,下面是我的“blur.swift”文件中的完整代码。

Just to make things more clear: the segue with identifier "back" goes from the view that has the "blur.swift" class to the view controller (named giraanam) that I want to open when the user press the button.

只是为了让事情更清楚:带有标识符“back”的 segue 从具有“blur.swift”类的视图转到我想在用户按下按钮时打开的视图控制器(名为 giraanam)。



@IBDesignable

class blur: UIView {

    override func layoutSubviews() {

        super.layoutSubviews()


        // Insert blur effect
        var blur:UIBlurEffect = UIBlurEffect(style: UIBlurEffectStyle.Light)
        var effectView:UIVisualEffectView = UIVisualEffectView (effect: blur)
        effectView.frame = CGRectMake(0,0,320,69)
        self.addSubview(effectView)

        ///Insert the button
        let button   = UIButton.buttonWithType(UIButtonType.System) as UIButton
        button.frame = CGRectMake(20, 25, 34, 34)
        button.setBackgroundImage(UIImage(named:"Back_Resultados.png"),forState: UIControlState.Normal)
        button.performSegueWithIdentifier("back", sender: self)
        self.addSubview(button)


    }

}


Thanks to you all!

谢谢大家!

回答by Matt Long

You know that layoutSubviews gets called very frequently right? The code you're using would attempt to call performSegueWithIdentifier every time layoutSubviews is called. You want to instead create your button once and add a target to the button--something like this:

您知道 layoutSubviews 被非常频繁地调用对吗?每次调用 layoutSubviews 时,您使用的代码都会尝试调用 performSegueWithIdentifier。您想改为创建一次按钮并向按钮添加目标 - 如下所示:

class blur: UIView {

    var button: UIButton?

    override func layoutSubviews() {

        super.layoutSubviews()

        // Insert blur effect
        var blur:UIBlurEffect = UIBlurEffect(style: UIBlurEffectStyle.Light)
        var effectView:UIVisualEffectView = UIVisualEffectView (effect: blur)
        effectView.frame = CGRectMake(0,0,320,69)
        self.addSubview(effectView)

        // Insert the button if it doesn't exist already
        if !button {
            button = UIButton.buttonWithType(UIButtonType.System) as UIButton
            button.frame = CGRectMake(20, 25, 34, 34)
            button.setBackgroundImage(UIImage(named:"Back_Resultados.png"),forState: UIControlState.Normal)
            // Add self as a target
            button.addTarget(self, action: "didTapButton:", forControlEvents: .TouchUpInside)
            self.addSubview(button)
        }

    }

    func didTapButton(sender: UIButton!) {
        // Perform segue here or call out to a delegate to perform the segue
    }

}

Here's a SO post on how to programmatically make a button in Swift: Make a UIButton programmatically in Swift

这是关于如何在 Swift 中以编程方式制作按钮的 SO 帖子:Make a UIButton programmatically in Swift

You really should consider moving all of this code into a setup method that only gets called once when the view is created rather than on every call to layoutSubviews. Also, you probably want to add a delegate to this view class and set it to your view controller. Then implement a method in the view controller that can perform the segue for you.

您真的应该考虑将所有这些代码移动到一个设置方法中,该方法仅在创建视图时调用一次,而不是在每次调用 layoutSubviews 时调用。此外,您可能希望向此视图类添加一个委托并将其设置为您的视图控制器。然后在视图控制器中实现一个可以为您执行转场的方法。

(p.s. Some of my syntax may be off. I wrote this in the browser and I'm still wrestling with Swift)

(ps 我的一些语法可能是关闭的。我在浏览器中写了这个,我仍在与 Swift 搏斗)