18장 스와이프 제스처 사용하기 




//

//  ViewController.swift

//  SwipeGesture

//

//  Created by stayfoolish on 23/10/2018.

//  Copyright © 2018 stayfoolish. All rights reserved.

//



import UIKit


class ViewController: UIViewController {

    let numOfTouchs = 2

    

    

    @IBOutlet var imgViewUp: UIImageView!

    @IBOutlet var imgViewDown: UIImageView!

    @IBOutlet var imgViewLeft: UIImageView!

    @IBOutlet var imgViewRight: UIImageView!

    

    var imgLeft = [UIImage]()

    var imgRight = [UIImage]()

    var imgUp = [UIImage]()

    var imgDown = [UIImage]()

    

    override func viewDidLoad() {

        super.viewDidLoad()

        // Do any additional setup after loading the view, typically from a nib.

        

        


        imgUp.append(UIImage(named: "arrow-up-black.png")!)

        imgUp.append(UIImage(named: "arrow-up-red.png")!)

        imgUp.append(UIImage(named: "arrow-up-green.png")!)

        

        imgDown.append(UIImage(named: "arrow-down-black.png")!)

        imgDown.append(UIImage(named: "arrow-down-red.png")!)

        imgDown.append(UIImage(named: "arrow-down-green.png")!)


        imgLeft.append(UIImage(named: "arrow-left-black.png")!)

        imgLeft.append(UIImage(named: "arrow-left-red.png")!)

        imgLeft.append(UIImage(named: "arrow-left-green.png")!)

        

        imgRight.append(UIImage(named: "arrow-right-black.png")!)

        imgRight.append(UIImage(named: "arrow-right-red.png")!)

        imgRight.append(UIImage(named: "arrow-right-green.png")!)

        

        imgViewUp.image = imgUp[0]

        imgViewDown.image = imgDown[0]

        imgViewLeft.image = imgLeft[0]

        imgViewRight.image = imgRight[0]


        

        let swipeUp = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.respondToSwipeGesture(_:)))

        

        swipeUp.direction = UISwipeGestureRecognizerDirection.up

//        swipeUp.numberOfTouchesRequired = numOfTouchs

        self.view.addGestureRecognizer(swipeUp)

        

        let swipeDown = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.respondToSwipeGesture(_:)))

        swipeDown.direction = UISwipeGestureRecognizerDirection.down

//        swipeDown.numberOfTouchesRequired = numOfTouchs

        self.view.addGestureRecognizer(swipeDown)

        

        let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.respondToSwipeGesture(_:)))

        

        swipeLeft.direction = UISwipeGestureRecognizerDirection.left

//        swipeLeft.numberOfTouchesRequired = numOfTouchs

        self.view.addGestureRecognizer(swipeLeft)

        

        let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.respondToSwipeGesture(_:)))

        

        swipeRight.direction = UISwipeGestureRecognizerDirection.right

