2014-11-06 129 views
25

從第一個XCode 6/iOS 8 beta版本開始,我一直在開發和關閉應用程序幾個月。我最喜歡的功能之一是實時渲染,使用Swift中的@IBDesignable標籤。IBDesignable查看渲染超時

我還沒有能夠得到一個單一的東西來生活渲染。我想這一定是因爲它是一個測試版,所以我決定等待完整版發佈再試一次。它仍然失敗。然後我發現在我的代碼中可能存在來自測試版本的文物,所以我放棄了它並開始新鮮。它仍然不起作用。當然,這些錯誤稍微有些描述性。

這裏是我的代碼:

import UIKit 

@IBDesignable class MyButton : UIButton { 

    let UNPRESSED_COLOR = UIColor(red: 221.0/256.0, green: 249.0/256.0, blue: 14.0/256.0, alpha: 1.0) 
    let PRESSED_COLOR = UIColor(red: 166.0/256.0, green: 187.0/156.0, blue: 11.0/256.0, alpha: 1.0) 
    var buttonColorValue: UIColor 

    let TEXT_COLOR = UIColor(red: 72.0/256.0, green: 160.0/256.0, blue: 5.0/256.0, alpha: 1.0) 

    required init(coder: NSCoder) { 
     self.buttonColorValue = UNPRESSED_COLOR 
     super.init(coder: coder) 
     // Initialization code 
     self.setTitleColor(TEXT_COLOR, forState: UIControlState.Normal) 
     self.setTitleColor(TEXT_COLOR, forState: UIControlState.Highlighted) 
     self.setTitleColor(TEXT_COLOR, forState: UIControlState.Selected) 
    } 

    override func touchesBegan(touches: NSSet, withEvent event: UIEvent) { 
     super.touchesBegan(touches, withEvent: event) 
     buttonColorValue = PRESSED_COLOR 
     self.setNeedsDisplay() 
    } 

    override func touchesEnded(touches: NSSet, withEvent event: UIEvent) { 
     super.touchesEnded(touches, withEvent: event) 
     buttonColorValue = UNPRESSED_COLOR 
     self.setNeedsDisplay() 
    } 

    override func touchesCancelled(touches: NSSet!, withEvent event: UIEvent!) { 
     super.touchesCancelled(touches, withEvent: event) 
     buttonColorValue = UNPRESSED_COLOR 
     self.setNeedsDisplay() 
    } 

    override func drawRect(rect: CGRect) { 
     buttonColorValue.setFill() 
     let ctx = UIGraphicsGetCurrentContext() 
     CGContextFillRect(ctx, rect) 
     //UIBezierPath(roundedRect: rect, cornerRadius: 5.0).fill() 
    } 
} 

這裏是我得到的錯誤,當我嘗試建立與我的故事板這些按鈕中的一個:

IB Designables: Failed to update auto layout status: Interface Builder Cocoa Touch Tool crashed 

IB Designables: Failed to render instance of MyButton: Rendering the view took longer than 200 ms. Your drawing code may suffer from slow performance. 

正如你可以在我的代碼中看到,我原本希望這個按鈕被四捨五入。我想這可能是導致這個問題的原因,但它讓我感到吃驚,蘋果會設計出一個效率低下的圓角矩形繪圖算法。我切換到簡單地設置顏色並按原樣繪製矩形。仍然有問題。

我想(我希望,反正)有是我做錯了一個很小的事情,因爲我GOOGLE了,也沒有其他人似乎也有這個問題。似乎它可能是某種無限循環?

這不是我需要繼續下去的事情,但讓生活渲染工作將使開發更快,更容易,因爲我將能夠看到我的界面並測試它們而無需運行它們。

在此先感謝任何人如何解決此問題的遠程線索。

回答

49

顯然,我需要重寫與init(code: NSCoder)一起init(frame: CGRect)

得到它的工作!如果有人可以關心解釋爲什麼這不起作用,那會很好。否則,我在這裏很好。

+3

據我讀過的其他職位,實時呈現不調用初始化 – 2014-12-28 16:48:03

+1

重寫的init(幀:的CGRect)時,init(代碼:NSCoder)和init()也幫助了我。 – DoN1cK 2015-03-04 09:32:41

+0

哈,難怪!謝謝你,這也解決了我的問題。不知道蘋果爲什麼不讓'init(frame:CGRect)'成爲必需的覆蓋。 – Ash 2015-04-15 07:16:12

0

對於我來說,這個錯誤竟然是由於我@IBInspectable屬性之一阻塞調用SQLite數據庫(使用CoreData)。

我的代碼最初查詢數據庫,然後用dispatchAsync設置標籤的基礎上,查詢的結果值。我對其進行了更改,以便查詢也在dispatchAsync區塊中啓動。錯誤立即消失。

其他人是否具有該錯誤的麻煩,我建議檢查出,將在設計階段期間被執行的任何代碼(構造或計算屬性標有@IBInspetable)。如果存在任何阻止代碼(網絡操作,數據庫訪問等),則可能導致超時錯誤。希望這可以幫助。

6

雖然這可能不是真的是一個有用的答案,我發現,尋找解決這個半小時後,一個簡單的關閉並重新打開的Xcode的伎倆 - 如果你還沒有嘗試過這種尚未給予它一去!

1

有同樣的問題,但一瞥這篇文章。它解決了我的問題。

http://nshipster.com/ibinspectable-ibdesignable/

總之

要獲得在Xcode這些選項

enter image description here

作出這樣一個屬性:

@IBInspectable var cornerRadius: CGFloat = 0 { 
    didSet { 
     layer.cornerRadius = cornerRadius 
     layer.masksToBounds = cornerRadius > 0 
    } 
} 
2

確保您覆蓋prepareForInterfaceBuilder來解決此問題。在所有:(NSCoder代碼) - 只是初始化(幀:的CGRect)

@IBDesignable 
class SomeView: UITableView { 
@IBInspectable var something: Int? { didSet { setup() } } 

required init?(coder aDecoder: NSCoder) { 
    super.init(coder: aDecoder) 
    setup() 
} 

override init(frame: CGRect, style: UITableViewStyle) { 
    super.init(frame: frame, style: style) 
    setup() 
} 

override func prepareForInterfaceBuilder() { 
    super.prepareForInterfaceBuilder() 
    setup() 
} 

func setup() { 
    // do sth 
} 
}