19 ios 스위프트 그림그리기 선 원 호
//
// ViewController.swift
// Drawing
//
// Created by MacBookPro on 2017. 11. 29..
// Copyright © 2017년 MacBookPro. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var imageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//선
@IBAction func btnDrawLine(_ sender: UIButton) {
//콘텍스트를 이미지뷰 크기와 같게 설정
UIGraphicsBeginImageContext(imageView.frame.size)
//생성한 콘텍스트의 정보를 가져온다.
let context = UIGraphicsGetCurrentContext()!
//선굵기 설정
context.setLineWidth(2.0)
//선 칼라 설정
context.setStrokeColor(UIColor.red.cgColor)
//시작위치로 커서 이동
context.move(to: CGPoint(x:50, y:50))
context.addLine(to: CGPoint(x:250, y:250))
context.strokePath()
//삼각형 그리기
context.setLineWidth(4.0)
context.setStrokeColor(UIColor.blue.cgColor)
context.move(to: CGPoint(x:150, y:200))
context.addLine(to: CGPoint(x:250, y:350))
context.addLine(to: CGPoint(x:50, y:350))
context.addLine(to: CGPoint(x:150, y:200))
context.strokePath()
imageView.image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
}
//사각형
@IBAction func btnDrawRectangle(_ sender: UIButton) {
//콘텍스트를 이미지뷰 크기와 같게 설정
UIGraphicsBeginImageContext(imageView.frame.size)
//생성한 콘텍스트의 정보를 가져온다.
let context = UIGraphicsGetCurrentContext()!
//선굵기 설정
context.setLineWidth(2.0)
//선 칼라 설정
context.setStrokeColor(UIColor.red.cgColor)
context.addRect(CGRect(x:50, y:100, width:200, height: 200))
context.strokePath()
imageView.image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
}
//원
@IBAction func btnDrawCircle(_ sender: UIButton) {
//콘텍스트를 이미지뷰 크기와 같게 설정
UIGraphicsBeginImageContext(imageView.frame.size)
//생성한 콘텍스트의 정보를 가져온다.
let context = UIGraphicsGetCurrentContext()!
context.setLineWidth(4.0)
context.setStrokeColor(UIColor.blue.cgColor)
context.addEllipse(in: CGRect(x:50, y:50, width: 200, height: 100))
context.strokePath()
context.setLineWidth(5.0)
context.setStrokeColor(UIColor.green.cgColor)
context.addEllipse(in: CGRect(x: 50, y: 200, width: 200, height: 200))
context.strokePath()
imageView.image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
}
//호
@IBAction func btnDrawArc(_ sender: UIButton) {
//콘텍스트를 이미지뷰 크기와 같게 설정
UIGraphicsBeginImageContext(imageView.frame.size)
//생성한 콘텍스트의 정보를 가져온다.
let context = UIGraphicsGetCurrentContext()!
context.setLineWidth(4.0)
context.setStrokeColor(UIColor.blue.cgColor)
context.move(to: CGPoint(x:50, y:50))
context.addArc(tangent1End: CGPoint(x:200, y:50), tangent2End: CGPoint(x:200, y:200), radius: CGFloat(50))
context.addLine(to: CGPoint(x:200, y:200))
context.move(to: CGPoint(x:100, y:250))
context.addArc(tangent1End: CGPoint(x:250, y:250), tangent2End: CGPoint(x:100, y:400), radius: CGFloat(20))
context.addLine(to: CGPoint(x:100, y:400))
context.strokePath()
imageView.image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
}
//채우기
@IBAction func btnDrawFill(_ sender: UIButton) {
//콘텍스트를 이미지뷰 크기와 같게 설정
UIGraphicsBeginImageContext(imageView.frame.size)
//생성한 콘텍스트의 정보를 가져온다.
let context = UIGraphicsGetCurrentContext()!
context.setLineWidth(4.0)
context.setStrokeColor(UIColor.brown.cgColor)
context.setFillColor(UIColor.red.cgColor)
let rectangle = CGRect(x:50, y:50, width: 200, height: 100)
context.addRect(rectangle)
context.fill(rectangle)
context.strokePath()
//draw circle
context.setLineWidth(4.0)
context.setStrokeColor(UIColor.blue.cgColor)
context.setFillColor(UIColor.blue.cgColor)
let circle = CGRect(x:50, y:200, width: 200, height: 100)
context.addEllipse(in: circle)
context.fillEllipse(in: circle)
context.strokePath()
//draw triangle
context.setLineWidth(4.0)
context.setStrokeColor(UIColor.purple.cgColor)
context.setFillColor(UIColor.purple.cgColor)
context.move(to: CGPoint(x:150, y:350))
context.addLine(to: CGPoint(x:250, y:450))
context.addLine(to: CGPoint(x:50, y:450))
context.addLine(to: CGPoint(x:150, y:350))
context.fillPath()
context.strokePath()
imageView.image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
}
}
'ios 뽀개기 > ios앱' 카테고리의 다른 글
21 ios 스위프트 그림그리기 기능 구현 (1) | 2017.11.30 |
---|---|
20 ios 스위프트 터치 기능 (0) | 2017.11.30 |
18 ios 스위프트 사진촬영&사진 불러오기 구현 (4) | 2017.11.29 |
17 ios 스위프트 동영상 재생 구현 (0) | 2017.11.28 |
16 ios 스위프트 오디오&녹음 어플 구현 (7) | 2017.11.28 |
댓글