我有一个分页的scrollView,我想删除未显示(或动画)的视图。我不能解决它。我想从显示的视图中清除-1和+1视图之外的视图,以减少内存。
func addViewController(vc:UIViewController)->Void{
controllers.append(vc)
// Setup the viewController view
vc.view.setTranslatesAutoresizingMaskIntoConstraints(false)
scrollview.addSubview(vc.view)
// Constraints
let metricDict = ["w":vc.view.bounds.size.width,"h":vc.view.bounds.size.height]
// - Generic cnst
vc.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[view(h)]", options:nil, metrics: metricDict, views: ["view":vc.view]))
vc.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[view(w)]", options:nil, metrics: metricDict, views: ["view":vc.view]))
scrollview.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[view]|", options:nil, metrics: nil, views: ["view":vc.view,]))
// cnst for position: 1st element
if controllers.count == 1{
scrollview.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[view]", options:nil, metrics: nil, views: ["view":vc.view,]))
// purge controllers.count > 2
// cnst for position: other elements
}else{
let previousVC = controllers[controllers.count-2]
let previousView = previousVC.view;
// purge controllers.count before previousView
scrollview.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[previousView]-0-[view]", options:nil, metrics: nil, views: ["previousView":previousView,"view":vc.view]))
if let cst = lastViewConstraint{
scrollview.removeConstraints(cst as [AnyObject])
}
lastViewConstraint = NSLayoutConstraint.constraintsWithVisualFormat("H:[view]-0-|", options:nil, metrics: nil, views: ["view":vc.view])
scrollview.addConstraints(lastViewConstraint! as [AnyObject])
}