728x90
반응형
//
// ViewController.swift
// SwipeGuestureQ
//
// Created by Leo_Jang on 27/08/2020.
// Copyright © 2020 Leo_Jang. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var imgView: UIImageView!
@IBOutlet weak var pageControl: UIPageControl!
//왼쪽, 오른쪽만 사용
var images = ["flower_01.png","flower_02.png","flower_03.png","flower_04.png","flower_05.png","flower_06.png"]
let numberofTouches = 2
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
// PageControl 설정 값
pageControl.numberOfPages = images.count
pageControl.currentPage = 0
pageControl.pageIndicatorTintColor = UIColor.green
pageControl.currentPageIndicatorTintColor = UIColor.blue
imgView.image = UIImage(named: images[0])
// 한 손가락 Guesture
// 오른쪽
let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.responseToSwipeGesture(_ :)))
swipeRight.direction = UISwipeGestureRecognizer.Direction.right
self.view.addGestureRecognizer(swipeRight)
// 왼쪽
let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.responseToSwipeGesture(_:)))
swipeLeft.direction = UISwipeGestureRecognizer.Direction.left
self.view.addGestureRecognizer(swipeLeft)
// 두 손가락 Guesture
// 오른쪽
let swipeRightMulti = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.responseToSwipeGestureMulti(_:)))
swipeRightMulti.direction = UISwipeGestureRecognizer.Direction.right
swipeRightMulti.numberOfTouchesRequired = numberofTouches
self.view.addGestureRecognizer(swipeRightMulti)
// 왼쪽
let swipeLeftMulti = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.responseToSwipeGestureMulti(_:)))
swipeLeftMulti.direction = UISwipeGestureRecognizer.Direction.left
swipeLeftMulti.numberOfTouchesRequired = numberofTouches
self.view.addGestureRecognizer(swipeLeftMulti)
}
// 한 손가락 Guesture
@objc func responseToSwipeGesture(_ gesture: UISwipeGestureRecognizer){
if let swipeGesture = gesture as? UISwipeGestureRecognizer{
// 발생한 이벤트가 각 방향의 스와이프 이벤트라면
// pageControl이 가르키는 현재 페이지에 해당하는 이미지를 imageView에 할당
switch swipeGesture.direction{
case UISwipeGestureRecognizer.Direction.right:
pageControl.currentPage -= 1
imgView.image = UIImage(named: images[pageControl.currentPage])
case UISwipeGestureRecognizer.Direction.left:
pageControl.currentPage += 1
imgView.image = UIImage(named: images[pageControl.currentPage])
default:
break
}
}
}
// 두 손가락 Guesture
@objc func responseToSwipeGestureMulti(_ gesture: UISwipeGestureRecognizer){
if let swipeGesture = gesture as? UISwipeGestureRecognizer{
// 발생한 이벤트가 각 방향의 스와이프 이벤트라면
// pageControl이 가르키는 현재 페이지에 해당하는 이미지를 imageView에 할당
switch swipeGesture.direction{
case UISwipeGestureRecognizer.Direction.right: // UISwipeGestureRecognizer.방향. 오른쪽
pageControl.currentPage -= 1 // pageControl에서 시작하여. 현재페이지 부터 + 1을or배열에 +1 을한다.
imgView.image = UIImage(named: images[pageControl.currentPage]) // 배열안에 있는 이미지들 = UIImage(이름: var:imgages 배열안에 [pageControl에 있는.현재페이지] 즉 배열안에 있는 이미지들은 누르면 배열순서대로 이미지가 바뀐다.
case UISwipeGestureRecognizer.Direction.left:
pageControl.currentPage += 1
imgView.image = UIImage(named: images[pageControl.currentPage])
default:
break
}
}
}
// PageControl
@IBAction func imgChange(_ sender: UIPageControl) {
imgView.image = UIImage(named: images[pageControl.currentPage])
}
} // ------------------------
반응형
'개발자 코드(Code) > Swfit' 카테고리의 다른 글
IOS) Tab Bar Controller 4개 연결 및 작동(Button + Timer + PageControl) (0) | 2020.08.30 |
---|---|
IOS) PageControl + Map (PageControl + ImageView + Label) (0) | 2020.08.30 |
IOS) Pin Gesture_02 (이미지 확대) (0) | 2020.08.27 |
IOS) PinGuesture (글자 확대) (0) | 2020.08.27 |
IOS ) SwipeGuesture(스와이프 제스쳐) (0) | 2020.08.27 |