博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
@IBDesignable和@IBInspectable
阅读量:6087 次
发布时间:2019-06-20

本文共 3224 字,大约阅读时间需要 10 分钟。

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

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

“Live RenderingYou 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@IBDesignableclass 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@IBDesignableclass 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中了。

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

详细project一览:

你可能感兴趣的文章
整合查询和临时表
查看>>
运维工作的前五分钟
查看>>
基于虚拟用户的FTP服务
查看>>
分类算法---朴素贝叶斯算法
查看>>
编译安装LAMP(三)——编译安装php-5.4.13
查看>>
大数据
查看>>
c#中的事件和委托
查看>>
exchange 2013 邮件收发流程
查看>>
我没你想的那么坚强
查看>>
关于坚持
查看>>
Nginx负载均衡的四种模式
查看>>
MySQL 索引类型大汇总
查看>>
js create an object
查看>>
java nio文件传输例子
查看>>
linux 启动项
查看>>
我的友情链接
查看>>
No bundle URL present
查看>>
Sencha Touch 和 jQuery Mobile 的比较
查看>>
win732 安装hadoop
查看>>
ztreeSearch
查看>>