Created
October 23, 2014 23:53
-
-
Save abhibeckert/226e5c4b2a5fb2c725b8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func updateKeys() | |
{ | |
CATransaction.begin() | |
CATransaction.setDisableActions(true); | |
var x: CGFloat = 0 | |
var y: CGFloat = 0 | |
var rowHeight = ceil(self.inputView.frame.size.height / 5) | |
var colWidth = ceil(self.inputView.frame.size.width / 10) | |
var rowIndex = 0 | |
var colIndex = 0 | |
for row in self.keyLayers { | |
for keyLayer in row { | |
var cellWidth = colWidth | |
let title = self.activeKeyTitles[rowIndex][colIndex] | |
switch title { | |
case "←", "↵", "⌥", "⚙", "⊙": | |
cellWidth *= 1.5 | |
cellWidth = ceil(cellWidth) | |
case " ": | |
cellWidth *= 4 | |
default: | |
break | |
} | |
var active = false | |
if title == "↑" && self.shiftStateActive { | |
active = true | |
} else if title == "⌥" && self.altStateActive { | |
active = true | |
} else { | |
for highlightedKey in self.highlightedKeys { | |
if highlightedKey.key == title { | |
active = true | |
break | |
} | |
} | |
} | |
if ((fabs(keyLayer.frame.size.width - cellWidth) > 0.1) || keyLayer.active != active || keyLayer.key != title) { | |
var h = rowHeight | |
if (y + h > self.inputView.frame.size.height) { | |
h = self.inputView.frame.size.height - y | |
} | |
var w = cellWidth | |
if (x + w > self.inputView.frame.size.width) { | |
w = self.inputView.frame.size.width - x | |
} | |
keyLayer.frame = CGRect(x: x, y: y, width: w, height: h) | |
keyLayer.key = title | |
keyLayer.active = active | |
keyLayer.setNeedsDisplay() | |
} | |
x += cellWidth | |
colIndex++ | |
} | |
y += rowHeight | |
x = 0 | |
colIndex = 0 | |
rowIndex++ | |
} | |
CATransaction.commit() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment