概要 - 私はフラッシュカードアプリケーションを作成しています。ユーザーが画像の配列を左右にスワイプできるポイントに達しました。画像は11の異なるグループに分割され、すべてのグループはユーザーがスワイプ(下のコード)できる最後の配列に追加されます。別のビューコントローラで画像アレイを有効または無効にするUIスイッチ
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.
}
}
}
}
NOW - 設定ページ(下の画像)で作業しています。上記のコードでは、11個の単語リストごとに1つのスイッチがあります。一番上のスイッチは
...第二のスイッチはLIST2などを制御するだろう、問題をリスト1を制御するだろう - 私は、各スイッチに機能を追加したいということです。スイッチがオフの位置にあるとき、関連付けられている画像のグループは最終的なアレイに含まれてはならず、ユーザーがフラッシュカードをスワイプしても表示されません。これまでの設定ページのコードは次のとおりです。私は、ViewControllerにスイッチを接続してOverride関数を追加するなど、さまざまなコードを試してみましたが、この時点でどこに行くのかはわかりません。
import UIKit
protocol WordSelectionDelegate: class {
func wordSelected(newWord: Word)
}
class MasterViewController: UITableViewController {
var words = [Word]()
weak var delegate: WordSelectionDelegate?
override func prepare(for segue: UIStoryboardSegue, sender: SecondViewController) {
if let vc = segue.destination as? MasterViewController {
vc.wordLists = wordLists
}
}
@IBAction func switchAction(_ sender: UISwitch) {
wordList[sender.tag].active = rollIntoLoanSwitch.isOn
}
override func viewDidLoad() {
super.viewDidLoad()
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
self.words.append(Word(name: "initial /l/ 1 syllable", description: "lake lamb lamp lark leaf leash left leg lime lion lips list lock log look love lunch"))
self.words.append(Word(name: "initial /l/ multisyllabic", description: ""))
self.words.append(Word(name: "intersyllabic /l/", description: ""))
self.words.append(Word(name: "final /l/", description: ""))
self.words.append(Word(name: "initial /pl/", description: ""))
self.words.append(Word(name: "initial /bl/", description: ""))
self.words.append(Word(name: "initial /fl/", description: ""))
self.words.append(Word(name: "initial /gl/", description: ""))
self.words.append(Word(name: "initial /kl/", description: ""))
self.words.append(Word(name: "initial /sl/", description: ""))
self.words.append(Word(name: "final /l/ clusters", description: ""))
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return self.words.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
// Configure the cell...
let word = self.words[indexPath.row]
cell.textLabel?.text = word.name
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt
indexPath: IndexPath) {
let selectedMonster = self.words[indexPath.row]
self.delegate?.wordSelected(newWord: selectedMonster)
if let Detail = self.delegate as? Detail {
splitViewController?.showDetailViewController(Detail, sender: nil)
}
}
/*
// Override to support conditional editing of the table view.
override func tableView(_ tableView: UITableView, can
この問題を解決する方法については、どのような考えがあれば幸いです。あなたがこの作品を作るためにあなたのプログラムへの部品の数が不足しているあなたに
UISwitchのデリゲート/アクションターゲットがコード内に設定されていないことがわかりました。そうしないと、 'switchAction'メソッドは呼び出されません。あなたはコード内の他の場所でそれをしましたか?そのメソッドにブレークポイントを置くと、それは今まで呼ばれていますか? – Mrwerdo
私はそれを設定していないと思います。設定ページはスプリットビューコントローラなので、スプリットビューに関連付けられている他のクラスがありますが、上記以外のクラスにスイッチアクションメソッドを配置していません。 –