2017-08-01 9 views
0

私はフラッシュカードアプリを作成しようとしています。私も正常にアプリを持っていた フラッシュカードの11の異なる配列があったすべての のこれらの配列は、私が を介してスワイプできる最後の配列に追加されたポイント。あなたが見ることができるように、各グループはその終わりに「アクティブ:真」を持っています。
これは、各単語グループをオンまたはオフにする設定ページがあるためです。複数の配列を追加して1つの最終配列を形成する。デバッグの問題、Swift Xcode

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! 

struct List { 
    let words: [Card] /*Create array of cards*/ 
    var active: Bool 
} 




let firstList:[Card] =[ 
    Card(image: UIImage(named: "lake")!, soundUrl: "Lake"), 
    Card(image: UIImage(named: "lamb")!, soundUrl: "Lamb"), 
    Card(image: UIImage(named: "lamp")!, soundUrl: "Lamp"), 
    Card(image: UIImage(named: "lark")!, soundUrl: "Lark"), 
    Card(image: UIImage(named: "leaf")!, soundUrl: "Leaf"), 
    Card(image: UIImage(named: "leash")!, soundUrl: "Leash"), 
    Card(image: UIImage(named: "left")!, soundUrl: "Left"), 
    Card(image: UIImage(named: "leg")!, soundUrl: "Leg"), 
    Card(image: UIImage(named: "lime")!, soundUrl: "Lime"), 
    Card(image: UIImage(named: "lion")!, soundUrl: "Lion"), 
    Card(image: UIImage(named: "lips")!, soundUrl: "Lips"), 
    Card(image: UIImage(named: "list")!, soundUrl: "List"), 
    Card(image: UIImage(named: "lock")!, soundUrl: "Lock"), 
    Card(image: UIImage(named: "log")!, soundUrl: "Log"), 
    Card(image: UIImage(named: "look")!, soundUrl: "Look"), 
    Card(image: UIImage(named: "love")!, soundUrl: "Love"), 
    Card(image: UIImage(named: "lunch")!, soundUrl: "Lunch") 
     ] 

let secondList:[Card] = [ 
    Card(image: UIImage(named: "lake")!, soundUrl: "Lake"), 
    Card(image: UIImage(named: "lamb")!, soundUrl: "Lamb"), 
    Card(image: UIImage(named: "lamp")!, soundUrl: "Lamp"), 
    Card(image: UIImage(named: "lark")!, soundUrl: "Lark"), 
    Card(image: UIImage(named: "leaf")!, soundUrl: "Leaf"), 

    ] 

struct List { 
    let words: [Card] /*Create array of cards*/ 
    var active: Bool 
} 

let list1 = List(words:firstList, active: true) 
let list2 = List(words:secondList, active: true) 

var imageList: [String]{ 
let wordLists = [list1, list2] 

print((wordLists[0] as! List).words[0].soundurl) 



    override func viewDidLoad() { 
    super.viewDidLoad() 



    let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(imageTapped(tapGestureRecognizer:))) 
    imgPhoto.isUserInteractionEnabled = true 
    imgPhoto.addGestureRecognizer(tapGestureRecognizer) 

    imgPhoto.image = (imageList[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) 

} 

@IBAction func imageTapped(tapGestureRecognizer: UITapGestureRecognizer) 
{ 
    print("hhh") 
    imageList[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 = words.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. 
     } 
    } 
} 
} 
+0

同じ質問を二度聞かないでください。 – matt

答えて

0

あなたはバグの内容を詳しく説明していません。しかし、貼り付けたコードに表示されているところから、主な変更点はサウンドファイルとisUserInteractionEnabledを有効にしたことです。

import AVFoundationも置くことを忘れないでください):それは完璧に動作している -

私は、playSoundの実装が表示されていないが、私はここで私はサウンドを再生する必要がある時はいつでも、私はしばしばSwift3に使用このスニペットを添付します

var audioPlayer : AVAudioPlayer? 

func playSound() 
{ 
    let fileURL = Bundle.main.path(forResource: "beep-01a", ofType:"mp3")! 

    let url = URL(fileURLWithPath: fileURL) 

    do 
    { 
     let player = try AVAudioPlayer(contentsOf: url) 
     self.audioPlayer = player 
     player.prepareToPlay() 
     player.play() 
    } 
    catch let error as NSError 
    { 
     print(error.description) 
    } 
} 

それはまだスムーズに実行していない場合は、あなたがタップでそれを呼び出したときに、バックグラウンドでのスレッドでplaySoundを入れて試してみてください。DispatchQueue.global()非同期{self.playSound()})

それはだ場合結局のところ音ではない - バグに関するデータを提供する..

幸運

+0

私の謝罪、おそらくもっと具体的であったはずです。サウンドの再生には問題ありませんが、画像配列には問題があります。最初のコードブロックを見ると、最終的な配列を1つ追加する11の異なるイメージ配列があることがわかります。私の新しいコードを見れば、私は2つの配列を持っていて、最初のコードブロックのように1つの配列としてコンパイルしたいと思っています –

+0

私はここに問題があると考えます: 'let wordLists = [list1、list2]' 。これにより、2つの配列項目の配列が生成され、それらの両方からの結合された配列の配列は生成されません。最初のブロックで 'reduce'を実行するか、' flatMap'を使って配列を結合してください。 –

+0

問題は、私の新しい構造では、各リストをどのように "アクティブ"にするかわからないので、減らすことができます。どのように私は各リストをアクティブにすることができますか考えていますか? –

関連する問題