17장 탭과 터치 사용하여 스케치 앱 만들기
//
// ViewController.swift
// TapTouch
//
// Created by stayfoolish on 22/10/2018.
// Copyright © 2018 stayfoolish. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
@IBOutlet var txtMessage: UILabel!
@IBOutlet var txtTapsLevel: UILabel!
@IBOutlet var txtTouchsLevel: UILabel!
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.
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first! as UITouch
txtMessage.text = "Touches Began"
txtTapsLevel.text = String(touch.tapCount)
txtTouchsLevel.text = String(touches.count)
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first! as UITouch
txtMessage.text = "Touches Moved"
txtTapsLevel.text = String(touch.tapCount)
txtTouchsLevel.text = String(touches.count)
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first! as UITouch
txtMessage.text = "Touches Ended"
txtTapsLevel.text = String(touch.tapCount)
txtTouchsLevel.text = String(touches.count)
}
}
//
// ViewController.swift
// Sketch
//
// Created by stayfoolish on 22/10/2018.
// Copyright © 2018 stayfoolish. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
@IBOutlet var imgView: UIImageView!
var lastPoint: CGPoint!
var lineSize:CGFloat = 2.0
var lineColor = UIColor.blue.cgColor
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 clearImageView(_ sender: UIButton) {
imgView.image = nil
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first! as UITouch
lastPoint = touch.location(in: imgView)
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
UIGraphicsBeginImageContext(imgView.frame.size)
UIGraphicsGetCurrentContext()?.setStrokeColor(lineColor)
UIGraphicsGetCurrentContext()?.setLineCap(CGLineCap.round)
UIGraphicsGetCurrentContext()?.setLineWidth(lineSize)
let touch = touches.first! as UITouch
let currPoint = touch.location(in: imgView)
imgView.image?.draw(in: CGRect(x: 0, y: 0, width: imgView.frame.size.width, height: imgView.frame.size.height))
UIGraphicsGetCurrentContext()?.move(to: CGPoint(x: lastPoint.x, y: lastPoint.y))
UIGraphicsGetCurrentContext()?.addLine(to: CGPoint(x: currPoint.x, y: currPoint.y))
UIGraphicsGetCurrentContext()?.strokePath()
imgView.image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
lastPoint = currPoint
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
UIGraphicsBeginImageContext(imgView.frame.size)
UIGraphicsGetCurrentContext()?.setStrokeColor(lineColor)
UIGraphicsGetCurrentContext()?.setLineCap(CGLineCap.round)
UIGraphicsGetCurrentContext()?.setLineWidth(lineSize)
imgView.image?.draw(in: CGRect(x: 0, y: 0, width: imgView.frame.size.width, height: imgView.frame.size.height))
UIGraphicsGetCurrentContext()?.move(to: CGPoint(x: lastPoint.x, y: lastPoint.y))
UIGraphicsGetCurrentContext()?.addLine(to: CGPoint(x: lastPoint.x, y: lastPoint.y))
UIGraphicsGetCurrentContext()?.strokePath()
imgView.image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
}
override func motionEnded(_ motion: UIEventSubtype, with event: UIEvent?) {
if motion == .motionShake{
imgView.image = nil
}
}
}
'Swift > 기초&문법' 카테고리의 다른 글
do it 스위프트 아이폰 앱 만들기 19장 Picnch Gesture swift (0) | 2018.10.25 |
---|---|
do it 스위프트 아이폰 앱 만들기 18장 swift swipe gestrue 스와이프 제스처 (0) | 2018.10.24 |
do it 스위프트 아이폰 앱 만들기 15~16장 swift camera , photo library , Dra Graphics app (0) | 2018.10.22 |
do it 스위프트 아이폰 앱 만들기 13~14장 swift Audio MoviePlayer app 오디오 비디오 앱 (0) | 2018.10.21 |
do it 스위프트 아이폰 앱 만들기 12장 swift tableview 테이블뷰 (0) | 2018.10.20 |