消失的弹出警报视图

时间:2015-12-25 16:02:40

标签: ios swift alert uiviewanimation

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

我使用animatewithduration,但因为它们可能有很多不同的警报,我想创建一个类来避免在任何可能显示那种警报的UIViewController中创建视图,标签和func ......种类:

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

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

doneAlert.display()

这是我写的课程:

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

没有显示任何内容。

2 个答案:

答案 0 :(得分:1)

如果您只想提醒用户并在一秒内消失,我建议您使用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")
    }
}

答案 1 :(得分:0)

pb来自视图内部的标签......这里是班级,工作得非常好!!! :

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)

}