近期一直在看苹果公司提供的两本swift官方教程电子书,一部是《The Swift Programming Language》,还有一部是《Using Swift With Cocoa and Objective-C》。昨天正好看到第二部电子书的“Writing Swift Classes with Objective-C Behavior”这一节,当中讲述了关于实时渲染这一技术。

以下是摘抄的当中一段内容:

“Live Rendering
You can use two different attributes—@IBDesignable and @IBInspectable—to enable live, interactive custom view design in Interface Builder. When you create a custom view that inherits from the UIView class or the NSView class, you can add the @IBDesignable attribute just before the class declaration. After you add the custom view to Interface Builder (by setting the custom class of the view in the inspector pane), Interface Builder renders your view in the canvas. You can also add the @IBInspectable attribute to properties with types compatible with user defined runtime attributes. After you add your custom view to Interface Builder, you can edit these properties in the inspector. SWIFT @IBDesignable
class MyCustomView: UIView {
@IBInspectable var textColor: UIColor
@IBInspectable var iconHeight: CGFloat
/* ... */
}
” 摘录来自: Apple Inc. “Using Swift with Cocoa and Objective-C”。 iBooks. https://itunes.apple.com/cn/book/using-swift-cocoa-objective/id888894773? mt=11

其大意就是说。能够将自己定义的代码实时渲染到Interface Builder中。

而它们之间的桥梁就是通过两个指令来完毕。即@IBDesignable和@IBInspectable。我们通过@IBDesignable告诉Interface Builder这个类能够实时渲染到界面中,可是这个类必须是UIView或者NSView的子类。通过@IBInspectable能够定义动态属性,就可以在attribute inspector面板中可视化改动属性值。

话不多说,以下举一个简单的样例,这个样例自己定义一个UIView的子类,该子类拥有一个UIButton。

import UIKit

@IBDesignable
class MyCustomView: UIView { @IBInspectable var buttonTitleColor: UIColor! // button title color
@IBInspectable var buttonTitle: String! // button title
@IBInspectable var buttonFrame: CGRect! // button frame var myButton: UIButton! override init(frame: CGRect) {
// init stored properties
buttonTitleColor = UIColor.redColor()
buttonTitle = "button title"
buttonFrame = CGRectMake(0, 0, 100, 50) myButton = UIButton(frame: buttonFrame)
myButton.setTitleColor(buttonTitleColor, forState: .Normal)
myButton.setTitle(buttonTitle, forState: .Normal) // call super initializer
super.init(frame: frame) // add button to self
addSubview(myButton) } required init(coder aDecoder: NSCoder) {
// init stored properties
buttonTitleColor = UIColor.redColor()
buttonTitle = "button title"
buttonFrame = CGRectMake(0, 0, 100, 50) myButton = UIButton(frame: buttonFrame)
myButton.setTitleColor(buttonTitleColor, forState: .Normal)
myButton.setTitle(buttonTitle, forState: .Normal) // call super initializer
super.init(coder: aDecoder) // add button to self
addSubview(myButton)
} override func layoutSubviews() {
// refresh button state through attribute inspector
myButton.setTitleColor(buttonTitleColor, forState: .Normal)
myButton.setTitle(buttonTitle, forState: .Normal)
} }

上图:

从图中能够看到,代码实时渲染到IB中了。

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdG91bmFvYnVu/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">

另外,我们在attribute inspector中也能够看到,由指令@IBInspectable声明的属性也出如今了面板中,通过改动这些值能够动态改变界面的效果(须要实现layoutSubviews方法)

详细project一览:

