- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在开发一个非常简单的快速游戏。游戏的主要布局是具有 16 个单元格(门)的 UICollectionView。其中 1 个单元格(门)是一个游戏结束单元格。除了 game over cell 之外,您还需要点击(打开)所有门。如果一个 game over cell 被点击,细胞将全部随机化,你会失去一条生命。如果你打开除了 game over cell 之外的所有门,你就赢了。
我的代码如下。
什么有效?
什么不起作用?
如果我点击一些普通的门(以禁用它们),然后点击游戏在门上,它会左右移动门(应该如此),但随后其他随机门被禁用并看起来启用(通过 1.0 alpha)。不打算。
如果我点击一些普通的门(以禁用它们),然后点击 Game Over 门,所有门都变为 1.0 alpha,只有一些有启用交互。此外,按下游戏结束门后,有时只有一扇门会保持 0.2 alpha。
我需要什么?当一扇普通的门被敲击时,该单元格现在将始终被禁用。如果门上的游戏被点击,它将忽略已经被点击的单元格,因为它们应该“退出游戏”。
import UIKit
class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
@IBOutlet weak var mainCollectionView: UICollectionView!
var gameOverDoorArray = ["GameOverIdentifier"]
var normalDoors = ["DoorIdentifier", "DoorIdentifier", "DoorIdentifier", "DoorIdentifier", "DoorIdentifier", "DoorIdentifier", "DoorIdentifier", "DoorIdentifier", "DoorIdentifier", "DoorIdentifier", "DoorIdentifier", "DoorIdentifier", "DoorIdentifier", "DoorIdentifier", "DoorIdentifier"]
var reuseIdentifiers = [String]()
var livesLeftCounter = 3
@IBAction func randomizeButton(sender: UIButton) {
randomizeReuseIdentifiers()
}
override func viewDidLoad() {
super.viewDidLoad()
mainCollectionView.dataSource = self
mainCollectionView.delegate = self
mainCollectionView.backgroundColor = UIColor.clearColor()
reuseIdentifiers = gameOverDoorArray + normalDoors
randomizeReuseIdentifiers()
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return reuseIdentifiers.count
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let identifier = reuseIdentifiers[indexPath.item]
let cell: DoorCollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier(identifier, forIndexPath: indexPath) as! DoorCollectionViewCell
cell.backgroundColor = UIColor(patternImage: UIImage(named: "doorClosedImage")!)
return cell
}
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
let currentCell = mainCollectionView.cellForItemAtIndexPath(indexPath) as UICollectionViewCell!
// Game Over door was pressed!
if reuseIdentifiers[indexPath.item] == "GameOverIdentifier" {
gameOverDetection()
randomizeReuseIdentifiers()
mainCollectionView.reloadData()
}
// Normal door was pressed!
else if reuseIdentifiers[indexPath.item] == "DoorIdentifier"{
if currentCell.userInteractionEnabled && (normalDoors.count > 0){
currentCell.alpha = 0.2
print("reuse ID's array - \(reuseIdentifiers)")
// when all the above is done, the button gets disabled.
currentCell.userInteractionEnabled = false
// Remove last item in normalDoors Array.
normalDoors.removeLast()
}
print("Array count of allTheDoors - \(reuseIdentifiers.count).")
}
}
func randomizeReuseIdentifiers() {
var randomized = [String]()
for _ in reuseIdentifiers {
let random = reuseIdentifiers.removeAtIndex(Int(arc4random_uniform(UInt32(reuseIdentifiers.count))))
randomized.append(random)
}
reuseIdentifiers = randomized
}
func gameOverDetection() {
livesLeftCounter -= 1
if livesLeftCounter < 0 {
print("GAME OVER!")
}
print(livesLeftCounter)
}
}
最佳答案
使用 UICollectionView
时(以及通常使用 MVC 时)最好将您要使用的所有数据与 View 分开存储。每扇门都有一些游戏状态属性,您当前正尝试使用 UICollectionViewCell
上的属性来存储这些属性。这是不正确的。您应该创建一个模型(我们称它为 Door
),并使用该模型的属性来存储您的游戏状态。
首先,创建一个带有嵌套枚举的类来存储门的类型。
class Door {
enum Type {
case Normal
case GameOver
}
var type: Type
init(type: Type) {
self.type = type
}
}
然后,您需要存储每个 Door
的打开/关闭状态:
class Door {
enum Type {
case Normal
case GameOver
}
var type: Type
var closed = true
init(type: Type) {
self.type = type
}
}
最后,您应该编写一个方法或计算属性来为每种门
返回适当的重用标识符。
class Door {
enum Type {
case Normal
case GameOver
}
var type: Type
var closed = true
var reuseIdentifier: String {
switch type {
case .Normal:
return "DoorIdentifier"
case .GameOver:
return "GameOverIdentifier"
default:
return ""
}
init(type: Type) {
self.type = type
}
}
然后您将初始化模型对象数组,而不是初始化reuseIdentifiers
数组。
var doors = [Door]()
override func viewDidLoad() {
super.viewDidLoad()
// Your other initialization in viewDidLoad here...
doors = [Door(.Normal), Door(.Normal), /* etc... */ Door(.GameOver)]
}
然后您需要重写 randomizeReuseIdentifiers
以随机化 doors
。
func randomizeDoors() {
var randomized = [Door]()
for _ in doors {
let random = doors.removeAtIndex(Int(arc4random_uniform(UInt32(doors.count))))
randomized.append(random)
}
doors = randomized
}
最后,您可以在 UICollectionViewDataSource
和 UICollectionViewDelegate
方法中使用这个新的数据模型:
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return reuseIdentifiers.count
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let door = doors[indexPath.item]
let identifier = door.reuseIdentifier
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(identifier, forIndexPath: indexPath) as! DoorCollectionViewCell
// Here, use the closed property to set interaction enabled and alpha.
if door.closed {
cell.userInteractionEnabled = true
cell.alpha = 1.0
} else {
cell.userInteractionEnabled = false
cell.alpha = 0.2
}
cell.backgroundColor = UIColor(patternImage: UIImage(named: "doorClosedImage")!)
return cell
}
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
let currentCell = mainCollectionView.cellForItemAtIndexPath(indexPath) as UICollectionViewCell!
let door = doors[indexPath.item]
switch door.type {
case .Normal:
// Set the door to opened, so it is disabled on the next reload.
door.closed = false
case .GameOver:
gameOverDetection()
randomizeDoors()
}
mainCollectionView.reloadData()
}
总的来说,我认为在您继续学习的过程中阅读更多关于 Model View Controller (MVC) 模式的内容会对您有所帮助。苹果有some documentation that might be helpful , 你可能会发现 these resources也是说明性的。
关于ios - 点击时禁用 UICollectionViewCell,并随机化单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36845527/
堆栈:Mac OS 10.15.6,Xcode 版本 12.0 (12A7209) 我有一个自行调整大小的收集单元。我覆盖 preferredLayoutAttributesFitting获得自我调整
我有一个 Storyboard,它是在 Xcode 6 中创建的,带有 Collection View ,并且单元格具有不同的大小类变化,例如不同的字体大小......( Storyboard中创建的
我有一个 UICollectionView,只有 1 个部分,该部分中有许多单元格。我启用了拖放行为(使用长按手势识别器,因为 UICollectionView 位于通用 UIViewControll
按照我的老问题here : How to add UIView inside a UITableViewCell Programmatically? 我想通过在这些 collectionViewCel
我将 UICollectionViewCell 与父 UICollectionViewCell 中的按钮一起使用: protocol DayCellDelegate { func button
例如,我有 10 个单元格的 UICollectionView。我想为选定的单元格添加边框,稍后,在选择另一个单元格时,想要删除以前的边框并为新的选定单元格添加边框。 我怎样才能做到这一点? 我已经试
我有一个 UICollectionViewCell(比如 UICollectionViewCell 1),单元格内有一个按钮和 UITextfields。连接到 (ViewController 1)
我在单个 UICollectionViewCell 内有 (3) UITextViews、(1) UITextField 和 (1) UILabel >。当文本换行到第二行时,在符合 UITextVi
我对 TableVieCell 中的 collectionView 有疑问。当我点击 collectionCell 时,didSelectItemAt 不会被调用。我在 collectionCell
我最近在我的 Storyboard中添加了一个 UICollectionView,它当前被另一个 View 插入 View ,这似乎工作正常但是,使用 Storyboard编辑器我将 View 设置为
我想在 UICollectionViewCell 上开始一些动画当用户点击一个单元格时。我的想法是在 didSelectItemAtIndexPath 中选择相应的单元格并触发动画。但是,这不起作用:
我可以通过使用手势识别器拖动它并简化新的 iOS 9 对重新排序的支持来重新排序 iOS 9 上的 UICollectionViewCells。 public func beginInteractiv
我正在开发一个应用程序,其中我在 UICollectionView 中实现了编辑模式。我正在尝试实现如下所示的内容: 当我单击编辑按钮时,相机按钮和眼睛按钮应隐藏,删除和重命名按钮应如下所示: 编辑后
我希望所有单元格之间以及屏幕边框和最近的单元格之间的间距相等。基本上是这样的。内部和周围的间距相等。 我设法通过设置这样的值在界面生成器中测试这一点。我将单元格大小设置为 110,最小间距设置为 0,
我已经设置了一个 UICollectionView,如下所示。我正在尝试设置自定义单元格高度,以便该集合在必须滚动之前适合屏幕上的 3 个单元格。但是,这种定制工作不起作用或似乎被忽略了。有谁知道我做
我在不使用 Storyboard的情况下以编程方式创建了 UICollectionView 和 UICollectionViewCell,但是发生了一些事情,我无法理解为什么。 BKPhotoCell
我正在寻找制作图片库。我希望我的图像全屏显示,但不确定这如何与自动布局一起使用。我知道有一种方法可以返回单元格的大小,但不知道我是否应该只获取屏幕大小并返回它,或者是否有使用自动布局的方法。 最佳答案
我有一个问题, 我有一个简单的 UICollectionView,其中包含从 Flickr 加载图像的静态 200 个单元格。 我的 CellForItemAtIndexPath 看起来像这样: -
以下代码是一个非常简单的程序,用于在 UICollectionView 中显示 1-10000 之间的数字。 .它在不滚动的情况下正确显示,但是如果您向下滚动并向后滚动 Collection View
这个问题在这里已经有了答案: UICollectionViewLayout Not working with UIImage in Swift 5 and Xcode 11 (2 个答案) 关闭 2
我是一名优秀的程序员,十分优秀!