From eddee5ede99dc11f27803d76f269f48b28675c4e Mon Sep 17 00:00:00 2001 From: nixzhu Date: Sat, 19 Sep 2015 00:16:00 +0800 Subject: [PATCH] refactor isOperatingSystemAtLeastMajorVersion; workaround of longPress for menu or link --- Yep/Configs/YepHelper.swift | 4 ++++ .../Cells/ChatLeftText/ChatLeftTextCell.swift | 15 ++++++++++++++- .../Cells/ChatRightText/ChatRightTextCell.swift | 15 ++++++++++++++- Yep/Views/TextViews/ChatTextView.swift | 10 ++++++---- 4 files changed, 38 insertions(+), 6 deletions(-) diff --git a/Yep/Configs/YepHelper.swift b/Yep/Configs/YepHelper.swift index e8bc3468..bfbf04f3 100644 --- a/Yep/Configs/YepHelper.swift +++ b/Yep/Configs/YepHelper.swift @@ -78,3 +78,7 @@ func cleanRealmAndCaches() { NSNotificationCenter.defaultCenter().postNotificationName(EditProfileViewController.Notification.Logout, object: nil) } +func isOperatingSystemAtLeastMajorVersion(majorVersion: Int) -> Bool { + return NSProcessInfo().isOperatingSystemAtLeastVersion(NSOperatingSystemVersion(majorVersion: majorVersion, minorVersion: 0, patchVersion: 0)) +} + diff --git a/Yep/Views/Cells/ChatLeftText/ChatLeftTextCell.swift b/Yep/Views/Cells/ChatLeftText/ChatLeftTextCell.swift index e1d5995d..1e2beb7a 100644 --- a/Yep/Views/Cells/ChatLeftText/ChatLeftTextCell.swift +++ b/Yep/Views/Cells/ChatLeftText/ChatLeftTextCell.swift @@ -116,7 +116,20 @@ extension ChatLeftTextCell: UIGestureRecognizerDelegate { func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRequireFailureOfGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool { - return false + // iOS 9 在长按链接时不弹出 menu + + if isOperatingSystemAtLeastMajorVersion(9) { + + if let longPressGestureRecognizer = otherGestureRecognizer as? UILongPressGestureRecognizer { + if longPressGestureRecognizer.minimumPressDuration == 0.75 { + return true + } + } + + return false + } + + return true } } diff --git a/Yep/Views/Cells/ChatRightText/ChatRightTextCell.swift b/Yep/Views/Cells/ChatRightText/ChatRightTextCell.swift index aa3ec6b6..4a7b3e1e 100644 --- a/Yep/Views/Cells/ChatRightText/ChatRightTextCell.swift +++ b/Yep/Views/Cells/ChatRightText/ChatRightTextCell.swift @@ -139,7 +139,20 @@ extension ChatRightTextCell: UIGestureRecognizerDelegate { func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRequireFailureOfGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool { - return false + // iOS 9 在长按链接时不弹出 menu + + if isOperatingSystemAtLeastMajorVersion(9) { + + if let longPressGestureRecognizer = otherGestureRecognizer as? UILongPressGestureRecognizer { + if longPressGestureRecognizer.minimumPressDuration == 0.75 { + return true + } + } + + return false + } + + return true } } diff --git a/Yep/Views/TextViews/ChatTextView.swift b/Yep/Views/TextViews/ChatTextView.swift index d7164e70..49fdbf73 100644 --- a/Yep/Views/TextViews/ChatTextView.swift +++ b/Yep/Views/TextViews/ChatTextView.swift @@ -10,17 +10,19 @@ import UIKit class ChatTextView: UITextView { - var menuLongPressGestureRecognizer: UILongPressGestureRecognizer! - override func canBecomeFirstResponder() -> Bool { return false } override func addGestureRecognizer(gestureRecognizer: UIGestureRecognizer) { - if NSProcessInfo().isOperatingSystemAtLeastVersion(NSOperatingSystemVersion(majorVersion: 9, minorVersion: 0, patchVersion: 0)) { + // iOS 9 以上,强制不添加文字选择长按手势,免去触发选择文字 + // 共有四种长按手势,iOS 9 正式版里分别加了两次:0.1 Reveal,0.12 tap link,0.5 selection, 0.75 press link + if isOperatingSystemAtLeastMajorVersion(9) { if let longPressGestureRecognizer = gestureRecognizer as? UILongPressGestureRecognizer { - return + if longPressGestureRecognizer.minimumPressDuration == 0.5 { + return + } } }