gpt4 book ai didi

ios - 如何自动滚动 UITableView 单元格

转载 作者:可可西里 更新时间:2023-11-01 01:58:12 25 4
gpt4 key购买 nike

enter image description here

如何自动滚动 UITableView 的单元格。
,我觉得很容易实现,而且我做到了这么近

This is my SPEECH function in ViewController below

var myUtterance = AVSpeechUtterance(string: "")
let speecher = AVSpeechSynthesizer()
func speechWord(word: String){
DispatchQueue.main.async {
self.speecher.stopSpeaking(at: AVSpeechBoundary.immediate)
self.myUtterance = AVSpeechUtterance(string: word)
self.myUtterance.voice = AVSpeechSynthesisVoice(language: "en-US")
self.myUtterance.rate = 0.5
self.speecher.speak(self.myUtterance)
}
}

This is my AutoSpeech function in ViewController below

func startAutoSpeechList(row: Int){
let indexPath = IndexPath(row: row, section: 0)
print(indexPath)
myTableView.selectRow(at: indexPath, animated: true, scrollPosition: .top) //Select
myTableView.scrollToNearestSelectedRow(at: .top, animated: true) //Scroll
let cell = tableView(myTableView, cellForRowAt: indexPath) as! WordTableViewCell
self.speechWord(word: cell.spell.text!) //Speech
}

(Method 1) This is The Button I use to activate The AutoSpeech function

var row = 0
@IBAction func myAutoButton(_ sender: UIBarButtonItem) {
self.startAutoSpeechList(row: row)
row += 1
}

方法一,看我上传的图片 当我第一次点击 AutoButton 时,它跳转到屏幕 A。我再次点击 AutoSpeech 按钮,它跳转到屏幕 B。然后我点击 AutoButton第三次,跳转到屏幕C。

(Method 2) This is The Button I use to activate The AutoSpeech function

@IBAction func myAutoButton(_ sender: UIBarButtonItem) {
for myRow in 0..<3{
self.startAutoSpeechList(row: myRow)
}
}

方法 2, 当我单击 AutoSpeech 按钮时,它会立即跳转到屏幕 C,而不会在屏幕 A、B 停留任何时间。你能解释一下为什么它会立即逃离屏幕 A、B 吗?

Is there Method 3 to help me achieve that When I click AutoSpeech Button, it will jump to Screen A and pronounce the word "cat", then jump to B after finishing the pronunciation and pronounce "dog", then jump to C, and so on.... Who can help me to achieve Method 3? Swift 4 will be better for me.

最佳答案

您的方法 2 代码是您所需要的,但是它会连续运行并在几毫秒内完成。

可用的最佳选择是使用 AVSpeechSynthesizerDelegate监控方法确实讲完了。

在你的情况下你可以使用

func speechSynthesizer(AVSpeechSynthesizer, didFinish: AVSpeechUtterance)

在当前播放的语音结束播放时收到通知。一旦它被调用,你就可以调用一个函数,比如 speakNextRow 或者其他的东西来移动到列表中的下一个。

粗略地说,这是您应该考虑做的事情。 (未在 xcode 中测试,用作如何执行所需操作的指南)。

class ViewController: UIViewController, AVSpeechSynthesizerDelegate {

var currentSelectedRow: Int?
var isAutoPlaying = false

var myUtterance = AVSpeechUtterance(string: "")
let speecher = AVSpeechSynthesizer()

override func viewDidLoad() {
super.viewDidLoad()

self.speecher.delegate = self
}

func speechWord(word: String){

DispatchQueue.main.async {
self.speecher.stopSpeaking(at: AVSpeechBoundary.immediate)
self.myUtterance = AVSpeechUtterance(string: word)
self.myUtterance.voice = AVSpeechSynthesisVoice(language: "en-US")
self.myUtterance.rate = 0.5
self.speecher.speak(self.myUtterance)
}
}

func startAutoPlay() {
isAutoPlaying = true
currentSelectedRow = 0
scrollToAndSpeak(at: currentSelectedRow)
}

func scrollToAndSpeak(at index: Int) {
// make sure to check not going out of bounds!!
let indexPath = IndexPath(row: index, section: 0)
myTableView.selectRow(at: indexPath, animated: true, scrollPosition: .top) //Select
myTableView.scrollToNearestSelectedRow(at: .top, animated: true) //Scroll
let cell = tableView(myTableView, cellForRowAt: indexPath) as! WordTableViewCell
self.speechWord(word: cell.spell.text!) //Speech
}

func speechSynthesizer(AVSpeechSynthesizer, didFinish: AVSpeechUtterance) {
if isAutoPlaying {
// make sure to check not going out of bounds!!
currentSelectedRow = (currentSelectedRow ?? 0) + 1
scrollToAndSpeak(at: currentSelectedRow)
}
}
}

因此,当您选择自动播放时,将标志 isAutoPlaying 设置为 true 并开始播放第一个。完成后,代表将收到通知,我们会增加索引并开始播放下一个,依此类推,直到用户取消它或我们用完单元格。

关于ios - 如何自动滚动 UITableView 单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49295400/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com