弹出通知视图,独立于 swift 中的当前视图控制器

Popover notification view, independent of current view controller in swift

我想通知用户某项操作已在后台完成。目前,AppDelegate 收到通知:

func didRecieveAPIResults(originalRequest: String, apiResponse: APIResponse) {
    if(originalRequest == "actionName") {
           // do something
    }
}

我真的很想在当前活动视图上显示弹出式通知(例如 "Awarded points to 10 students")。

我知道如何使用 NSNotification 执行此操作,但这意味着我必须为每个视图添加一个侦听器。如果有替代方案那就太好了!

问题的下一部分是我如何真正让视图淡入然后在我拥有的任何视图前面再次淡出 - 无论是 table 视图、集合视图还是其他任何视图。我尝试了以下代码(在 viewDidLoad 中进行测试):

override func viewDidLoad() {
    // set up views
    let frame = CGRectMake(0, 200, 320, 200)
    let notificationView = UIView(frame: frame)
    notificationView.backgroundColor = UIColor.blackColor()
    let label = UILabel()
    label.text = "Hello World"
    label.tintColor = UIColor.whiteColor()

    // add the label to the notification
    notificationView.addSubview(label)

    // add the notification to the main view
    self.view.addSubview(notificationView)

    print("Notification should be showing")

    // animate out again
    UIView.animateWithDuration(5) { () -> Void in
        notificationView.hidden = true
        print("Notification should be hidden")
    }
}

视图确实在没有隐藏动画的情况下出现,但其中包含该代码会立即隐藏。我也不确定如何将其粘贴到视图的底部,尽管也许最好将其保存到另一个问题中。我假设我在这里做错了一些事情,所以任何指向我正确方向的建议都会很棒!谢谢!

关于您的通知问题,也许 UIAlertController 适合您的需要?

这也可以解决 in/out UIView

淡出的问题
func didRecieveAPIResults(originalRequest: String, apiResponse: APIResponse) {
    if(originalRequest == "actionName") {

        // Creates an UIAlertController ready for presentation
        let alert = UIAlertController(title: "Score!", message: "Awarded points to 10 students", preferredStyle: UIAlertControllerStyle.Alert)

        // Adds the ability to close the alert using the dismissViewControllerAnimated
        alert.addAction(UIAlertAction(title: "Close", style: UIAlertActionStyle.Cancel, handler: { action in    alert.dismissViewControllerAnimated(true, completion: nil)}))

        // Presents the alert on top of the current rootViewController            
        UIApplication.sharedApplication().keyWindow?.rootViewController?.presentViewController(alert, animated: true, completion: nil)
    }
}

UIAlertController


添加子视图时,您希望位于其他所有内容之上,请执行以下操作:

self.view.addSubview(notificationView)
self.view.bringSubviewToFront(notificationView)

通过直接更改 alpha 来淡化 UIView:

为了进行测试,您应该在 viewDidAppear 中调用它,以便在实际显示视图 之后 开始淡入淡出动画。

// Hides the view
UIView.animateWithDuration(5) { () -> Void in
    notificationView.alpha = 0
}

// Displays the view
UIView.animateWithDuration(5) { () -> Void in
    notificationView.alpha = 0
}

此解决方案在您的代码中占用了不必要的 space,我建议为此目的进行扩展。


分机:

创建一个 Extensions.swift 文件并将以下代码放入其中。

用法:myView.fadeIn()myView.fadeOut()

import UIKit    

extension UIView {

    // Sets the alpha to 0 over a time period of 0.15 seconds
    func fadeOut(){
        UIView.animateWithDuration(0.15, animations: {
            self.alpha = 0
        })
    }

    // Sets the alpha to 1 over a time period of 0.15 seconds
    func fadeIn(){
        UIView.animateWithDuration(0.15, animations: {
            self.alpha = 1
        })
    }
}

Swift 2.1 Extensions

希望这对您有所帮助! :)