Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/107.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios Swift 3-根据mp3的长度自动滚动UITextView_Ios_Swift_Scroll_Uitextview_Mp3 - Fatal编程技术网

Ios Swift 3-根据mp3的长度自动滚动UITextView

Ios Swift 3-根据mp3的长度自动滚动UITextView,ios,swift,scroll,uitextview,mp3,Ios,Swift,Scroll,Uitextview,Mp3,在我的iOS应用程序中,我在可滚动的不可编辑文本视图中播放MP3的歌词。我想根据每个mp3的长度自动滚动文本视图。如何在Swift 3中执行此操作?您只需安排一个计时器,并计算在更新方法(选择器)中滚动的位置 在你们班的某个地方: var timer = Timer() func update() { // Assuming you're using an AVAudioPlayer, calculate how far along you are let progress =

在我的iOS应用程序中,我在可滚动的不可编辑文本视图中播放MP3的歌词。我想根据每个mp3的长度自动滚动文本视图。如何在Swift 3中执行此操作?

您只需安排一个
计时器,并计算在更新方法(选择器)中滚动的位置

在你们班的某个地方:

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)

歌曲结束后,使计时器无效

您所需要做的就是安排一个
计时器
并计算在更新方法(选择器)中滚动的位置

在你们班的某个地方:

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)

歌曲结束后,使计时器无效

您可以设置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)

您可以设置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)

您是否尝试过使用
计时器
?使用计时器并根据播放机的位置设置UITextView的滚动。@nighttalker,您能详细说明一下吗?我曾想过使用计时器,但不知道如何根据当前播放时间自动滚动文本视图song@SinaKH,请参阅我之前的评论您是否尝试过使用
计时器
?使用计时器并根据播放机的位置设置UITextView的滚动。@nighttalker,您能详细说明一下吗?我曾想过使用计时器,但不知道如何根据当前播放时间自动滚动文本视图song@SinaKH,请看我之前的评论,很好的答案!只是想知道,这不会与歌词同步,因为它们不一定以恒定的速率流动。他可能需要把时间戳标记和歌词或其他东西放在一起,以便文本与声音精确地同步。它工作了!是的@Antoine是对的,滚动与歌词不同步。我没想清楚。我认为它不会像我想象的那样起作用。感谢anywayshow在搜索歌曲时处理滑块值增加的问题?有什么解决办法吗?很好的答案!只是想知道,这不会与歌词同步,因为它们不一定以恒定的速率流动。他可能需要把时间戳标记和歌词或其他东西放在一起,以便文本与声音精确地同步。它工作了!是的@Antoine是对的,滚动与歌词不同步。我没想清楚。我认为它不会像我想象的那样起作用。感谢anywayshow在搜索歌曲时处理滑块值增加的问题?有什么解决办法吗?