ios

Create a auto hide popup with swift

by
published on
Mar 05, 2015 19:401
So i needed a popup kind of view for an IOS app. The purpose of the popup was to display a message when a gesture action has taken place. Gestures can be confusing for people and for complex gesture its better to show some kind of message so that user knows that something happened. Here is the code. 1) Take a ViewController. Add a button and a UIVIew on it. In the UIView add a label. This label can be changed based on the message that needs to be passed.
Screenshot at Mar 05 19-44-25
2) In the view controller we add our code.  Code has 2 methods.  On button click i am displaying the popup and triggering a timer.  2nd method gets called when the timer is over. In the 2nd method i hide the popup with animation.
class ViewController: UIViewController {

    @IBOutlet weak var label: UILabel!
    @IBOutlet weak var PopUpView: UIView!
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        PopUpView.hidden = true
        PopUpView.layer.cornerRadius = 5;
        PopUpView.layer.masksToBounds = true
    }

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

    @IBAction func buttonClick(sender: AnyObject) {
        PopUpView.hidden = false
        PopUpView.alpha = 1.0
         var timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("update"), userInfo: nil, repeats: false)
    }

    func update() {
      //  PopUpView.hidden = true
        UIView.animateWithDuration(1.0, delay: 0.0, options: UIViewAnimationOptions.CurveEaseIn, animations: {
            self.PopUpView.alpha = 0.0
            }, completion: nil)
        
    }
}