Questa è stata una grande risposta, ma ci sono voluti alcuni ritocchi per me per farlo bene.
Quindi l'idea generale è che tu abbia un testo nel mezzo della tua scrollView e quando l'utente scorre oltre quel testo allora vuoi che diventi il nuovo titolo. Inoltre, quando si scorre indietro, si desidera tornare al titolo predefinito.
Quindi, utilizzando il codice che Gix postato, ora convertito in Swift 3, ecco come avere fatto.
aggiungere queste variabili alla parte superiore del vostro viewController
var didChangeTitle = false
let defaultTitle = "Default Title"
let animateUp: CATransition = {
let animation = CATransition()
animation.duration = 0.5
animation.type = kCATransitionPush
animation.subtype = kCATransitionFromTop
animation.timingFunction = CAMediaTimingFunction.init(name: kCAMediaTimingFunctionEaseInEaseOut)
return animation
}()
let animateDown: CATransition = {
let animation = CATransition()
animation.duration = 0.5
animation.type = kCATransitionPush
animation.subtype = kCATransitionFromBottom
animation.timingFunction = CAMediaTimingFunction.init(name: kCAMediaTimingFunctionEaseInEaseOut)
return animation
}()
Nella tua viewDidLoad, aggiungere questo codice per impostare il titolo predefinito.
let titleLabelView = UILabel.init(frame: CGRect(x: 0, y: 0, width: 200, height: 44))
titleLabelView.backgroundColor = .clear
titleLabelView.textAlignment = .center
titleLabelView.textColor = UINavigationBar.appearance().tintColor
titleLabelView.font = UIFont.boldSystemFont(ofSize: 16)
titleLabelView.text = defaultTitle
self.navigationItem.titleView = titleLabelView
Ora è aggiungere del codice alla funzione delegato ScrollView:
extension MyViewController: UIScrollViewDelegate {
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if scrollView.contentOffset.y >= (labelName.frame.origin.y + labelName.frame.height) && !didChangeTitle {
if let label = navigationItem.titleView as? UILabel {
label.layer.add(animateUp, forKey: "changeTitle")
label.text = labelName.text
}
didChangeTitle = true
} else if scrollView.contentOffset.y < labelName.frame.origin.y && didChangeTitle {
if let label = navigationItem.titleView as? UILabel {
label.layer.add(animateDown, forKey: "changeTitle")
label.text = defaultTitle
}
didChangeTitle = false
}
}
}
Il var "nomeEtichetta" è l'etichetta nel vostro ScrollView che contiene il titolo futuro.
fonte
2017-08-30 21:44:09
La mia ipotesi è che abbiano una visualizzazione personalizzata nella barra di navigazione, nella quale fanno l'animazione. – rckoenes
Come potrei fare per farlo? – Jacob
Cosa hai provato? Sembra che tu stia chiedendo il codice, prova a risolvere il problema tu stesso – rckoenes