22 ios 스위프트 스와이프 기능 구현
//
// ViewController.swift
// SwipeGestuer
//
// Created by MacBookPro on 2017. 11. 30..
// Copyright © 2017년 MacBookPro. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
let numberOfTouch = 2
@IBOutlet weak var imgVIewUp: UIImageView!
@IBOutlet weak var imgVIewLeft: UIImageView!
@IBOutlet weak var imgVIewRight: UIImageView!
@IBOutlet weak var imgVIewDown: UIImageView!
//이미지 배열 선언하기
//각 방향별로 검은색과 빨간색 이미지를 저장하기 위해 다음과 같이 배열을 선언한다.
var imgLeft = [UIImage]()
var imgRigth = [UIImage]()
var imgUp = [UIImage]()
var imgDown = [UIImage]()
//이미지 배열에 이미지 할당 하기
override func viewDidLoad() {
super.viewDidLoad()
//배열을 uiimage형으로 만들었기 때문에 append의 인수로 uiimage형의 값을 입력한다.
//스와이프하면 검은색 화살표가 빨간색이 되도록 하기 위해 처음에는 검은색 화살표의 파일명,
//그 뒤에는 빨간색 화살표의 파일명을 입력
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")!)
imgRigth.append(UIImage(named:"arrow-right-black.png")!)
imgRigth.append(UIImage(named:"arrow-right-red.png")!)
imgRigth.append(UIImage(named:"arrow-right-green.png")!)
//위에서 할당한 각 이미지 배열의 첫 번째 인자를 이미지 뷰에 할당
imgVIewUp.image = imgUp[0]
imgVIewDown.image = imgDown[0]
imgVIewLeft.image = imgLeft[0]
imgVIewRight.image = imgRigth[0]
//* UISwipeGestureRecognizer 클래스 상수를 선언한다. 액션 인수는 해당 스와이프 제스처를 실행했을 때 실행할 메서드를 의미 한다.
let swipeUp = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.respondToSwipeGesture(_:)))
//위에서 선언한 클래스 상수의 direction의 방형을 up으로 설정한다.
swipeUp.direction = UISwipeGestureRecognizerDirection.up
//몇 손가락으로 터치를 할것인지 정하기 2손가락으로
//swipeUp.numberOfTouchesRequired = numberOfTouch
//뷰 객체의 addGesture~~ 메서드를 이용해서 위쪽 방향의 스와이프 제스처를 등록 한다.
self.view.addGestureRecognizer(swipeUp)
//*아래
let swipeDown = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.respondToSwipeGesture(_:)))
swipeDown.direction = UISwipeGestureRecognizerDirection.down
// swipeDown.numberOfTouchesRequired = numberOfTouch
self.view.addGestureRecognizer(swipeDown)
//*왼쪽
let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.respondToSwipeGesture(_:)))
swipeLeft.direction = UISwipeGestureRecognizerDirection.left
// swipeLeft.numberOfTouchesRequired = numberOfTouch
self.view.addGestureRecognizer(swipeLeft)
//*오른쪽
let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.respondToSwipeGesture(_:)))
swipeRight.direction = UISwipeGestureRecognizerDirection.right
// swipeRight.numberOfTouchesRequired = numberOfTouch
self.view.addGestureRecognizer(swipeRight)
//------------------ 멀티 스와이프 구현하기 ------------------
let swipeUpMulti = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.respondToSwipeGestureMulti(_:)))
swipeUpMulti.direction = UISwipeGestureRecognizerDirection.up
//몇 손가락으로 터치를 할것인지 정하기 2손가락으로
swipeUpMulti.numberOfTouchesRequired = numberOfTouch
self.view.addGestureRecognizer(swipeUpMulti)
let swipeDownMulti = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.respondToSwipeGestureMulti(_:)))
swipeDownMulti.direction = UISwipeGestureRecognizerDirection.down
swipeDownMulti.numberOfTouchesRequired = numberOfTouch
self.view.addGestureRecognizer(swipeDownMulti)
let swipeLeftMulti = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.respondToSwipeGestureMulti(_:)))
swipeLeftMulti.direction = UISwipeGestureRecognizerDirection.left
swipeLeftMulti.numberOfTouchesRequired = numberOfTouch
self.view.addGestureRecognizer(swipeLeftMulti)
let swipeRightMulti = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.respondToSwipeGestureMulti(_:)))
swipeRightMulti.direction = UISwipeGestureRecognizerDirection.right
swipeRightMulti.numberOfTouchesRequired = numberOfTouch
self.view.addGestureRecognizer(swipeRightMulti)
}
//멀티 스와이프 액션 함수
@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 = imgRigth[0]
switch swipeGesture.direction {
case UISwipeGestureRecognizerDirection.up:
imgVIewUp.image = imgUp[2]
case UISwipeGestureRecognizerDirection.down:
imgVIewDown.image = imgDown[2]
case UISwipeGestureRecognizerDirection.up:
imgVIewLeft.image = imgLeft[2]
case UISwipeGestureRecognizerDirection.up:
imgVIewRight.image = imgRigth[2]
default: break
}
}
}
//스와이프 제스처를 등록할 때 입력한 액션인수는 스와이프 제스쳐를 행했을 때 실행할 메서드를 의미한다.
@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 = imgRigth[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 = imgRigth[1]
default: break
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
'ios 뽀개기 > ios앱' 카테고리의 다른 글
24 ios 스위프트 핀치기능으로 이미지 확대 축소하기 (0) | 2017.12.02 |
---|---|
23 ios 스위프트 핀치기능으로 문자 확대 축소하기 (0) | 2017.12.01 |
21 ios 스위프트 그림그리기 기능 구현 (1) | 2017.11.30 |
20 ios 스위프트 터치 기능 (0) | 2017.11.30 |
19 ios 스위프트 그림그리기 선 원 호 구현 (0) | 2017.11.29 |
댓글