在我的iOS應用程序中,我在裏面播放着一個可滾動的不可編輯textview的mp3歌詞。我想根據每個mp3的長度自動滾動文本視圖。我如何在Swift 3中做到這一點?Swift 3 - 根據mp3的長度自動滾動UITextView
0
A
回答
3
您只需計劃Timer
並計算更新方法(選擇器)的滾動位置。
某處在您的類:
var timer = Timer()
func update() {
// Assuming you're using an AVAudioPlayer, calculate how far along you are
let progress = player.currentTime/player.duration
// Get the number of characters
let characters = textView.text.characters.count
// Calculate where to scroll
let location = Double(characters) * progress
// Scroll the textView
text.scrollRangeToVisible(NSRange(location: Int(location), length: 10))
}
然後,每當你想啓動計時器:
timer = Timer.scheduledTimer(timeInterval: 0.5, target: self, selector: #selector(update), userInfo: nil, repeats: true)
當歌曲結束時,無效計時器。
0
您可以設置contentOffset:
func autoScroll() {
let progress = currentTime/duration
let contentOffset = CGPoint(x: 0.0, y: textView.contentSize.height * progress)
textView.setContentOffset(contentOffset, animated: true)
}
以及起動計時器:
let timer = Timer(timeInterval: 0.5, target: self, selector: #selector(autoScroll), userInfo: nil, repeats: true)
RunLoop.current.add(timer, forMode: .commonModes)
相關問題
- 1. UITextView自動滾動
- 2. Swift:水平滾動UITextView
- 3. 輸入長度=值長度&根據值長度自動擴展
- 4. 自動滾動UITextView問題
- 5. JavaFX的 - 根據滾動窗格的水平長度尺度水平長度
- 6. 的UITextView自動高度
- 7. 如何在uitextview中控制自動滾動的速度
- 8. UITextView自動滾動到最後一行
- 9. xcode 6無法滾動長uitextview
- 10. UITextView滾動
- 11. UIScrollView不滾動Swift 3
- 12. 根據當前滾動位置在Recyclerview中自動滾動
- 13. Swift:自動調整單元格內的高度UITextView
- 14. UITextView更改高度而不是滾動
- 15. UITextView分頁不滾動
- 16. UITextView - 水平滾動?
- 17. 用UIScrollview滾動UITextview
- 18. 根據滾動調整div高度
- 19. 如何使用Swift 3在UIScrollview中實現自動滾動?
- 20. 限制自動滾動的文本長度NSTextView
- 21. SWIFT UITableview根據內容設置自動高度 - Xcode - iOS
- 22. 自定義UITextView滾動大小
- 23. UITextView的水平滾動
- 24. 根據長度
- 25. 根據UITextView的大小自動調整UIView的大小
- 26. css根據高度自動下拉
- 27. UITableView的自動滾動同時根據其他的UITableView
- 28. 爲什麼UITextView高度只能在滾動上變爲動態?
- 29. Swift 3:UIPickerView裏面滾動查看
- 30. 自動伸縮UITextView的,滾動視圖和靜止細胞
您是否嘗試過使用'Timer'? – nighttalker
使用計時器並根據玩家的位置設置UITextView的滾動條。 –
@nighttalker,你可以詳細說明一下嗎?我想過使用計時器,但不知道如何根據歌曲的當前時間自動滾動文本視圖 – SergeH