IBDesignable View渲染超时 [英] IBDesignable View Rendering times out

查看:120
本文介绍了IBDesignable View渲染超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从第一个XCode 6 / iOS 8 beta版本开始,我一直在开发和关闭应用程序几个月。我最喜欢的功能之一是实时渲染,可以使用Swift中的 @IBDesignable 标签。

I've been working on an app on and off for a few months starting with the first XCode 6/iOS 8 beta release. One of my favorite features added is live rendering, made possible with the @IBDesignable tag in Swift.

我的避风港生活渲染能够得到一件事。我认为一定是因为它是测试版,所以我决定等待完整版本再次尝试。它仍然失败了。我认为在我的代码中可能存在来自beta版本的工件,所以我废弃它并开始新鲜。它仍然无法正常工作。当然,这些错误现在更具描述性。

I haven't been able to get a single thing to live render. I figured that must have been because it was a beta release, so I decided to wait for the full release to come out to try again. It still failed. I figured then that there might be artifacts from the beta releases in my code, so I scrapped it and started fresh. It still doesn't work. Granted, the errors are slightly more descriptive now.

这是我的代码:

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()
    }
}

以下是我在故事板中尝试使用其中一个按钮构建时出现的错误:

Here are the errors I get when I try to build with one of these buttons in my storyboard:

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.

正如您在我的代码中所看到的,我原本希望这个按钮被舍入。我认为这可能是造成这个问题的原因,尽管让我感到惊讶的是Apple会设计一个低效的圆角矩形绘制算法。我切换到简单地设置颜色并按原样绘制矩形。仍有问题。

As you can see in my code, I originally wanted this button to be rounded. I figured this might be the reason for this problem, though it surprised me that Apple would design a rounded rectangle drawing algorithm that inefficient. I switched to simply setting the color and drawing the rectangle as-is. Still had problems.

我认为(我希望,无论如何)有一件小事我做错了,因为我用Google搜索并且没有人其他似乎有这个问题。好像它可能是某种无限循环?

I figure (I hope, anyway) that there is one small thing that I'm doing wrong, because I've googled and there is no one else that seems to have this problem. Seems like it might be some kind of infinite loop?

这不是我继续需要的东西,但让实时渲染工作将使开发更快更容易对我来说,因为我将能够看到我的界面并测试它们而无需运行它们。

It's not something that's necessary for me to continue, but getting live rendering to work will make development a lot faster and easier for me, because I will be able to see my interfaces and test them without having to run them.

提前感谢任何有远程线索如何解决的人这个。

Thanks in advance to anyone who has a remote clue on how to solve this.

推荐答案

显然我需要覆盖 init(frame:CGRect)以及 init(代码:NSCoder)

搞定了!如果有人能够解释为什么这不起作用,那就太好了。否则,我在这里很好。

Got it working! If anyone could care to explain why this wasn't working, that would be great. Otherwise, I'm fine here.

这篇关于IBDesignable View渲染超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