3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[iOS13]UITextViewDelegateがTouchDownで反応してしまう

Last updated at Posted at 2019-10-24

何が問題か

iOS13からテキストのリンクなどに用いられるUITextViewDelegatetextViewメソッド
iOS12まではTouchUpで反応していたもののiOS13からなぜかTouchDownで反応してしまうという問題がありました.
参考記事

どう解決するか

  • iOS13の不具合(?)の修正を待つ
  • textViewメソッドのtextViewインスタンスからタッチイベントを取得する
  • そもそもtextViewDelegateメソッドを使用しない

以上の3つがあると思います.

iOS13の不具合修正を待つ

いつAppleが修正を行うのかは不明瞭で今の所この修正はベータ版にも含まれていないようなので,これは懸命な解決法ではなさそうです.

textViewメソッドのtextViewインスタンスからタッチイベントを取得する

以下のtextViewメソッドから受け取れるtextViewのタッチイベントを取得する方法です.

.swift
func textView(_ textView: UITextView,
                  shouldInteractWith URL: URL,
                  in characterRange: NSRange,
                  interaction: UITextItemInteraction) -> Bool

以下のようにまずinteractionで処理をわけ,textViewのタップイベント処理が終わった時に何かの処理をさせることで擬似的にTouchUpの挙動と同じになるようにしています.出典はstackOverflowです.

.swift
func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {

    switch interaction {
    case .invokeDefaultAction:
        if textView.gestureRecognizers?.contains(where: {$0.isKind(of: UITapGestureRecognizer.self) && $0.state == .ended}) == true {

            // Handle your custom logic here.

            return false
        }
        return true
    case .presentActions:

        // Default action.

        return true
    case .preview:

        // Default action.

        return true
    @unknown default:
        fatalError()
    }
}

そもそもUITextViewDelegateを使用しない

そもそもiOS13側の不具合である可能性が高く,いつ対応がされるのかがわからないため上記のtextViewのタッチイベントを拾ってtouchUpのように振る舞わさせるのは少し冗長な気もします.
iOS13からURLなどを長押しした際のOS側の挙動が変わっている(以前までのアクションシートが起動するだけのものから変更されてプレビューが表示されるなどOS準拠の挙動がリッチになってきている)こともあり,あえて自分でそのさきを処理せず,OS準拠の挙動に合わせるということもありなのではないかなと思います.

参考資料

3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?