消失的弹出警报视图

Vanishing popup alert view

我想弹出一个带有单个标签的视图,该标签会在一秒钟内缓慢消失,以通知用户他所做的选择、发生的更新或其他任何事情。

我使用 animatewithduration,但因为它们可能是许多不同的警报,所以我想创建一个 class 以避免在可能显示那种警报的任何 UIViewController 中创建视图、标签和函数 .. . 种类 :

let doneAlert = PopUpAlert(parentView : self, textAlert : "You're done")

我希望显示 textAlert 的视图中的 parentView,然后在需要时显示:

doneAlert.display()

这是我写的 class :

class PopUpAlert: UIView {

convenience init(parentView : UIView,textAlert : String) {
    self.init(frame: CGRect(x: 0, y: 0, width: 200, height: 150))

    self.alpha = 0
    self.center = parentView.center

    let popUpLabel = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 150))
    popUpLabel.center = self.center
    popUpLabel.text = textAlert

    self.addSubview(popUpLabel)
    parentView.addSubview(self)
 }


func display() {
    PopUpAlert.animateWithDuration(0.5, delay: 0.0, options: UIViewAnimationOptions.CurveEaseOut, animations: {
        self.alpha = 1.0
        }, completion: nil)

    PopUpAlert.animateWithDuration(1.0, delay: 0.5, options: UIViewAnimationOptions.CurveEaseIn, animations: {
        self.alpha = 0.0
        }, completion: nil)

 }

}

这是我使用它的方式:

class CarteVC: UIViewController {

var daddy : RootViewController?
var goAlert : PopUpAlert?
@IBOutlet weak var mapView: MKMapView!

override func viewDidLoad() {
    super.viewDidLoad()
    goAlert = PopUpAlert(parentView: mapView, textAlert: "On the way ....")
}

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)
    let bb = UIBarButtonItem(title: "< List", style: .Plain, target: papa!, action: "pred")
    bb.tintColor = UIColor.lightGrayColor()
    daddy!.navigationItem.leftBarButtonItem = bb
    daddy!.navigationItem.rightBarButtonItem = nil

    goAlert!.display()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
 }



// MARK: - Navigation

没有显示。

如果你只想提醒用户并在一秒钟内消失,我建议你使用 Toast Swift

https://github.com/scalessec/Toast-Swift

这是你做吐司的方法

self.view.makeToast("Testing Toast")

// specific duration and position
self.view.makeToast("Testing Toast with duration and position", duration: 3.0, position: .Top)

// toast with image and all possible 
self.view.makeToast("testing toast image and all possible ", duration: 2.0, position: CGPoint(x: 110.0, y: 110.0), title: "Toast Title", image: UIImage(named: "toast.png"), style:nil) { (didTap: Bool) -> Void in
    if didTap {
        print("with tap")
    } else {
        print("without tap")
    }
}

pb 来自于视图内的标签居中...这是 class 并且工作得很好!!! :

class PopUpAlert: UIView {

convenience init(parentView : UIView,textAlert : String) {
    self.init(parentView: parentView,textAlert: textAlert,background: UIColor.darkGrayColor(),foreground: UIColor.whiteColor())
}

convenience init(parentView : UIView,textAlert : String, background : UIColor) {
    self.init(parentView: parentView,textAlert: textAlert,background: background,foreground: UIColor.whiteColor())
}

convenience init(parentView : UIView,textAlert : String, foreground : UIColor) {
    self.init(parentView: parentView,textAlert: textAlert,background: UIColor.darkGrayColor(),foreground: foreground)
}

convenience init(parentView : UIView,textAlert : String, background : UIColor, foreground : UIColor) {
    self.init(frame: CGRect(x: 0, y: 0, width: 150, height: 40))

    self.alpha = 0
    self.backgroundColor = background
    self.layer.cornerRadius = 5
    self.layer.masksToBounds = true
    self.center = (parentView.superview ?? parentView).center

    let popUpLabel = UILabel(frame: CGRect(x: 0, y: 0, width: 150, height: 40))
    popUpLabel.textAlignment = .Center
    popUpLabel.textColor = foreground
    popUpLabel.text = textAlert
    self.addSubview(popUpLabel)

    parentView.addSubview(self)
}

deinit {
    self.removeFromSuperview()
}


func display() {
    PopUpAlert.animateWithDuration(0.5, delay: 0.0, options: UIViewAnimationOptions.CurveEaseOut, animations: {
        self.alpha = 0.85
        }, completion: nil)

    PopUpAlert.animateWithDuration(1.0, delay: 0.7, options: UIViewAnimationOptions.CurveEaseIn, animations: {
        self.alpha = 0.0
        }, completion: nil)

}