-1

当我在应用程序处于活动状态时收到推送通知时,我会创建一个显示的 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 执行此操作的方法。

任何想法?

4

1 回答 1

0

声明一个 3 秒的计时器

var timer = NSTimer()

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

    func myFunc(){
       // Called after 3 seconds
    }
于 2015-11-30T23:12:15.570 回答