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

            }

        }

    }

    

    

}







+ Recent posts