@IBDesignable和@IBInspectable的更多相关文章

  1. Swift - @IBDesignable和@IBInspectable

    前言: 用storyboard/xib搞项目时,一些属性在Interface Builder上时无法设置,比如常用的layer的一些属性cornerRadius,borderColor等 (有时没必须 ...

  2. swift 第十四课 可视化view: @IBDesignable 、@IBInspectable

    以前应objctiew-c 写项目的时候,就知道有这两个关键字,现在用swift了.用法稍作改变,基本用法还是一致的 虽然使用这个之后,有时候会报错的非常的莫名其妙----(其实还是自己技术不够牛…… ...

  3. iOS @IBDesignable和@IBInspectable

    http://www.tuicool.com/articles/JVNRBjY @IBDesignable和@IBInspectable 时间 2014-10-08 11:02:03  CSDN博客 ...

  4. 关于IB_DESIGNABLE / IBInspectable的那些事

    前言 IB_DESIGNABLE / IBInspectable 这两个关键字是在WWDC 2014年”What’s New in Interface Builder”这个Session里面,用Swi ...

  5. 如何设计一个 iOS 控件?(iOS 控件完全解析) (转)

    前言 一个控件从外在特征来说,主要是封装这几点: 交互方式 显示样式 数据使用 对外在特征的封装,能让我们在多种环境下达到 PM 对产品的要求,并且提到代码复用率,使维护工作保持在一个相对较小的范围内 ...

  6. [翻译]使用Swift在Xcode中创建自定义控件

    使用Swift在Xcode中创建自定义控件 原文 IBDesignable and IBInspectable With IBDesignable and IBInspectable, develop ...

  7. ios Swift 特性

    特性提供了关于声明和类型的更多信息.在Swift中有两类特性,用于修饰声明的以及用于修饰类型的.例如,required特性,当应用于一个类的指定或便利初始化器声明时,表明它的每个子类都必须实现那个初始 ...

  8. Swift互用性: 使用Objective-C特性编写Swift类(Swift 2.0版)-b

    本节包括内容: 继承Objective-C的类(Inheriting from Objective-C Classes) 采用协议(Adopting Protocols) 编写构造器和析构器(Writ ...

  9. 如何设计一个 iOS 控件?(iOS 控件完全解析)

    前言 一个控件从外在特征来说,主要是封装这几点: 交互方式 显示样式 数据使用 对外在特征的封装,能让我们在多种环境下达到 PM 对产品的要求,并且提到代码复用率,使维护工作保持在一个相对较小的范围内 ...

随机推荐

  1. SQL 去除小数点后无效 0 的方法

     select convert(float,10.0000)  就是这么简单

  2. nexus 的使用及maven的配置

    一.nexus的安装 1.下载nexus(点解这里) 2.下载后解压文件,将解压后的nexus文件放在你自己想要的地方 3.配置环境变量(和配置java的环境变量一样) 4.安装和启动nexus 由于 ...

  3. xcode 删除 Provisioning Profile

    provisioning profile path: ~/Library/MobileDevice/Provisioning Profiles 打开并日期排序,删除老的 provisioning pr ...

  4. JavaScript 显示弹出窗口

    window . showModalDialog ( sURL,vArguments , sFeatures )参数说明: sURL--必选参数,用来指定对话框要显示的文档的URL. //要显示页面的 ...

  5. JAVA-2-GetDay

    import java.util.*; public class Ch0310 { public static void main(String[] args) { // TODO 自动生成的方法存根 ...

  6. decode()与case then 学习与使用

    今天做项目的时候遇到一个oracle数值转换的问题,按需求需要对匹配系统时间进行固定赋值,为了避免增加复杂度并易于维护,尽量不要使用存储过程或触发器,最好是使用oracle 自带函数. 如: SQL& ...

  7. MPMediaPickerController 选取本地音频--备用

    MPMediaPickerController 与 UIImagePickerController 类似,允许用户从音乐库中选取音乐.播客.音频书. 一.创建 MPMediaPickerControl ...

  8. 转:Keil MDK从未有过的详细使用讲解

    来自:http://blog.csdn.net/zhzht19861011/article/details/5846510 熟悉Keil C 51的朋友对于Keil MDK上手应该比较容易,毕竟界面是 ...

  9. VC++中调用cmd的集中方式

    1. system方法: 原型: int __cdecl system(const char *); 例如: system("ipconfig"); 2. WinExec方法: 相 ...

  10. 文件上传[Uploadify]

    Uploadify文件上传插件 Uploadify简单说来,是基于Jquery的一款文件上传插件.它的功能特色总结如下: 支持单文件或多文件上传,可控制并发上传的文件数 在服务器端支持各种语言与之配合 ...