我的ViewController中有一个名为Keyboard的UIView
类来创建可自定义的钢琴键盘图:
class Key: UIView {
var keyType = 0
var keyTypeArray = ["A", "Bb", "B", "C", "Db", "D", "Eb", "E", "F", "Gb", "G", "Ab"]
// error key type: 0
// white key types: 1, 3, 4, 6, 8, 9, 11
// black key types: 2, 5, 7, 10, 12
init() {
super.init(frame: CGRect())
self.backgroundColor = .white
self.layer.borderWidth = 1
self.clipsToBounds = false
self.layer.shadowOffset = CGSize(width: 5, height: 5)
self.layer.shadowOpacity = 1.0
self.layer.shadowRadius = 5
self.layer.shadowColor = UIColor(red: 44.0/255.0, green: 62.0/255.0, blue: 80.0/255.0, alpha: 1.0).cgColor
self.layer.maskedCorners = [.layerMinXMaxYCorner, .layerMaxXMaxYCorner]
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
class Keyboard: UIView {
let initialKey: Int
let numberOfKeys: Int
var scale: Double
var keys = [Key]()
var numWhiteKeys = 0
var numBlackKeys = 0
var startingX: CGFloat = 100.0
var startingY: CGFloat = 200.0
var zPos: CGFloat
var keyboardDimensions = [23.0, 24.0, 14.0, 91.0, 52.0]
// narrowWhiteKeyWidth = 23.0
// wideWhiteKeyWidth = 24.0
// blackKeyWidth = 14.0
// whiteKeyHeight = 91.0
// blackKeyHeight = 52.0
var blackOffsetsArray = [20.0, 38.0, 14.0, 19.0, 36.0, 13.0, 17.0]
var whiteOffsetsArray = [3.0, 9.0, 5.0, 11.0, 7.0]
var nextBlackKeyPos = [41.0, 28.0, 41.0, 27.0, 27.0]
init(startingX: CGFloat, startingY: CGFloat, initialKey: Int, numberOfKeys: Int, scale: Double, zPos: CGFloat) {
self.startingX = startingX
self.startingY = startingY
self.initialKey = initialKey
self.numberOfKeys = numberOfKeys
self.scale = scale
self.zPos = zPos
super.init(frame: CGRect())
}
@discardableResult
func addKeys() -> [UIView] {
for (index, _) in blackOffsetsArray.enumerated() {
blackOffsetsArray[index] *= scale
}
for (index, _) in whiteOffsetsArray.enumerated() {
whiteOffsetsArray[index] *= scale
}
for (index, _) in nextBlackKeyPos.enumerated() {
nextBlackKeyPos[index] *= scale
}
for (index, _) in keyboardDimensions.enumerated() {
keyboardDimensions[index] *= scale
}
var counter = initialKey
var whiteKeys = [Key]()
var blackKeys = [Key]()
var wXPos = CGFloat()
var bXPos = CGFloat()
let blackOffsetsDict = [1: 0, 3: 1, 4: 2, 6: 3, 8: 4, 9: 5, 11: 6]
let whiteOffsetsDict = [2: 0, 5: 1, 7: 2, 10: 3, 12: 4]
switch initialKey {
case 1, 3, 4, 6, 8, 9, 11:
wXPos = startingX
if let offsetNum = blackOffsetsDict[initialKey] {
bXPos = startingX + CGFloat(blackOffsetsArray[offsetNum])
}
case 2, 5, 7, 10, 12:
bXPos = startingX
if let offsetNum = whiteOffsetsDict[initialKey] {
wXPos = startingX + CGFloat(whiteOffsetsArray[offsetNum])
}
default:
print("Error!")
}
while counter <= (numberOfKeys + initialKey - 1) {
var keyNum = Int()
let timesIn = Int(counter/12)
let key = Key()
key.layer.shadowOffset = CGSize(width: 5*CGFloat(scale), height: 5*CGFloat(scale))
key.layer.shadowRadius = 5*CGFloat(scale)
if counter <= 12 {
keyNum = counter
} else if counter > 12 && (counter % 12) != 0 {
keyNum = counter - (12 * (timesIn))
} else {
keyNum = counter - (12 * (timesIn - 1))
}
switch keyNum {
case 1, 3, 4, 6, 8, 9, 11:
keys.append(key)
whiteKeys.append(key)
self.numWhiteKeys += 1
key.backgroundColor = UIColor.white
key.layer.cornerRadius = 5*CGFloat(scale)
key.layer.borderColor = UIColor.black.cgColor
key.layer.zPosition = zPos
// print("keyNum is \(keyNum), counter is \(counter)")
switch keyNum {
case 1, 4, 8, 11:
key.frame = CGRect(x: Double(wXPos), y: Double(startingY), width: keyboardDimensions[0], height: keyboardDimensions[3])
switch keyNum {
case 1:
// print(" A")
key.keyType = 1
case 4:
// print(" C")
key.keyType = 4
case 8:
// print(" E")
key.keyType = 8
case 11:
// print(" G")
key.keyType = 11
default:
key.keyType = 0
print(" Error!")
}
case 3, 6, 9:
key.frame = CGRect(x: Double(wXPos), y: Double(startingY), width: keyboardDimensions[1], height: keyboardDimensions[3])
switch keyNum {
case 3:
// print(" B")
key.keyType = 3
case 6:
// print(" D")
key.keyType = 6
case 9:
// print(" F")
key.keyType = 9
default:
key.keyType = 0
print(" Error!")
}
default:
key.keyType = 0
print(" Error!")
}
wXPos += key.frame.width
case 2, 5, 7, 10, 12:
self.numBlackKeys += 1
key.layer.zPosition = zPos + 1
key.layer.cornerRadius = 2.5*CGFloat(scale)
key.layer.borderColor = UIColor.black.cgColor
key.backgroundColor = UIColor.darkGray
key.layer.shadowOffset = CGSize(width: 2*CGFloat(scale), height: 2*CGFloat(scale))
key.layer.shadowRadius = 2*CGFloat(scale)
key.layer.shadowOpacity = 1.0
// print("keyNum is \(keyNum), counter is \(counter)")
key.frame = CGRect(x: Double(bXPos), y: Double(startingY), width: keyboardDimensions[2], height: keyboardDimensions[4])
switch keyNum {
case 2:
// print(" Bb")
key.keyType = 2
bXPos += CGFloat(nextBlackKeyPos[0])
case 5:
// print(" Db")
key.keyType = 5
bXPos += CGFloat(nextBlackKeyPos[1])
case 7:
// print(" Eb")
key.keyType = 7
bXPos += CGFloat(nextBlackKeyPos[2])
case 10:
// print(" Gb")
key.keyType = 10
bXPos += CGFloat(nextBlackKeyPos[3])
case 12:
// print(" Ab")
key.keyType = 12
bXPos += CGFloat(nextBlackKeyPos[4])
default:
key.keyType = 0
print(" Error!")
}
keys.append(key)
blackKeys.append(key)
default:
key.keyType = 0
print("Key # is \(keyNum). Error!")
}
counter += 1
}
return keys
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
然后我有一个addKeyboard
函数创建每个键盘并将其添加到keyboards
数组中:
var keyboards = [Keyboard]()
func addKeyboard(startingX: CGFloat, startingY: CGFloat, initialKey: Int, numberOfKeys: Int, scale: Double, zPos: CGFloat) {
let myKeyboard = Keyboard(startingX: startingX, startingY: startingY, initialKey: initialKey, numberOfKeys: numberOfKeys, scale: scale, zPos: zPos)
myKeyboard.addKeys()
myKeyboard.layer.zPosition = zPos
var i = 0
while i < myKeyboard.keys.count {
view.addSubview(myKeyboard.keys[i])
i += 1
}
keyboards.append(myKeyboard)
}
最后,在viewDidLoad中,我可以这样调用addKeyboard:
addKeyboard(startingX: 250, startingY: 30, initialKey: 9, numberOfKeys: 7, scale: 1.0, zPos: 2)
addKeyboard(startingX: 150, startingY: 100, initialKey: 4, numberOfKeys: 5, scale: 0.5, zPos: 2)
我的问题是当我检索我的键盘阵列并尝试更改任何键盘的值时。该数组返回一堆UIViews,帧数为(0 0; 0 0),我无法更改viewDidLoad中的任何参数。
此:
print(keyboards)
keyboards[5].scale = 2.0
print(keyboards[5].scale)
print(keyboards[5].frame)
得出这个:
[<_TtCC17drawKeyboardTest314ViewController8Keyboard: 0x7f9f9b2002e0; frame = (0 0; 0 0); layer = <CALayer: 0x60400003f4c0>>, <_TtCC17drawKeyboardTest314ViewController8Keyboard: 0x7f9f9b105390; frame = (0 0; 0 0); layer = <CALayer: 0x6000002225c0>>
等
并且print(keyboards[5].scale)
会产生2.0,但实际尺寸不会改变(上面最后2 print()
次调用的结果)。
2.0
(0.0, 0.0, 0.0, 0.0)
如何更改内容,以便在将键盘添加到键盘数组后,我可以根据需要获取正确的值并进行更改(例如,调整大小,移动,突出显示某些键)?
非常感谢。
杰克
答案 0 :(得分:1)
夫妻问题......
class Keyboard: UIView
,并且在该类中,您正在为一堆class Key: UIView
创建(并设置框架)。 但是,当您将它们添加为Keys
视图的子视图时,您要将这些Keyboard
添加为ViewController的主视图的子视图。然后,您会将keyboard
添加到主.view
。 (注意:您还需要设置键盘对象的框架以包含其键)。
.scale
属性时,您的代码没有执行任何操作。一个选项是添加&#34; resize()&#34;类型函数,可以直接调用它,也可以在更改scale属性时自动调用它。也许是这样:
var scale: Double {
didSet {
self.resize()
}
}
作为旁注,你可能会得到更好的结果 - 我希望从长远来看会更容易 - 使用自动调整大小和约束而不是明确的位置和大小的&#34;键&#34;