2015-12-25 3 views
2

Я хотел бы просмотреть всплывающее окно с одной меткой в ​​нем, которая медленно исчезает в течение одной секунды, чтобы сообщить пользователю о выборе, который он сделал, произошел обновление или что-то еще.Исчезновение всплывающего окна предупреждения

Я использую animatewithduration, но поскольку у них может быть много разных предупреждений, я бы хотел создать класс, чтобы избежать создания вида, метки и func в любом UIViewController, который может отображать этот вид предупреждения ... Вид:

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 

Ничто.

+0

Нашел это ..... Это вопрос только позиционирования .... больше нет popUpLabel.center ... и он появляется !!!! – Hal

ответ

0

УБ пришел из центрирования этикетки внутри представления ... Вот класс и работает отлично! :

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) 

} 
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") 
    } 
} 
+0

, если ответ был полезен для вас, не забудьте принять и упредить .. :) –