스와이프를 활용하는 방법 예제를 만들어 보았다.
import UIKit
class ViewController: UIViewController {
let numOfTouchs = 2
@IBOutlet var imgViewUp: UIImageView!
@IBOutlet var imgViewRight: UIImageView!
@IBOutlet var imgViewLeft: UIImageView!
@IBOutlet var imgViewDown: 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.
//배열에 할당
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 = UISwipeGestureRecognizer.Direction.up //위쪽 방향으로 스와이프 했을 때 속성
self.view.addGestureRecognizer(swipeUp) //제스쳐 등록
let swipeDown = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.respondToSwipeGesture(_:)))
swipeDown.direction = UISwipeGestureRecognizer.Direction.down
self.view.addGestureRecognizer(swipeDown)
let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.respondToSwipeGesture(_:)))
swipeLeft.direction = UISwipeGestureRecognizer.Direction.left
self.view.addGestureRecognizer(swipeLeft)
let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.respondToSwipeGesture(_:)))
swipeRight.direction = UISwipeGestureRecognizer.Direction.right
self.view.addGestureRecognizer(swipeRight)
let swipeUpMulti = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.respondToSwipeGestureMulti(_:)))
swipeUpMulti.direction = UISwipeGestureRecognizer.Direction.up
swipeUpMulti.numberOfTouchesRequired = numOfTouchs
self.view.addGestureRecognizer(swipeUpMulti)
let swipeDownMulti = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.respondToSwipeGestureMulti(_:)))
swipeDownMulti.direction = UISwipeGestureRecognizer.Direction.down
swipeDownMulti.numberOfTouchesRequired = numOfTouchs //두개를 눌렀을 때
self.view.addGestureRecognizer(swipeDownMulti)
let swipeLeftMulti = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.respondToSwipeGestureMulti(_:)))
swipeLeftMulti.direction = UISwipeGestureRecognizer.Direction.left
swipeLeftMulti.numberOfTouchesRequired = numOfTouchs
self.view.addGestureRecognizer(swipeLeftMulti)
let swipeRightMulti = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.respondToSwipeGestureMulti(_:)))
swipeRightMulti.direction = UISwipeGestureRecognizer.Direction.right
swipeRightMulti.numberOfTouchesRequired = numOfTouchs
self.view.addGestureRecognizer(swipeRightMulti)
}
//한 터치로 스와이프 했을 때 이미지 할당
@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 UISwipeGestureRecognizer.Direction.up:
imgViewUp.image = imgUp[1]
case UISwipeGestureRecognizer.Direction.down:
imgViewDown.image = imgDown[1]
case UISwipeGestureRecognizer.Direction.left:
imgViewLeft.image = imgLeft[1]
case UISwipeGestureRecognizer.Direction.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 UISwipeGestureRecognizer.Direction.up:
imgViewUp.image = imgUp[2]
case UISwipeGestureRecognizer.Direction.down:
imgViewDown.image = imgDown[2]
case UISwipeGestureRecognizer.Direction.left:
imgViewLeft.image = imgLeft[2]
case UISwipeGestureRecognizer.Direction.right:
imgViewRight.image = imgRight[2]
default:
break
}
}
}
}
반복되는 구문이 많아 캡슐화를 시도했지만, swift에 대한 이해력이 부족해 실패했다...차후에 수정해야겠다.