ios 에서 사용자 입력 ( 터치 스크린) 에 관한 메소드를 정리한다.




멀티터치를 사용하기 위해서는 storyboard의 view 를 클릭하고 아래 사진처럼 Multiple Touch 박스에 체크해줘야 한다.






유저가 화면을 터치했을 때 최초 호출되는 touchesBegan 메소드 이다.  메소드 안의 코드는 탭을 몇번 했는지, 몇개의 손가락으로 터치를 했는지를 확인 할 수 있으며 클릭한 화면의 좌표를 보여주는 예제이다. (화면에 표시할 레이블을 생성하고 컨트롤러에 연결하는 과정은 생략한다.)


var startPoint : CGPoint? // 유저가 처음으로 터치하면 위치정보를 변수에 저장하여 좌표를 표시.


override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {

        let touchCount = touches.count

        let touch = touches.first

        let tapCount = touch!.tapCount

        

        methodStatus.text = "touchesBegan"

        touchStatus.text = "\(touchCount) touches"

        tapStatus.text = "\(tapCount) taps"


if let theTouch = touches.first {

            startPoint = theTouch.locationInView(self.view)

            let x = startPoint!.x

            let y = startPoint!.y

            xCoord.text = ("x = \(x)")

            yCoord.text = ("y = \(y)")

        }

 }



유저가 화면을 터치하고 움직이면 움직임이 멈춰질때까지 touchesMoved 메소드가 호출된다. 그리고 유저가 손가락을 떼면 touchesEnded 메소드가 호출된다.

아래 메소드 안의 코드는 움직임과 멈춤을 화면에 표시하고 화면에 움직이는 동안의 좌표를 보이도록 하는 예제이다.


override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {

        let touchCount = touches.count

        let touch = touches.first

        let tapCount = touch!.tapCount

        

        methodStatus.text = "touchesMoved"

        touchStatus.text = "\(touchCount) touches"

        tapStatus.text = "\(tapCount) taps"

        

        if let theTouch = touches.first {

            let touchLocation = theTouch.locationInView(self.view)

            let x = touchLocation.x

            let y = touchLocation.y

            xCoord.text = ("x = \(x)")

            yCoord.text = ("y = \(y)")

        }

 }



아래는 touchesEnded 메소드가 호출되었을 때 탭카운트, 몇개의 멀티터치인지 확인하고 화면에 좌표를 보여주기  위한 예제이다.


override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {

        let touchCount = touches.count

        let touch = touches.first

        let tapCount = touch!.tapCount

        

        methodStatus.text = "touchesEnded"

        touchStatus.text = "\(touchCount) touches"

        tapStatus.text = "\(tapCount) taps"


if let theTouch = touches.first {

            let endPoint = theTouch.locationInView(self.view)

            let x = endPoint.x

            let y = endPoint.y

            xCoord.text = ("x = \(x)")

            yCoord.text = ("y = \(y)")

        }

 }






제스처 ( swipe, pinch, rotation, long press) 를 사용하기 위해서는 아래 사진과 같이 뷰에 Gesture Recognizer 오브젝트를 드래그 앤 드롭하여 넣어야 하며 컨트롤러에 Action을 주고 type을 변경하여 연결해야 한다.







아래 코드는 각각의 제스처를 뷰에 등록하고 컨트롤러 파일에 연결한 후 화면에 간단히 표시해주기 위해 작성한 예제이다.

tapDetected 는 Tap Gesture Recognizer 오브젝트의 Attributes inspector 에서  Taps를 2로 바꾸어 2번 이상 탭했을때만 인식되도록 하였으며,

swipeDetected 가 2개 있는 이유는 Swipe Gesuter Recognizer 는 위 아래 왼쪽 오른쪽 중 한가지 방향만 정하여 설정할 수 있어 2개의 오브젝트를 넣고 Attributes inspector 에서 왼쪽 하나 오른쪽 하나 설정하였기 때문이다.


class ViewController: UIViewController {

    

    @IBOutlet var statusLabel: UILabel!

    

    override func viewDidLoad() {

        super.viewDidLoad()

        // Do any additional setup after loading the view, typically from a nib.

    }

    

    @IBAction func tapDetected(sender: UITapGestureRecognizer) {

        statusLabel.text = "Double Tap"

    }

   

    @IBAction func pinchDetected(sender: UIPinchGestureRecognizer) {

        let scale = sender.scale

        let velocity = sender.velocity

        let resultString = "Pinch - scale = \(scale), velocity = \(velocity)"

        statusLabel.text = resultString

    }


    @IBAction func rotationDetected(sender: UIRotationGestureRecognizer) {

        let radians = sender.rotation

        let velocity = sender.velocity

        let resultString = "Rotation - Radians = \(radians), velocity =  \(velocity)"

        statusLabel.text = resultString

    }

    

    @IBAction func swipeDetected(sender: UISwipeGestureRecognizer) {

        statusLabel.text = "Right swipe"

    }

    

    @IBAction func swipe2Detecte(sender: UISwipeGestureRecognizer) {

        statusLabel.text = "Left swipe"

    }

    

    @IBAction func longPressDetected(sender: UILongPressGestureRecognizer) {

        statusLabel.text = "Long Press"

    }

    

    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

        // Dispose of any resources that can be recreated.

    }

}


+ Recent posts