//        swipeRight.numberOfTouchesRequired = numOfTouchs

        self.view.addGestureRecognizer(swipeRight)

        

        let swipeUpMulti = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.respondToSwipeGestureMulti(_:)))

        swipeUpMulti.direction = UISwipeGestureRecognizerDirection.up

        swipeUpMulti.numberOfTouchesRequired = numOfTouchs

        self.view.addGestureRecognizer(swipeUpMulti)

        

        let swipeDownMulti = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.respondToSwipeGestureMulti(_:)))

        swipeDownMulti.direction = UISwipeGestureRecognizerDirection.down

        swipeDownMulti.numberOfTouchesRequired = numOfTouchs

        self.view.addGestureRecognizer(swipeDownMulti)

        

        let swipeLeftMulti = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.respondToSwipeGestureMulti(_:)))

        swipeLeftMulti.direction = UISwipeGestureRecognizerDirection.left

        swipeLeftMulti.numberOfTouchesRequired = numOfTouchs

        self.view.addGestureRecognizer(swipeLeftMulti)

        

        let swipeRightMulti = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.respondToSwipeGestureMulti(_:)))

        swipeRightMulti.direction = UISwipeGestureRecognizerDirection.right

        swipeRightMulti.numberOfTouchesRequired = numOfTouchs

        self.view.addGestureRecognizer(swipeRightMulti)

        

    }


    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

        // Dispose of any resources that can be recreated.

    }


    @objc func respondToSwipeGesture(_ gesture: UIGestureRecognizer) {

        if let swipeGesture = gesture as? UISwipeGestureRecognizer {

            

            imgViewUp.image = imgUp[0]

            imgViewDown.image = imgDown[0]

            imgViewLeft.image = imgLeft[0]

            imgViewRight.image = imgRight[0]

            

            switch swipeGesture.direction{

            case UISwipeGestureRecognizerDirection.up:

                imgViewUp.image = imgUp[1]

            case UISwipeGestureRecognizerDirection.down:

                imgViewDown.image = imgDown[1]

            case UISwipeGestureRecognizerDirection.left:

                imgViewLeft.image = imgLeft[1]

            case UISwipeGestureRecognizerDirection.right:

                imgViewRight.image = imgRight[1]

            default:

                break

            }

        }

    }

    

    @objc func respondToSwipeGestureMulti(_ gesture: UIGestureRecognizer){

        if let swipeGesture = gesture as? UISwipeGestureRecognizer {

            

            imgViewUp.image = imgUp[0]

            imgViewDown.image = imgDown[0]

            imgViewLeft.image = imgLeft[0]

            imgViewRight.image = imgRight[0]

            

            switch swipeGesture.direction{

            case UISwipeGestureRecognizerDirection.up:

                imgViewUp.image = imgUp[2]

            case UISwipeGestureRecognizerDirection.down:

                imgViewDown.image = imgDown[2]

            case UISwipeGestureRecognizerDirection.left:

                imgViewLeft.image = imgLeft[2]

            case UISwipeGestureRecognizerDirection.right:

                imgViewRight.image = imgRight[2]

            default:

                break

            }

        }

    }

    

    

}







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

        }

    }

    

}












15장 카메라와 포토 라이브러리에서 미디어 가져오기



개발자 계정이 있어야 되서 동영상 녹화 못함, 시뮬레이션으로 하면 아래와 같이 에러 발생

 




//

//  ViewController.swift

//  CameraPhotoLibrary

//

//  Created by stayfoolish on 22/10/2018.

//  Copyright © 2018 stayfoolish. All rights reserved.

//


import UIKit

import MobileCoreServices


class ViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate {


    @IBOutlet var imgView: UIImageView!

    

    let imagePicker: UIImagePickerController! = UIImagePickerController()

    var captureImage: UIImage!

    var videoURL: URL!

    var flagImageSave = false

    

    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 btnCaptureImageFromCamera(_ sender: UIButton) {

        if (UIImagePickerController.isSourceTypeAvailable(.camera)){

            flagImageSave = true

            

            imagePicker.delegate = self

            imagePicker.sourceType = .camera

            imagePicker.mediaTypes = [kUTTypeImage as String]

            imagePicker.allowsEditing = false

            

            present(imagePicker, animated: true, completion: nil)

        } else {

            myAlert("Camera inaccessable", message: "Application cannot access the camera.")

        }

    }

    @IBAction func btnLoadImageFromLibrary(_ sender: UIButton) {

        if (UIImagePickerController.isSourceTypeAvailable(.photoLibrary)){

            flagImageSave = false

            

            imagePicker.delegate = self

            imagePicker.sourceType = .photoLibrary

            imagePicker.mediaTypes = [kUTTypeImage as String]

            imagePicker.allowsEditing = true

            

            present(imagePicker, animated: true, completion: nil )

        }else {

            myAlert("Photo album inaccessable", message: "Application cannot access the photo album.")

        }

    }

    @IBAction func btnRecordVideoFromCamera(_ sender: UIButton){

        if (UIImagePickerController.isSourceTypeAvailable(.camera)){

            flagImageSave = true

            

            imagePicker.delegate = self

            imagePicker.sourceType = .camera

            imagePicker.mediaTypes = [kUTTypeImage as String]

            imagePicker.allowsEditing = false

            

            present(imagePicker, animated: true, completion: nil)

        }else {

            myAlert("Camera inaccessable", message: "Application cannout access the camera.")

        }

        

    }

