好的。這真的很奇怪。我正在嘗試將手機應用中的顏色傳輸到其伴侶手錶應用。從手機發送UIColor以奇怪的顏色觀看結果
標準(UIColor.red等)顏色工作,但由IB文件定義的顏色不工作。我得到奇怪的顏色,這好像是一個色彩空間問題。
First, here's a VERY simple (and absolutely HIDEOUS) app project that demonstrates this.
我在做什麼,在發送從監視的消息來了電話,詢問電話的一組顏色。
電話通過編譯UIColor實例列表並將它們發送到手錶來實現責任。它通過首先實例化三種標準顏色(UIColor.red,UIColor.green,UIColor.blue),然後附加一些從應用程序故事板中定義的標籤列表中讀取的顏色(它從標籤中讀取文本顏色)。
然後將由此產生的7個UIColor實例組成的數組序列化併發送到手錶,並將其反序列化,並創建一個簡單的標籤表;每個都有相應的顏色。
前三個顏色好看:
然而,當我讀到來自IB的顏色,他們得到傾斜。
這裏是什麼應該發生:
這裏到底發生了什麼:
只有最後一個(奇數)的顏色是正確的。顏色在故事板中被定義爲RGB(滑塊)。
Here's the code in the iOS app that gathers and sends the color:
func session(_ session: WCSession, didReceiveMessage message: [String : Any]) {
print("iOS App: session(_:,didReceiveMessage:)\n\(message)")
if let value = message["HI"] as? String {
if "HOWAYA" == value {
var colorArray: [UIColor] = [UIColor.red, UIColor.green, UIColor.blue]
if let primaryViewController = self.window?.rootViewController {
if let view = primaryViewController.view {
for subview in view.subviews {
if let label = subview as? UILabel {
if let color = label.textColor {
colorArray.append(color)
}
}
}
}
}
let colorData = NSKeyedArchiver.archivedData(withRootObject: colorArray)
let responseMessage = [value:colorData]
session.sendMessage(responseMessage, replyHandler: nil, errorHandler: nil)
}
}
}
Here's the code in the Watch that gets the colors, and instantiates the labels:
func session(_ session: WCSession, didReceiveMessage message: [String : Any]) {
if let value = message["HOWAYA"] as? Data {
if let colorArray = NSKeyedUnarchiver.unarchiveObject(with:value) as? [UIColor] {
self.labelTable.setNumberOfRows(colorArray.count, withRowType: "TestColorTableRow")
for row in 0..<colorArray.count {
if let controller = self.labelTable.rowController(at: row) as? TestTableRowController {
controller.setLabelColor(colorArray[row])
}
}
}
}
}
我送(在另一個應用程序),通過細變,但顏色偏其他數據。
任何想法?
更新:有人建議,也許序列化是一個問題,所以我分支演示,並且仍然在iOS中反序列化。事實證明,確定。
I created this branch,在那裏我發送消息之前反序列化:
func colorCrosscheck(_ value: Data) {
if let colorArray = NSKeyedUnarchiver.unarchiveObject(with:value) as? [UIColor] {
if let primaryViewController = self.window?.rootViewController {
if let view = primaryViewController.view {
for subview in view.subviews {
if let containerView = subview as? ViewList {
for row in 3..<colorArray.count {
if let label = containerView.subviews[row - 3] as? UILabel {
label.textColor = colorArray[row]
}
}
break
}
}
}
}
}
}
我所做的,是添加四個標籤清晰文本顏色,是由我的手錶上使用相同的過程着色。
手錶收到信息幾秒鐘後,iOS中的頂部下方會出現四個標籤。
結果表明序列化不是問題:
UPDATE(2):有人建議我嘗試以編程方式添加顏色。這些工作正常,這意味着這看起來像一個蘋果的錯誤。當我收到我的DTS事件迴應時,我會報告回來。
First, here's a new branch that demonstrates adding the colors dynamically.
Here's the relevant section of code(I還與IB文件弄亂):
colorArray.append(UIColor(red: 1.0, green: 0.0, blue: 1.0, alpha: 1.0))
colorArray.append(UIColor(red: 1.0, green: 0.5, blue: 0.0, alpha: 1.0))
colorArray.append(UIColor(red: 0.0, green: 0.0, blue: 0.5, alpha: 1.0))
colorArray.append(UIColor(hue: 1.2, saturation: 1.0, brightness: 0.5, alpha: 1.0))
colorArray.append(UIColor(hue: 1.2, saturation: 0.6, brightness: 0.7, alpha: 1.0))
我加入5種新的動態顏色,在RGB和HSL。
它們顯示了罰款,在iOS應用:
而且他們還正確地傳送到手錶:
你確定你的數據編碼和解碼是正確的嗎?我會嘗試解除電話本身的數據並查看顏色是否仍然正確。我看不出有什麼明顯的代碼問題,所以這是目前我能想到的唯一bug。 –
可能就是這樣。我會在午餐時間測試一下。 –
我剛測試過它。在iOS上運行良好。我會用我的研究來更新這個問題。 –