[swift] Swift Core Graphics로 그래프 그리기
Swift에서 Core Graphics를 사용하여 그래프를 그리는 방법에 대해 알아보겠습니다.
Core Graphics 소개
Core Graphics는 iOS 및 macOS 애플리케이션에서 2D 그래픽을 그리기 위한 프레임워크입니다. Core Graphics를 사용하여 커스텀 뷰나 이미지를 그리거나 애니메이션을 만들 수 있습니다.
그래프 그리기
다음은 Swift를 사용하여 Core Graphics를 이용해 간단한 선 그래프를 그리는 예제입니다:
import UIKit
class GraphView: UIView {
override func draw(_ rect: CGRect) {
let context = UIGraphicsGetCurrentContext()
context?.setStrokeColor(UIColor.blue.cgColor)
context?.setLineWidth(2.0)
context?.move(to: CGPoint(x: 0, y: 0))
context?.addLine(to: CGPoint(x: 50, y: 100))
context?.addLine(to: CGPoint(x: 100, y: 50))
context?.addLine(to: CGPoint(x: 150, y: 75))
context?.strokePath()
}
}
위의 예제는 GraphView
클래스를 만들고 draw(_ rect:)
메서드를 오버라이드하여 Core Graphics 컨텍스트를 얻어 선을 그리는 방법을 보여줍니다.
추가 리소스
- Core Graphics Framework - Apple Developer Documentation
- Drawing and Printing Guide for iOS - Apple Developer Documentation
이제 Core Graphics를 사용하여 Swift로 그래프를 그리는 방법에 대해 좀 더 이해하셨을 것입니다!