gpt4 book ai didi

ios - 设置页面添加或减去不同的图像阵列。使用 swift 和 xcode 进行编码

转载 作者:行者123 更新时间:2023-11-30 12:20:07 25 4
gpt4 key购买 nike

我正在尝试制作一个简单的抽认卡应用程序。

概述 - 为了让您了解应用程序的基本运行情况,所有抽认卡都包含一个带有字母 l 的对象。有时单词以字母 l 开头,有时有两个 ll,有时单词以 pl 或 kl,或 sl 开头,等等...

到目前为止 - 我已经制作了应用程序,以便有一个主页,从主页上按“Go”,然后它也会带你第二个 View Controller 。从那里您可以左右滑动闪存卡。我通过创建一个包含所有图像的数组,然后添加一个滑动手势(下面的代码)来做到这一点

//
// SecondViewController.swift
// firstapp
//
// Created by Anthony Rubin on 6/20/17.
// Copyright © 2017 rubin. All rights reserved.
//

import UIKit

class SecondViewController: UIViewController , UIGestureRecognizerDelegate {



@IBAction func home(_ sender: Any) {
performSegue(withIdentifier: "home", sender: self)
}


@IBOutlet weak var imgPhoto: UIImageView!





var imageList:[String] = ["alligator", "apple", "balance", "ball", "ballerina", "balloon", "bell", "belt", "black", "blanket", "blender", "blocks", "blond", "blood", "blow", "blue", "bowling", "bubble", "bully", "calendar", "castle", "cello", "clam", "clamp", "clap", "claw", "clean", "climb", "clip", "cloud", "cold", "colors", "crawl", "curlyhair", "dollar", "dolphin", "elephant", "elf", "eyelashes", "fall", "fishbowl", "flag", "flipflop", "float", "floor", "flower", "fluffy", "flute", "fly", "gasoline", "girl", "glacier", "glad", "glasses", "glide", "glitter", "globe", "glove", "glue", "goalie", "golf", "hula", "jellyfish", "ladder", "ladybug", "lake", "lamb", "lamp", "lark", "laughing", "lawnmower", "leaf", "leash", "left", "leg", "lemon", "leopard", "leprechaun", "letters", "licking", "lifesaver", "lifting", "lightbulb", "lightning", "lime", "lion", "lips", "list", "listen", "llama", "lock", "log", "look", "love", "lunch", "melt", "milk", "olive", "owl", "pail", "peel", "pillow", "pilot", "planet", "plank", "plant", "plate", "play", "plum", "plumber", "plus", "polarbear", "pool", "rollerskate", "ruler", "shelf", "silly", "sled", "sleep", "sleeves", "slice", "slide", "slime", "slip", "slow", "smile", "telephone", "television", "tulip", "umbrella", "valentine", "violin", "whale", "wheel", "xylophone", "yellow"]
let maxImages = 135
var imageIndex: NSInteger = 0


override func viewDidLoad() {
super.viewDidLoad()

// 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 = maxImages

}

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 > maxImages {

imageIndex = 0

}

imgPhoto.image = UIImage(named: imageList[imageIndex])




default:
break //stops the code/codes nothing.


}
}
}










override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}


/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/

问题 - 我想创建一个设置页面。此设置页面将为每组单词提供基本的开关。因此,假设我不想要任何首字母为 l 的单词,我会将按钮转到关闭位置,然后当我滑动图像时,将不会出现首字母为 l 的抽认卡。

settings page

我的想法 - 我想我必须将每个开关与其相应的单词数组连接起来,然后为每个按钮编写 if、then 语句。然后将所有数组加在一起。但是我完全不确定如何开始这样做。我已经制作了一个包含所有不同开关的表格 View ,但尚未添加任何功能。我也不确定如何将信息从 TableView 发送到第二个 View Controller 。

我知道这个问题有很多问题要问,但如果有任何帮助,我们将不胜感激。谢谢

最佳答案

这里有很多内容,但您似乎已经有了一个良好的开始。

如果单词和类别相当固定,那么我要做的第一件事就是将单词列表分成不同的类别,然后您可以使用以下方法组合您想要的图像:

let list1 = ["Bat", "Cow"]
let list2 = ["Frog", "Rabbit"]
let list3 = ["Parrot", "Tiger"]

var imageList: [String] {
return list1+list2
}

您可以保留一个 activeList 数组,然后使用 reduce 函数返回最终数组:

var activeLists = [list1, list2]
var imageList: [String] {
return activeLists.reduce([], {$0 + $1})
}

但是您最终可能需要一个比简单的字符串数组更易于管理的数据源来存储信息:

struct List {
let words: [String]
var active: Bool
}

然后你可以:

let list1 =  List(words: ["Bat", "Cow"], active: true)
let list2 = List(words: ["Frog", "Rabbit"], active: true)
let list3 = List(words: ["Parrot", "Tiger"], active: true)
var wordLists = [list1, list2, list3]

var imageList: [String] {

let active = wordLists.reduce([]) { (result:[String], list:List) in
if list.active {
return result + list.words
} else {
return result
}
}

return active

}

注意:我不会使用 maxImages 作为常量,因为它需要更改,您可以使用 imageList.count 代替。

关于发送信息,有几种方法可以实现。一种方法是使用prepareForSegue将信息发送到新的viewController。像这样的事情:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let vc = segue.destination as? SettingsViewController {
vc.wordLists = wordLists
}
}

然后在“设置”中,您可以使用以下命令切换各个单词列表:

wordLists[1].active = false

将开关与数组引用相匹配的一种简单方法是在 Storyboard 中为每个 UISwitch 提供一个与其在 wordLists 数组中的索引相匹配的标签,将所有 UISwitch Hook 到同一个 IBAction,然后在触发时使用:

@IBAction func switchAction(_ sender: UISwitch) {
wordList[sender.tag].active = rollIntoLoanSwitch.isOn
}

然后,您可以使用委托(delegate)在每次切换或离开 View 时发回信息。或者,您可以在单独的类中使用单例定义单词列表,并从任何地方引用它,这取决于您需要访问单词列表的不同 View 的数量。

关于ios - 设置页面添加或减去不同的图像阵列。使用 swift 和 xcode 进行编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44896038/

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