moyeshkhanal
moyeshkhanal

Reputation: 11

VisualEffectView in Swift and Xcode9

Picture of my code

import UIKit

class ViewController: UIViewController {

   @IBOutlet var addItemView: UIView!

   @IBOutlet weak var visualEffectView: UIVisualEffectView!

   var effect: UIVisualEffect!

    override func viewDidLoad() {
        super.viewDidLoad()

        effect = visualEffectView.effect
        visualEffectView.effect = nil

        addItemView.layer.cornerRadius = 5
   }

    func animateIn(){
        self.view.addSubview(addItemView)
        addItemView.center = self.view.center

        addItemView.transform = CGAffineTransform.init(scaleX: 1.3, y: 1.3)
        addItemView.alpha = 0

        UIView.animate(withDuration: 0.4){
            self.visualEffectView.effect = self.effect
            self.addItemView.alpha = 1
            self.addItemView.transform = CGAffineTransform.identity
        }
    }

    @IBAction func sourcesButton(_ sender: Any) {
        animateIn()
    }


    @IBAction func sourceDone(_ sender: Any) {
    }

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


}

I am trying to make a blurry modal that pops up when the user clicks a button, but for some reason this code is not executing. I was following a tutorial on youtube (Youtube Link) I would appreciate the help.

Upvotes: 1

Views: 358

Answers (1)

lex_shvets
lex_shvets

Reputation: 717

Create blur view like this instead:

var blurEffect = UIBlurEffect(style: UIBlurEffectStyle.Dark)
var blurEffectView = UIVisualEffectView(effect: blurEffect)
view.addSubview(blurEffectView)

And after it is created you can set alpha to 0 and animate it to 1.
Please note that animation with UIBlurEffect looks horrible on the device :p

Upvotes: 1

Related Questions