微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

cocoa – 在一定时间后隐藏NSUserNotification

目前,当我使用警报样式创建NSUserNotification时,除非我手动关闭它,否则它将不会隐藏.

enter image description here

有没有办法可以在2秒后自动关闭/隐藏它?

NSUserNotification代码仅供参考:

let notification:NSUserNotification = NSUserNotification()
notification.title = "Title"
notification.subtitle = "Subtitle"
notification.informativeText = "informative text"

notification.soundName = NSUserNotificationDefaultSoundName

notification.deliveryDate = NSDate(timeIntervalSinceNow: 10)
notification.hasActionButton = false
let notificationcenter:NSUserNotificationCenter = NSUserNotificationCenter.defaultUserNotificationCenter()
notificationcenter.scheduleNotification(notification)

解决方法

使用NSObject来实现这一点非常简单
performSelector:withObject:afterDelay:方法.

由于您在一定时间间隔后安排通知传递,因此您需要在解除之前将额外延迟添加到传递之前的初始延迟.在这里,我把它们写成交货前10秒的常数,以及解雇前2秒:

let delayBeforeDelivering: NSTimeInterval = 10
let delayBeforedismissing: NSTimeInterval = 2

let notification = NSUserNotification()
notification.title = "Title"
notification.deliveryDate = NSDate(timeIntervalSinceNow: delayBeforeDelivering)

let notificationcenter = NSUserNotificationCenter.defaultUserNotificationCenter()

notificationcenter.scheduleNotification(notification)

notificationcenter.performSelector("removeDeliverednotification:",withObject: notification,afterDelay: (delayBeforeDelivering + delayBeforedismissing))

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