(第一次用簡書排版不周請見諒)
簡述:IOS 即時渲染從xcode6就有了今天教得這個版本我拿得xcode7.1正式版本搞的。
1.新建項目選擇Single View Application→Next
選擇Swift 如圖
2.選擇項目File→New→Target
選擇Cocoa Touch Framework之后隨便給個名字譬如ErNaoshuai(二撓帥),然后Finish
3.這時候你項目應該是這樣的
4.在ErNaoshuai內新建Cocoa Touch Class 或者直接command+n如下圖
隨便起個名字然后繼承自UIView
在這個文件內把如下代碼添加上去或者直接復制
//
//? 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內拖進去一個UIView,設置Class為AFKUIView,再回到設置屬性那里會發現上面多一條
這個Style就是我們代碼內寫的@IBInspectable var style:NSInteger? = 0,默認把首字母大寫了 這時你分別設置下這個屬性為0,1,2 自己玩玩吧~!
完整代碼放到github上了,如有需求自己下載,點我下載