使用 Core Graphics 绘制基本形状
作者:Arthur Knopper,原文鏈接,原文日期:2015-08-31
譯者:lfb_CD;校對:千葉知風;定稿:shanks
Core Graphics是Cocoa和Cocoa Touch所共有的API。它允許你在畫布上繪制圖形對象。在此篇教程中,我們會繪制一些標準的圖形,比如三角形或者圓形。教程運行在 iOS 9 和 Xcode 7 下。
打開 Xcode 并創建一個new Single View Application項目。項目名稱為IOS9DrawShapesTutorial,接著填上你的Organization Name和Organization Identifier,選擇 Swift 語言,確保在設備一欄只選擇了 IPhone。
打開故事板,在主視圖中拖入三個按鈕,使他們水平對齊,并分別設置title為"Lines, Rectangle, Circle"。之后你的故事板內容應該像下面這樣:
選中所有按鈕,打開Attributes Inspector(屬性檢測器)。在View部分給從左到右的按鈕添上"0,1,2"的tag。tag是我們后面才需要的,我們可以通過tag的值得知哪個按鈕被按下了。
打開Assistant Editor(關聯面板),并確保ViewController.swift文件是打開著的。按住 Ctrl鍵,把Lines按鈕拖出到ViewController.swift文件中,并創建下面的Action
選中其它的按鈕,按住Ctrl鍵并拖到ViewController類的IBAction方法里(剛剛創建的那個Action)。之后我們點擊每一個按鈕就會觸發這里的IBAction方法。繪制的圖形會呈現在一個自定義的視圖中。接下來,我們為項目添加一個新文件。選中File ->New File ->iOS ->Source ->Cocoa Touch Class。類名稱為"ShapeView",確保父類為UIView。
打開ShapeView.swift文件,添加下面的屬性。
var currentShapeType: Int = 0currentShapeType屬性是用于選擇正確的方法畫出對應的對象。接著添加初始化方法:
init(frame: CGRect, shape: Int) {super.init(frame: frame)self.currentShapeType = shape }required init?(coder aDecoder: NSCoder) {fatalError("init(coder:) has not been implemented") }當自定義視圖被初始化的時候,tag的值會決定繪制的圖形類型。drawRect方法會在自定義視圖繪制的過程中調用。
override func drawRect(rect: CGRect) {switch currentShapeType {case 0: drawLines()case 1: drawRectangle()case 2: drawCircle()default: print("default")} }接下來,實現繪圖的方法:
func drawLines() {//1let ctx = UIGraphicsGetCurrentContext()//2CGContextBeginPath(ctx)CGContextMoveToPoint(ctx, 20.0, 20.0)CGContextAddLineToPoint(ctx, 250.0, 100.0)CGContextAddLineToPoint(ctx, 100.0, 200.0)CGContextSetLineWidth(ctx, 5)//3CGContextClosePath(ctx)CGContextStrokePath(ctx) }func drawRectangle() {let center = CGPointMake(self.frame.size.width / 2.0, self.frame.size.height / 2.0)let rectangleWidth:CGFloat = 100.0let rectangleHeight:CGFloat = 100.0let ctx = UIGraphicsGetCurrentContext()//4CGContextAddRect(ctx, CGRectMake(center.x - (0.5 * rectangleWidth), center.y - (0.5 * rectangleHeight), rectangleWidth, rectangleHeight))CGContextSetLineWidth(ctx, 10)CGContextSetStrokeColorWithColor(ctx, UIColor.grayColor().CGColor)CGContextStrokePath(ctx)//5CGContextSetFillColorWithColor(ctx, UIColor.greenColor().CGColor)CGContextAddRect(ctx, CGRectMake(center.x - (0.5 * rectangleWidth), center.y - (0.5 * rectangleHeight), rectangleWidth, rectangleHeight))CGContextFillPath(ctx) }func drawCircle() {let center = CGPointMake(self.frame.size.width / 2.0, self.frame.size.height / 2.0)let ctx = UIGraphicsGetCurrentContext()CGContextBeginPath(ctx)//6 CGContextSetLineWidth(ctx, 5)let x:CGFloat = center.xlet y:CGFloat = center.ylet radius: CGFloat = 100.0let endAngle: CGFloat = CGFloat(2 * M_PI)CGContextAddArc(ctx, x, y, radius, 0, endAngle, 0)CGContextStrokePath(ctx) }這里的Graphic Context就是你繪圖的畫布。如果你想在一個視圖上繪圖,那么view就是你的畫布。這里我們需要得到一個Graphic Context的引用。
path就是一些線條,弧線和曲線的集合,你可以在當前畫布使用它們來構建的復雜對象。這里我們繪制了一些線條并設置了線條的寬度為 5。
此處關閉path,并繪制圖像到畫布上。
CGContextAddRect方法給我們繪制了一個長方形,并且外框的顏色為灰色。
這里定義了一個相同的長方形,并填充綠色到內部。
CGContextAddArc繪制了一個圓形。
接著,在ViewController.swift文件中實現buttonPressed方法
@IBAction func buttonPressed(sender: UIButton) {let myView = ShapeView(frame: CGRectMake(50, 200, 280, 250), shape: sender.tag)myView.backgroundColor = UIColor.cyanColor()view.addSubview(myView) }編譯并運行程序,點擊不同的按鈕來繪制不同的圖形。
你可以在Github上下載IOS9DrawShapesTutorial的代碼。
本文由 SwiftGG 翻譯組翻譯,已經獲得作者翻譯授權,最新文章請訪問 http://swift.gg。
總結
以上是生活随笔為你收集整理的使用 Core Graphics 绘制基本形状的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: highcharts中pie统计图获取数
- 下一篇: 四大组件---Activity