Skip to content

Instantly share code, notes, and snippets.

@KentarouKanno
Last active May 12, 2018 13:57
Show Gist options
  • Save KentarouKanno/a89d51c1d7945b813ca44df0fd338f0e to your computer and use it in GitHub Desktop.
Save KentarouKanno/a89d51c1d7945b813ca44df0fd338f0e to your computer and use it in GitHub Desktop.
UIResponder

UIResponder

// 画面に指が触れた時
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    super.touchesBegan(touches, with: event)
    // 次のViewにタッチイベントを渡す時
    next?.touchesBegan(touches, with: event)
}

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    super.touchesMoved(touches, with: event)
    next?.touchesMoved(touches, with: event)
}

// 画面から指が離れた時
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    super.touchesEnded(touches, with: event)
    next?.touchesEnded(touches, with: event)
}

override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
    super.touchesCancelled(touches, with: event)
    next?.touchesCancelled(touches, with: event)
}

★ タッチした場所にあるViewをタグにより識別する

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    let touchEvent = touches.first!
    print(touchEvent.locationInView(self.view))
    
    let tag = touchEvent.view!.tag
    switch tag {
    case 999:
        print("tapped")
    default:
        break
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment