Pan Gesture-2の使用



  • Pan Gestureの使用-画面を離れるときに再び戻る設定と移動軸の制限部分を追加します.

  • ViewController.swift
  • //
    //  ViewController.swift
    //  panGesture
    //
    //  Created by yoon-yeoungjin on 2022/03/26.
    //
    
    import UIKit
    
    /*
      제한할 축 enum 선언
     */
    enum dtype{
        case x
        case y
        case none
    }
    
    class ViewController: UIViewController {
        
        var dragTyep = dtype.none
        let myView = DraggbleView()
        
        override func viewDidLoad() {
            super.viewDidLoad()
            myView.center = self.view.center
            myView.bounds = CGRect(x: 0, y: 0, width: 100, height: 100)
            myView.backgroundColor = .red
            self.view.addSubview(myView)
            // Do any additional setup after loading the view.
        }
        
        /*
         segmentedControl 스위치에 따라서 어떤 축을 제한할 건지 셋팅하고 이를 draggbleView 클래스에 전달한다. 
         */
        @IBAction func selectPanType(_ sender: UISegmentedControl) {
            switch sender.selectedSegmentIndex {
            case 0:
                dragTyep = .x
            case 1:
                dragTyep = .y
            case 2:
                dragTyep = .none
            default:
                break
            }
            
            myView.dragType = self.dragTyep
        }
        
    }
    
  • DraggbleView.swift
  • //
    //  DraggbleView.swift
    //  panGesture
    //
    //  Created by yoon-yeoungjin on 2022/03/26.
    //
    
    import Foundation
    import UIKit
    
    class DraggbleView: UIView {
        
        var dragType = dtype.none
        
        override init(frame: CGRect) {
            super.init(frame: CGRect.zero)
            let pan = UIPanGestureRecognizer(target: self, action: #selector(dragging))
            self.addGestureRecognizer(pan)
        }
        
        required init?(coder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
        
        @objc func dragging(pan: UIPanGestureRecognizer){
            switch pan.state {
            case .began:
                print("began pan gesture")
            case .changed:
                let delta = pan.translation(in: self.superview)
                var myPosition = self.center
                
                /**
                 제한한 축만 움직힐 수 있도록 셋팅
                 */
                if dragType == .x {
                    myPosition.x += delta.x
                } else if dragType == .y {
                    myPosition.y += delta.y
                } else if dragType == .none {
                    myPosition.x += delta.x
                    myPosition.y += delta.y
                } else {
                    print("[ERROR] none dtype")
                    exit(1)
                }
                print("x : ", myPosition.x, " y: ", myPosition.y)
                self.center = myPosition
                pan.setTranslation(CGPoint.zero, in: self.superview)
            case .ended, .cancelled:
                print("end pan gesture")
                
                /**
                 화면 밖을 넘어가면 다시 돌아오게 셋팅
                 */
                if self.frame.minX < 0 {
                    self.frame.origin.x = 0
                }
                if self.frame.maxX > (self.superview?.frame.maxX ?? 0) {
                    if let hasSuperView = self.superview {
                        self.frame.origin.x = hasSuperView.frame.maxX - self.bounds.width
                    }
                }
                
                if self.frame.minY < 126 {
                    self.frame.origin.y = 126
                }
                if self.frame.maxY > (self.superview?.frame.maxY ?? 0) {
                    if let hasSuperViewY = self.superview {
                        self.frame.origin.y = hasSuperViewY.frame.maxY - self.bounds.width
                    }
                }
            @unknown default:
                print("err")
            }
        }
    }
  • 駆動画面