티스토리 뷰

Xcode(버전11.3)를 코어 그래픽스(Core Graphics) 화면에 꽃 모양(원, 삼각형) 그림 그리기 앱을 만든다:D

전에 만든 프로젝트를 참고하여 구현한다.

 

[iOS/swift] 코어 그래픽스(Core Graphics) 화면에 그림 그리기 앱 만들기

Xcode(버전11.3)를 코어 그래픽스(Core Graphics) 그림 그리기 앱을 만든다:D 코어 그래픽스(Core Graphics)는 아이폰과 아이패드에서 2차원 그래픽을 그릴 수 있도록 제공하는 그래픽 라이브러리이다. 코어 그래..

moonibot.tistory.com

 

 

 

- 스토리보드 화면 구성

Image View 추가

 

 

 

전체 소스 보기

//
//  ViewController.swift
//  day200101_DrawGraphics2
//
//  Created by 무니 on 2020/01/01.
//  Copyright © 2020 com.mooni. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    @IBOutlet var imgView: UIImageView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        drawFlower()
    }
    
    // 꽃 모양의 그림을 그리는 함수
    private func drawFlower(){
        UIGraphicsBeginImageContext(imgView.frame.size)
        let context = UIGraphicsGetCurrentContext()!
        
        // 삼각형 그리기
        context.setLineWidth(1.0)
        context.setStrokeColor(UIColor.green.cgColor)
        context.setFillColor(UIColor.green.cgColor)
        
        context.move(to: CGPoint(x: 140, y: 200))
        context.addLine(to: CGPoint(x: 170, y: 450))
        context.addLine(to: CGPoint(x: 110, y: 450))
        context.addLine(to: CGPoint(x: 140, y: 200))
        context.fillPath() // 선 채우기
        context.strokePath() // 선으로 그려진 삼각형의 내부 채우기
        
        // 원 그리기
        context.setLineWidth(1.0)
        context.setStrokeColor(UIColor.red.cgColor)
        
        context.addEllipse(in: CGRect(x: 90, y: 150, width: 100, height: 100))
        context.addEllipse(in: CGRect(x: 90+50, y: 150, width: 100, height: 100))
        context.addEllipse(in: CGRect(x: 90-50, y: 150, width: 100, height: 100))
        context.addEllipse(in: CGRect(x: 90, y: 150-50, width: 100, height: 100))
        context.addEllipse(in: CGRect(x: 90, y: 150+50, width: 100, height: 100))
        context.strokePath()
        
        imgView.image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
    }
}

 

 

 

시뮬레이터 결과 화면

 

공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함