    @IBAction func btnLoadVideoFromLibrary(_ sender: UIButton){

        if (UIImagePickerController.isSourceTypeAvailable(.photoLibrary)){

            flagImageSave = false

            

            imagePicker.delegate = self

            imagePicker.sourceType = .photoLibrary

            imagePicker.mediaTypes = [kUTTypeImage as String]

            imagePicker.allowsEditing = false

            

            present(imagePicker, animated: true, completion: nil)

        }else {

            myAlert("Photo album inaccessable", message: "Application cannout access the photo album.")

        }

    }

    

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {

        let mediaType = info[UIImagePickerControllerMediaType] as! NSString

        

        if mediaType.isEqual(to: kUTTypeImage as NSString as String){

            captureImage = info[UIImagePickerControllerOriginalImage] as! UIImage

            

            if flagImageSave {

                UIImageWriteToSavedPhotosAlbum(captureImage, self, nil, nil)

            }

            imgView.image = captureImage

        }else if mediaType.isEqual(to: kUTTypeMovie as NSString as String){

            if flagImageSave {

                videoURL = (info[UIImagePickerControllerMediaURL] as! URL)

                

                UISaveVideoAtPathToSavedPhotosAlbum(videoURL.relativePath, self, nil, nil)


            }

        }

        self.dismiss(animated: true, completion: nil)

    }

    

    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {

        self.dismiss(animated: true, completion: nil)

    }

    

    func myAlert(_ title: String, message: String){

        let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)

        let action = UIAlertAction(title: "ok", style: UIAlertActionStyle.default, handler: nil)

        alert.addAction(action)

        self.present(alert, animated: true, completion: nil)

        

    }


}






16장 코어 그래픽스로 화면에 그림 그리기




//

//  ViewController.swift

//  DrawGraphics

//

//  Created by stayfoolish on 22/10/2018.

//  Copyright © 2018 stayfoolish. 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, typically from a nib.

    }


    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

        // Dispose of any resources that can be recreated.

    }

    

    @IBAction func btnDrawLine(_ sender: UIButton){

        UIGraphicsBeginImageContext(imgView.frame.size)

        let context = UIGraphicsGetCurrentContext()!

        

        // Draw Line

        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()

        

        // Draw Triangle

        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()

        imgView.image = UIGraphicsGetImageFromCurrentImageContext()

        UIGraphicsEndImageContext()

    }


    @IBAction func btnDrawRectangle(_ sender: UIButton){

        UIGraphicsBeginImageContext(imgView.frame.size)

        let context = UIGraphicsGetCurrentContext()!

        

        // Draw Rectangle

        context.setLineWidth(2.0)

        context.setStrokeColor(UIColor.red.cgColor)

        

        context.addRect(CGRect(x: 50, y: 100, width: 200, height: 200))

        context.strokePath()

        

        imgView.image = UIGraphicsGetImageFromCurrentImageContext()

        UIGraphicsEndImageContext()

    }

    

    

    @IBAction func btnDrawCircle(_ sender: UIButton){

        UIGraphicsBeginImageContext(imgView.frame.size)

        let context = UIGraphicsGetCurrentContext()!

        

        // Draw Ellipse

        context.setLineWidth(2.0)

        context.setStrokeColor(UIColor.red.cgColor)

        context.addEllipse(in: CGRect(x: 50, y: 50, width: 200, height: 100))

        context.strokePath()

        

        // Draw Circle

        context.setLineWidth(5.0)

        context.setStrokeColor(UIColor.green.cgColor)

        

        context.addEllipse(in: CGRect(x: 50, y: 200, width: 200, height: 200))

        context.strokePath()

        

        imgView.image = UIGraphicsGetImageFromCurrentImageContext()

        UIGraphicsEndImageContext()

        

    }

    @IBAction func btnDrawArc(_ sender: UIButton){

        UIGraphicsBeginImageContext(imgView.frame.size)

        let context = UIGraphicsGetCurrentContext()!

        

        // Draw Arc

        context.setLineWidth(5.0)

        context.setStrokeColor(UIColor.red.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()

        

        imgView.image = UIGraphicsGetImageFromCurrentImageContext()

        UIGraphicsEndImageContext()

    }

    @IBAction func btnDrawFill(_ sender: UIButton){

        UIGraphicsBeginImageContext(imgView.frame.size)

        let context = UIGraphicsGetCurrentContext()!

        

        // Draw Rectangle

        context.setLineWidth(1.0)

        context.setStrokeColor(UIColor.red.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(1.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(1.0)

        context.setStrokeColor(UIColor.green.cgColor)

        context.setFillColor(UIColor.green.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()

        

        imgView.image = UIGraphicsGetImageFromCurrentImageContext()

        UIGraphicsEndImageContext()

        

    }

}







+ Recent posts