在我的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. iPhone中的UITextView滚动(自动)?
- 7. swift中的自动标签高度3
- 8. 根据mp3/wav自动嘴唇同步
- 9. 的UITextView自动高度
- 10. 如何在uitextview中控制自动滚动的速度
- 11. 滚动长度如何根据网页长度的增加而改变
- 12. 根据其内容动态调整UITextView的高度?
- 13. 滚动速度自动滚动速度
- 14. UIScrollView不滚动Swift 3
- 15. iOS UITextView自动滚动从上到下
- 16. UITextView自动滚动到最后一行
- 17. 在iOS中自动滚动UITextView ..
- 18. xcode 6无法滚动长uitextview
- 19. UITextView滚动
- 20. UITextView不滚动
- 21. 自定义UIScrollView滚动自定义UITextView
- 22. 根据当前滚动位置在Recyclerview中自动滚动
- 23. Swift:自动调整单元格内的高度UITextView
- 24. 禁用自动更正UITextView Swift
- 25. UITextView更改高度而不是滚动
- 26. 如何使用Swift 3在UIScrollview中实现自动滚动?
- 27. UITextView分页不滚动
- 28. 根据滚动调整div高度
- 29. UITextView - 水平滚动?
- 30. 用UIScrollview滚动UITextview
您是否尝试过使用'Timer'? – nighttalker
使用计时器并根据玩家的位置设置UITextView的滚动条。 –
@nighttalker,你可以详细说明一下吗?我想过使用计时器,但不知道如何根据歌曲的当前时间自动滚动文本视图 – SergeH