Ich habe eine benutzerdefinierte UIView, die den Bildschirm abdecken soll, sobald der Benutzer auf eine Schaltfläche klickt. Es simuliert eine benutzerdefinierte Ansicht. In der benutzerdefinierten UIView gibt es eine untergeordnete UIView, die von unten (zunächst verborgen) bis zur normalen Position (unten) animiert werden sollte. Das Problem, das ich habe, ist, dass es wie Layoutsubviews ist ein schlechter Ort, um mit Animationen zu beginnen. Wo wäre der richtige Ort? Etwas wie viewDidAppear, aber für UIViews.Starten Sie die Animation, sobald eine UIView auf dem Bildschirm angezeigt wird
In UIViewController:
let rect: CGRect = CGRectMake(0, 0, view.bounds.size.width, view.bounds.size.height)
let alertView = AlertView(frame: rect)
view.addSubview(alertView)
Im Alertview:
import UIKit
class AlertView: UIView {
let nibName = "AlertView"
let animationDuration = 0.5
var view: UIView!
@IBOutlet weak var notificationView: UIView!
@IBOutlet weak var notificationBottomConstraint: NSLayoutConstraint!
override init(frame: CGRect) {
super.init(frame: frame)
viewSetup()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
viewSetup()
}
override func didAddSubview(subview: UIView) {
super.didAddSubview(subview)
}
func viewSetup() {
view = loadViewFromNib()
view.frame = bounds
view.autoresizingMask = [UIViewAutoresizing.FlexibleWidth, UIViewAutoresizing.FlexibleHeight]
// move the notification view offscreen
notificationBottomConstraint.constant = -notificationView.frame.size.height
addSubview(view)
}
func loadViewFromNib() -> UIView {
let bundle = NSBundle(forClass: self.dynamicType)
let nib = UINib(nibName: nibName, bundle: bundle)
return nib.instantiateWithOwner(self, options: nil)[0] as! UIView
}
override func layoutSubviews() {
super.layoutSubviews()
print("layoutSubviews")
animate()
}
func animate() {
// move the notification up
self.notificationBottomConstraint.constant = 0
UIView.animateWithDuration(animationDuration) {() -> Void in
self.view.setNeedsDisplay()
}
}
}
Es braucht nicht innerhalb der Animation Schließung zu sein. Arbeitet auch draußen. –