Swift 如何在 3 秒后从 AppDelegate 中删除 FromSuperview

How to removeFromSuperview from AppDelegate after 3 seconds in Swift

当我在应用处于活动状态时收到推送通知时,我创建了一个显示的 UIView。问题是视图不会消失,但会永远存在。这是代码:

    func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {

    let rect = CGRect(x: 0, y: 0, width: UIScreen.mainScreen().bounds.width, height: 65)

    let myView = UIView(frame: rect)

    let myViewLabel = UILabel(frame: rect)

    //myViewLabel.text = "Hola"

    myView.addSubview(myViewLabel)

    myView.backgroundColor = .orangeColor()

    if let aps = userInfo["aps"] as? NSDictionary {

        if let alert = aps["alert"] as? NSDictionary {

            if let message = alert["message"] as? NSString {

                //do stuff

            }
        } else if let alert = aps["alert"] as? NSString {

            if application.applicationState == .Active{

                AudioServicesPlayAlertSound(SystemSoundID(kSystemSoundID_Vibrate))

                myViewLabel.text = alert as String

                self.window?.addSubview(myView)

            }

        }
    }

}

关注

self.window?.addSubview(myView)

它永远留在那里。我想做一些像 animateWithDuration 这样的事情来在 3 秒内隐藏,但我找不到从 AppDelegate 中做到这一点的方法。

有什么想法吗?

声明一个 3 秒的计时器

var timer = NSTimer()

    timer = NSTimer.scheduledTimerWithTimeInterval(3, target: self, selector: Selector("myFunc"), userInfo: nil, repeats: false)

    func myFunc(){
       // Called after 3 seconds
    }