Kullanıcı bir düğmeyi tıkladığında ekranı kapatmak istediğim özel bir UIView var. Bu özel bir görünümü simüle eder. Özel UIView'de alttan (ilk başta gizli) normal konuma (altta görünür) kadar animasyonlu çocuk UIView var. Yapmakta olduğum problem, layoutSubviews'ın animasyon yapmaya başlamak için kötü bir yer olduğunu gösteriyor. Doğru yer neresi olurdu? ViewDidAppear gibi bir şey ama UIViews için. UIViewController yılındaUIView ekranda görüntülendiğinde animasyonu başlatın
: AlertView olarak
let rect: CGRect = CGRectMake(0, 0, view.bounds.size.width, view.bounds.size.height)
let alertView = AlertView(frame: rect)
view.addSubview(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()
}
}
}
Bu animasyon kapatılması içinde olması gerekmez. Dışarıda da çalışır. –