目前 - 我正在构建flashcard应用程序。该应用程序显示一个闪存卡阵列,用户可以左右滑动(下面的代码)正如您所看到的,有11个不同的图像阵列(每个阵列包含一组特定的单词),所有这些阵列加起来最后一个阵列。我有一个音频文件来连接每个图像。点击屏幕上的声音(声音文件数组),xcode和swift
问题 - 我想这样做,当用户点击屏幕上的音频文件将播放。例如,如果湖图像正在显示并且用户点击屏幕,那么将会播放该lake.mp4音频文件,如果用户也将剩余的图像划过并点击屏幕,则lamb.mp4音频文件将播放等。 。我知道我必须添加一个轻敲手势识别器,但我不知道如何使每个音频文件都连接正确的图像。
下面的代码是我目前正在使用的。
import UIKit
class SecondViewController: UIViewController , UIGestureRecognizerDelegate {
@IBAction func home(_ sender: Any) {
performSegue(withIdentifier: "home", sender: self)
}
@IBOutlet weak var imgPhoto: UIImageView!
struct List {
let words: [String]
var active: Bool
}
let list1 = List(words:["lake", "lamb", "lamp", "lark", "leaf", "leash", "left", "leg", "lime", "lion", "lips", "list", "lock", "log", "look", "love", "lunch"], active: true)
let list2 = List(words: ["ladder", "ladybug", "laughing", "lawnmower", "lemon", "leopard", "leprechaun", "letters", "licking", "lifesaver", "lifting", "lightbulb", "lightning", "listen", "llama"], active: true)
let list3 = List(words: ["alligator", "balance", "ballerina", "balloon", "bowling", "cello", "colors", "curlyhair", "dollar", "dolphin", "elephant", "eyelashes", "gasoline", "goalie", "hula", "jellyfish", "olive", "pillow", "pilot", "polarbear", "rollerskate", "ruler", "silly", "telephone", "television", "tulip", "umbrella", "valentine", "violin", "xylophone", "yellow"], active: true)
let list4 = List(words: ["apple", "ball", "bell", "bubble", "castle", "fall", "fishbowl", "girl", "owl", "pail", "peel", "pool", "smile", "whale", "wheel"], active: true)
let list5 = List(words: ["planet", "plank", "plant", "plate", "play", "plum", "plumber", "plus"], active: true)
let list6 = List(words: ["black", "blanket", "blender", "blocks", "blond", "blood", "blow", "blue"], active: true)
let list7 = List(words: ["flag", "flipflop", "float", "floor", "flower", "fluffy", "flute", "fly"], active: true)
let list8 = List(words: ["glacier", "glad", "glasses", "glide", "glitter", "globe", "glove", "glue"], active: true)
let list9 = List(words: ["clam", "clamp", "clap", "claw", "clean", "climb", "clip", "cloud"], active: true)
let list10 = List(words:["sled", "sleep", "sleeves", "slice", "slide", "slime", "slip", "slow"], active: true)
let list11 = List(words: ["belt", "cold", "dolphin", "elf", "golf", "melt", "milk", "shelf"], active: true)
var imageIndex: Int = 0
var imageList: [String] {
let wordLists = [list1, list2, list3, list4, list5, list6, list7, list8, list9, list10, list11]
let active = wordLists.reduce([]) { (result:[String], list:List) in
if list.active {
return result + list.words
} else {
return result
}
}
return active
}
override func viewDidLoad() {
super.viewDidLoad()
imgPhoto.image = UIImage(named: imageList[imageIndex])
// Do any additional setup after loading the view.
imgPhoto.isUserInteractionEnabled = true
let leftSwipe = UISwipeGestureRecognizer(target: self, action: #selector(Swiped(gesture:)))
leftSwipe.cancelsTouchesInView = false
let rightSwipe = UISwipeGestureRecognizer(target: self, action: #selector(Swiped(gesture:)))
rightSwipe.cancelsTouchesInView = false
leftSwipe.direction = .left
rightSwipe.direction = .right
view.addGestureRecognizer(leftSwipe)
view.addGestureRecognizer(rightSwipe)
}
func Swiped(gesture: UIGestureRecognizer) {
if let swipeGesture = gesture as? UISwipeGestureRecognizer {
switch swipeGesture.direction {
case UISwipeGestureRecognizerDirection.right :
print("User swiped right")
// decrease index first
imageIndex -= 1
// check if index is in range
if imageIndex < 0 {
imageIndex = imageList.count - 1
}
imgPhoto.image = UIImage(named: imageList[imageIndex])
case UISwipeGestureRecognizerDirection.left:
print("User swiped Left")
// increase index first
imageIndex += 1
// check if index is in range
if imageIndex > imageList.count - 1 {
imageIndex = 0
}
imgPhoto.image = UIImage(named: imageList[imageIndex])
default:
break //stops the code/codes nothing.
}
}
}
}
import UIKit
class SecondViewController: UIViewController , UIGestureRecognizerDelegate {
var imageIndex: Int = 0
@IBAction func home(_ sender: Any) {
performSegue(withIdentifier: "home", sender: self)
}
@IBOutlet weak var imgPhoto: UIImageView!
let itemList:[Card] = [
Card(image: UIImage(named: "alligator")!, soundUrl: "Alligator.m4a"),
Card(image: UIImage(named: "apple")!, soundUrl: "Apple.m4a"),
Card(image: UIImage(named: "ball")!, soundUrl: "Ball.m4a")
]
override func viewDidLoad() {
super.viewDidLoad()
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(imageTapped(tapGestureRecognizer:)))
imgPhoto.isUserInteractionEnabled = true
imgPhoto.addGestureRecognizer(tapGestureRecognizer)
imgPhoto.image = (itemList[0]).image
// Do any additional setup after loading the view.
imgPhoto.isUserInteractionEnabled = true
let leftSwipe = UISwipeGestureRecognizer(target: self, action: #selector(Swiped(gesture:)))
leftSwipe.cancelsTouchesInView = false
let rightSwipe = UISwipeGestureRecognizer(target: self, action: #selector(Swiped(gesture:)))
rightSwipe.cancelsTouchesInView = false
leftSwipe.direction = .left
rightSwipe.direction = .right
view.addGestureRecognizer(leftSwipe)
view.addGestureRecognizer(rightSwipe)
}
func imageTapped(tapGestureRecognizer: UITapGestureRecognizer)
{
itemList[imageIndex].playSound()
// Your action
}
func Swiped(gesture: UIGestureRecognizer) {
if let swipeGesture = gesture as? UISwipeGestureRecognizer {
switch swipeGesture.direction {
case UISwipeGestureRecognizerDirection.right :
print("User swiped right")
// decrease index first
imageIndex -= 1
// check if index is in range
if imageIndex < 0 {
imageIndex = itemList.count - 1
}
imgPhoto.image = itemList[imageIndex].image
case UISwipeGestureRecognizerDirection.left:
print("User swiped Left")
// increase index first
imageIndex += 1
// check if index is in range
if imageIndex > itemList.count - 1 {
imageIndex = 0
}
imgPhoto.image = itemList[imageIndex].image
default:
break //stops the code/codes nothing.
}
}
}
}
import Foundation; import UIKit; import AVFoundation
class Card: NSObject
{
var image: UIImage
var soundUrl: String
var player: AVAudioPlayer?
init(image: UIImage, soundUrl: String) {
self.image = image
self.soundUrl = soundUrl
}
func playSound()
{
guard let url = Bundle.main.url(forResource: self.soundUrl, withExtension: "m4a") else { return }
do
{
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
try AVAudioSession.sharedInstance().setActive(true)
player = try AVAudioPlayer(contentsOf: url)
guard let player = player else { return }
player.play()
print("hhh")
} catch let error {
print(error.localizedDescription)
}
}
}
我在我的代码中添加了修订版,并且出现了一些错误。因为我将每张卡片作为一个对象,所以不会让我从他们身上制作一个字符串。我也收到了预期的声明错误。我编辑了这个问题并发布了修改后的代码和错误。 –
好吧,所以我把卡类放在一个单独的文件中。 Xcode告诉我没有AVPlayer这样的模块。同样在什么时候我应该插入单词[index] .playSound()?我不断收到错误“顶级不允许使用表达式”。另外,我为我的superviewdidload发现错误,说超级不能在类成员之外使用。谢谢 –
谢谢。正如你从我的原始代码中看到的那样,这些单词被分组到了11个不同的列表中,然后每个列表都有一串单词,并在最后有active = true。这是因为我有一个设置页面来打开和关闭每个单词列表。我怎么能有11个不同的“itemList”,并有每个列表活动=真?因为现在它不再是一个字符串,每个列表中都有一串字。 –