Search code examples
swiftpopupalertuiblureffect

How do I dismiss the blureffect after the user pressed a alternativ in alertview?


This is the blureffect and the alert that showes up if the user wants to go back. i could not find anything that I understod so I hope that someone here could help me. Thanks in advice for your help!

This is all the code please but the new code in.

       //
//  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.
    }





}
}

Solution

  • You will need to create a blurEffectView variable that you can access from outside the scope of your tillbakaAction function - somewhere in the viewController. Then, in your UIAlertActions you can say self.blurEffectView.removeFromSuperview() to remove it. There will be no animation - it'll just disappear.

    Also, as another commenter noted, you don't need to say alert.dismiss... as that happens automatically.

    As an example for your variable, this would work:

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

    And in your action:

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