標籤:代碼 nbsp swift override 旋轉 graphics lin border stroke
圖形內容相關的矩陣操作(旋轉、縮放和平移)CGContextRotateCTM:圖形上下文旋轉,以內容相關的原點(左上方)為基準CGContextScaleCTM:圖形內容相關的縮放,以內容相關的原點(左上方)為基準CGContextTranslateCTM:圖形內容相關的平移,以內容相關的原(左上方)點為基準 注意:一定要在添加路徑之前進行設定 下面貼出swift版代碼:
1 override func draw(_ rect: CGRect) { 2 let context = UIGraphicsGetCurrentContext() 3 4 // MARK: - 注意:矩陣操作一定要在添加路徑之前設定 5 6 // 旋轉 - 逆時針旋轉-M_PI_4(以原點為基準) 7 // context?.rotate(by: CGFloat(-M_PI_4)) 8 9 // 平移 - 往左下角(以原點為基準)10 // context?.translateBy(x: 50, y: 50)11 12 // 縮放 - 寬高各縮小一半(以原點為基準)13 context?.scaleBy(x: 0.5, y: 0.5)14 15 let borderPath = UIBezierPath(rect: rect)16 17 let rectPath = UIBezierPath(rect: CGRect(x: 10, y: 10, width: 200, height: 200))18 19 let circlePath = UIBezierPath(ovalIn: CGRect(x: 10, y: 210, width: 200, height: 200))20 21 context?.addPath(borderPath.cgPath)22 context?.addPath(rectPath.cgPath)23 context?.addPath(circlePath.cgPath)24 25 context?.setLineWidth(2)26 27 context?.strokePath()28 }
圖形內容相關的矩陣操作(平移-縮放-旋轉)