用户在alertview中按下alta后,如何解除blureffect?

时间:2017-02-07 18:37:01

标签: swift popup alert uiblureffect

如果用户想要返回,这是blureffect和警告。我找不到任何我想要的东西,所以我希望有人可以帮助我。感谢您的帮助!

这是所有代码,但新代码。

       //
//  Visartotal.swift
//  Segment controll
//
//  Created by Simon Harrysson on 2017-01-28.
//  Copyright © 2017 Simon Harrysson. All rights reserved.
//

import UIKit

var Allabetygtillsamans: Double = 0


class Visartotal: UIViewController
{

    @IBOutlet weak var Tillbaka: UIButton!
    @IBOutlet weak var Börjaomknapp: UIButton!
    @IBOutlet weak var Visartotalreligion: UILabel!
    @IBOutlet weak var VisartotalIMusik: UILabel!
    @IBOutlet weak var Visartotaltmerit: UILabel!
    @IBOutlet weak var VisartotalBild: UILabel!
    @IBOutlet weak var Visartotalmatematik:UILabel!
    @IBOutlet weak var VisartotalEngelska: UILabel!

    var blurEffectView: UIVisualEffectView?

    override func viewDidAppear(_ animated: Bool)
    {
        self.blurEffect = UIBlurEffect(style: .dark)
        self.blurEffectView = UIVisualEffectView(effect: self.blurEffect)
         blurEffectView?.frame = view.bounds
        blurEffectView?.autoresizingMask = [.flexibleWidth, .flexibleHeight] // for supporting device rotation


    }
    var blurEffect: UIBlurEffect?

       @IBAction func TillbakaAction(_ sender: UIButton)
    {
         createAlert(title: "Är du säker på att du vill börja om?", message: "Ifyllda betyg nollställs")
    blurEffectView = UIVisualEffectView(effect: blurEffect)

               view.addSubview(blurEffectView!)
        }

    func createAlert (title:String, message:String)
    {
        let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)

        //CREATING ON BUTTON
        alert.addAction(UIAlertAction(title: "Ja", style: UIAlertActionStyle.default, handler: {
            (action) in
            alert.dismiss(animated: true, completion: nil)
            print ("Jag vill gå tillbaka")
            self.performSegue(withIdentifier: "Tillbaka", sender: nil)
            self.blurEffectView?.removeFromSuperview()

                    }))

        alert.addAction(UIAlertAction(title: "Nej", style: UIAlertActionStyle.default, handler: { (action) in
            alert.dismiss(animated: true, completion: nil)
            print("Nej, jag vill inte gå tillbaka")
            self.blurEffectView?.removeFromSuperview()

        }))

        self.present(alert, animated: true, completion: nil)



    }

    override func viewDidLoad()
    {


        let Allabetygtillsamans = MusikbetygIDouble + BildbetygValtDouble + MatematikbetygvaltIDouble + ReligionbetygvaltDouble + EngelskabetygvaltDouble

        Visartotaltmerit.font = UIFont(name: "Arial", size: 21)


        let totalameritet = String(Allabetygtillsamans)


        VisartotalIMusik.text = MusikbetygValt
        VisartotalBild.text = BildbetygValt
        Visartotalmatematik.text = Matematikbetygvalt
        Visartotalreligion.text = Religionbetygvalt
        VisartotalEngelska.text = Engelskabetygvalt

        Visartotaltmerit.text = totalameritet + ("p")


        if BildbetygValt == "F"
        {
            VisartotalBild.textColor = UIColor.red
        }
        if MusikbetygValt == "F"
        {
            VisartotalIMusik.textColor = UIColor.red
        }
        if Matematikbetygvalt == "F"
        {
            Visartotalmatematik.textColor = UIColor.red
        }
        if Religionbetygvalt == "F"
        {
            Visartotalreligion.textColor = UIColor.red
        }
        if Engelskabetygvalt == "F"
        {
            VisartotalEngelska.textColor = UIColor.red
        }



        switch Allabetygtillsamans {
        case 290...340:
            Visartotaltmerit.textColor = UIColor.green
            Visartotaltmerit.backgroundColor = UIColor.clear
            Visartotaltmerit.backgroundColor = UIColor.clear
        case 230...290:
            Visartotaltmerit.textColor = UIColor.yellow
            Visartotaltmerit.backgroundColor = UIColor.clear
            Visartotaltmerit.backgroundColor = UIColor.clear
        case 220...230:
            Visartotaltmerit.textColor = UIColor.orange
            Visartotaltmerit.backgroundColor = UIColor.clear
        case 0...220:
            Visartotaltmerit.textColor = UIColor.red
            Visartotaltmerit.backgroundColor = UIColor.clear
        default:
            print("defult")
        }



    func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }





}
}

1 个答案:

答案 0 :(得分:1)

您需要创建一个blurEffectView变量,您可以在tillbakaAction函数的范围之外访问该变量 - viewController中的某个位置。然后,在您的UIAlertActions中,您可以说self.blurEffectView.removeFromSuperview()将其删除。没有动画 - 它就会消失。

此外,正如另一位意见提供者所指出的那样,您不需要说alert.dismiss...,因为这会自动发生。

作为变量的示例,这将起作用:

var blurEffectView: UIVisualEffectView?

@IBAction func tillbakaAction(_ sender: UIButton) {
    ...
    self.blurEffectView = UIVisualEffectView(effect: blurEffect)
    ...
}

在你的行动中:

alert.addAction(UIAlertAction(title: "Ja", style: UIAlertActionStyle.default, handler: {
    (action) in
    self.blurEffectView?.removeFromSuperview()
    ...