为什么单击按钮时这些框的颜色不会改变? (Swift)
Why won't the colors to these boxes change when button is clicked? (Swift)
所以我有一个名为 "cell" 的 UIView,它在 for 循环中重复,我设置了 func makeStuffHappen() ,它在按下 UIButton 时执行。该函数应该是将颜色更改为 UIView。
我做错了什么?
import UIKit
import PlaygroundSupport
// STEP 1: consts and vars
let view = UIView(frame: CGRect(x: 0, y: 0, width: 500, height: 600))
let colorButton = UIButton(frame: CGRect(x: 460, y: 560, width: 30, height: 30))
let numberOfCellsOnAxis = 6
let cellDimensions = view.frame.height / CGFloat(numberOfCellsOnAxis)
var cell = UIView()
func randomColor() -> UIColor {
let red = CGFloat(arc4random_uniform(256))
let green = CGFloat(arc4random_uniform(256))
let blue = CGFloat(arc4random_uniform(256))
return UIColor(red: red / 255, green: green / 255, blue: blue / 255, alpha: 1)
}
class takeAction : NSObject {
var tapCount = 0
@objc func makeStuffHappen() {
cell.backgroundColor = randomColor()
tapCount += 1
print("gesture recognized, colors have changed \(tapCount) times!")
}
}
let TakeAction = takeAction()
colorButton.addTarget( TakeAction, action: #selector(takeAction.makeStuffHappen), for: .touchUpInside)
// STEP 2: Setup repeating boxes on both x and y axis within view
for xAxis in 0...(numberOfCellsOnAxis - 1) {
for yAxis in 0...(numberOfCellsOnAxis - 1) {
cell = UIView()
cell.frame = CGRect(x: CGFloat(xAxis) * cellDimensions, y: CGFloat(yAxis) * cellDimensions, width: cellDimensions, height: cellDimensions)
cell.backgroundColor = randomColor()
view.addSubview(cell)
}
}
colorButton.backgroundColor = .magenta
view.backgroundColor = randomColor()
view.addSubview(colorButton)
PlaygroundPage.current.liveView = view
image of UIView in for loop
问题是您重复使用了 cell
变量,所以最后它只引用了您创建的最后一个视图。因此,您的按钮操作只会更新最后一个视图。
您需要一个包含所有视图的数组。然后在按钮处理程序中,遍历它们并给每个新的随机颜色。
import UIKit
import PlaygroundSupport
let view = UIView(frame: CGRect(x: 0, y: 0, width: 500, height: 600))
let colorButton = UIButton(frame: CGRect(x: 460, y: 560, width: 30, height: 30))
let numberOfCellsOnAxis = 6
let cellDimensions = view.frame.height / CGFloat(numberOfCellsOnAxis)
var cells = [UIView]()
func randomColor() -> UIColor {
let red = CGFloat(arc4random_uniform(256))
let green = CGFloat(arc4random_uniform(256))
let blue = CGFloat(arc4random_uniform(256))
return UIColor(red: red / 255, green: green / 255, blue: blue / 255, alpha: 1)
}
class TakeAction {
var tapCount = 0
@objc func makeStuffHappen() {
cells.forEach { [=10=].backgroundColor = randomColor() }
tapCount += 1
print("gesture recognized, colors have changed \(tapCount) times!")
}
}
let takeAction = TakeAction()
colorButton.addTarget(takeAction, action: #selector(takeAction.makeStuffHappen), for: .touchUpInside)
for xAxis in 0..<numberOfCellsOnAxis {
for yAxis in 0..<numberOfCellsOnAxis {
let cell = UIView(frame: CGRect(x: CGFloat(xAxis) * cellDimensions, y: CGFloat(yAxis) * cellDimensions, width: cellDimensions, height: cellDimensions))
cell.backgroundColor = randomColor()
view.addSubview(cell)
cells.append(cell)
}
}
colorButton.backgroundColor = .magenta
view.backgroundColor = randomColor()
view.addSubview(colorButton)
PlaygroundPage.current.liveView = view
所以我有一个名为 "cell" 的 UIView,它在 for 循环中重复,我设置了 func makeStuffHappen() ,它在按下 UIButton 时执行。该函数应该是将颜色更改为 UIView。
我做错了什么?
import UIKit
import PlaygroundSupport
// STEP 1: consts and vars
let view = UIView(frame: CGRect(x: 0, y: 0, width: 500, height: 600))
let colorButton = UIButton(frame: CGRect(x: 460, y: 560, width: 30, height: 30))
let numberOfCellsOnAxis = 6
let cellDimensions = view.frame.height / CGFloat(numberOfCellsOnAxis)
var cell = UIView()
func randomColor() -> UIColor {
let red = CGFloat(arc4random_uniform(256))
let green = CGFloat(arc4random_uniform(256))
let blue = CGFloat(arc4random_uniform(256))
return UIColor(red: red / 255, green: green / 255, blue: blue / 255, alpha: 1)
}
class takeAction : NSObject {
var tapCount = 0
@objc func makeStuffHappen() {
cell.backgroundColor = randomColor()
tapCount += 1
print("gesture recognized, colors have changed \(tapCount) times!")
}
}
let TakeAction = takeAction()
colorButton.addTarget( TakeAction, action: #selector(takeAction.makeStuffHappen), for: .touchUpInside)
// STEP 2: Setup repeating boxes on both x and y axis within view
for xAxis in 0...(numberOfCellsOnAxis - 1) {
for yAxis in 0...(numberOfCellsOnAxis - 1) {
cell = UIView()
cell.frame = CGRect(x: CGFloat(xAxis) * cellDimensions, y: CGFloat(yAxis) * cellDimensions, width: cellDimensions, height: cellDimensions)
cell.backgroundColor = randomColor()
view.addSubview(cell)
}
}
colorButton.backgroundColor = .magenta
view.backgroundColor = randomColor()
view.addSubview(colorButton)
PlaygroundPage.current.liveView = view
image of UIView in for loop
问题是您重复使用了 cell
变量,所以最后它只引用了您创建的最后一个视图。因此,您的按钮操作只会更新最后一个视图。
您需要一个包含所有视图的数组。然后在按钮处理程序中,遍历它们并给每个新的随机颜色。
import UIKit
import PlaygroundSupport
let view = UIView(frame: CGRect(x: 0, y: 0, width: 500, height: 600))
let colorButton = UIButton(frame: CGRect(x: 460, y: 560, width: 30, height: 30))
let numberOfCellsOnAxis = 6
let cellDimensions = view.frame.height / CGFloat(numberOfCellsOnAxis)
var cells = [UIView]()
func randomColor() -> UIColor {
let red = CGFloat(arc4random_uniform(256))
let green = CGFloat(arc4random_uniform(256))
let blue = CGFloat(arc4random_uniform(256))
return UIColor(red: red / 255, green: green / 255, blue: blue / 255, alpha: 1)
}
class TakeAction {
var tapCount = 0
@objc func makeStuffHappen() {
cells.forEach { [=10=].backgroundColor = randomColor() }
tapCount += 1
print("gesture recognized, colors have changed \(tapCount) times!")
}
}
let takeAction = TakeAction()
colorButton.addTarget(takeAction, action: #selector(takeAction.makeStuffHappen), for: .touchUpInside)
for xAxis in 0..<numberOfCellsOnAxis {
for yAxis in 0..<numberOfCellsOnAxis {
let cell = UIView(frame: CGRect(x: CGFloat(xAxis) * cellDimensions, y: CGFloat(yAxis) * cellDimensions, width: cellDimensions, height: cellDimensions))
cell.backgroundColor = randomColor()
view.addSubview(cell)
cells.append(cell)
}
}
colorButton.backgroundColor = .magenta
view.backgroundColor = randomColor()
view.addSubview(colorButton)
PlaygroundPage.current.liveView = view