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

        }

    }

    

}










+ Recent posts