(第一次用簡書排版不周請見諒)
簡述:IOS 即時渲染從xcode6就有了今天教得這個版本我拿得xcode7.1正式版本搞的。
1.新建項目選擇Single View Application→Next

選擇Swift 如圖

2.選擇項目File→New→Target
選擇Cocoa Touch Framework之后隨便給個名字譬如ErNaoshuai(二撓帥),然后Finish

3.這時候你項目應(yīng)該是這樣的

4.在ErNaoshuai內(nèi)新建Cocoa Touch Class 或者直接command+n如下圖

隨便起個名字然后繼承自UIView

在這個文件內(nèi)把如下代碼添加上去或者直接復(fù)制
//
//? AFKUIView.swift
//? 即時渲染Demo
//
//? Created by 張建強 on 15/10/26.
//? Copyright ? 2015年 二撓. All rights reserved.
//
import UIKit
@IBDesignable class AFKUIView: UIView {
@IBInspectable var style:NSInteger? = 0 {
didSet {
switch style {
case 0:
self.backgroundColor = UIColor.blueColor();
case 1:
self.backgroundColor = UIColor.whiteColor()
default:
self.backgroundColor = UIColor.yellowColor()
}
}
}
/*? ? // Only override drawRect: if you perform custom drawing.? ? // An empty implementation adversely affects performance during animation.? ? override func drawRect(rect: CGRect)? ? {? ? ? ? // Drawing code? ? }? ? */
}
5.回到項目的Main.storyboard內(nèi)拖進(jìn)去一個UIView,設(shè)置Class為AFKUIView,再回到設(shè)置屬性那里會發(fā)現(xiàn)上面多一條

這個Style就是我們代碼內(nèi)寫的@IBInspectable var style:NSInteger? = 0,默認(rèn)把首字母大寫了 這時你分別設(shè)置下這個屬性為0,1,2 自己玩玩吧~!

完整代碼放到github上了,如有需求自己下載,點我下載