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

时间:2015-12-17 08:20:06

标签: ios swift uiview notifications

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

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

我真的想在当前有效的视图中显示弹出式通知(例如"向10名学生授予分数")。

我知道如何使用NSNotification执行此操作,但这意味着我必须为每个视图添加一个侦听器。替代方案会很棒!

问题的下一部分是我如何让视图淡入然后在我拥有的任何视图之前再次淡出 - 无论是表视图,集合视图还是其他任何视图。我已经尝试了以下代码(在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")
    }
}

视图确实没有隐藏动画,但是其中的代码会立即隐藏。我也不确定如何将其粘贴到视图的底部,尽管可能更好地保存了另一个问题。我想我在这里做了一些错事,所以任何指示我正确方向的建议都会很棒!谢谢!

1 个答案:

答案 0 :(得分:1)

对于您的通知问题,也许UIAlertController可能符合您的需求?

这也可以解决您淡出/退出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
}

此解决方案占用代码中不必要的空间,我建议为此目的使用扩展。

<强>扩展

创建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

希望这有帮助! :)