UIGestureRecognizer+QMUI.m 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /**
  2. * Tencent is pleased to support the open source community by making QMUI_iOS available.
  3. * Copyright (C) 2016-2021 THL A29 Limited, a Tencent company. All rights reserved.
  4. * Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
  5. * http://opensource.org/licenses/MIT
  6. * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
  7. */
  8. //
  9. // UIGestureRecognizer+QMUI.m
  10. // qmui
  11. //
  12. // Created by QMUI Team on 2017/8/21.
  13. //
  14. #import "UIGestureRecognizer+QMUI.h"
  15. #import "QMUICore.h"
  16. #import "UIView+QMUI.h"
  17. @implementation UIGestureRecognizer (QMUI)
  18. + (void)load {
  19. static dispatch_once_t onceToken;
  20. dispatch_once(&onceToken, ^{
  21. OverrideImplementation([UIGestureRecognizer class], @selector(setEnabled:), ^id(__unsafe_unretained Class originClass, SEL originCMD, IMP (^originalIMPProvider)(void)) {
  22. return ^(UIGestureRecognizer *selfObject, BOOL firstArgv) {
  23. // 检测常见的错误,例如在 viewWillAppear: 里把系统手势返回禁用,会导致从下一个界面手势返回到当前界面的瞬间,手势返回无效,界面处于混乱状态,无法接受任何点击事件
  24. // _UIParallaxTransitionPanGestureRecognizer
  25. if ([NSStringFromClass(selfObject.class) containsString:@"_UIParallaxTransition"] && selfObject.enabled && !firstArgv && (selfObject.state == UIGestureRecognizerStateBegan || selfObject.state == UIGestureRecognizerStateChanged)) {
  26. NSString *desc = @"disabling interactivePopGestureRecognizer during its execution may lead to interface state inconsistency!";
  27. UINavigationController *navController = selfObject.view.qmui_viewController;
  28. if ([navController isKindOfClass:UINavigationController.class]) {
  29. UIViewController *fromVc = [navController.transitionCoordinator viewControllerForKey:UITransitionContextFromViewControllerKey];
  30. UIViewController *toVc = [navController.transitionCoordinator viewControllerForKey:UITransitionContextToViewControllerKey];
  31. if (fromVc || toVc) {
  32. desc = [NSString stringWithFormat:@"%@ fromVc: %@, toVc: %@", desc, NSStringFromClass(fromVc.class), NSStringFromClass(toVc.class)];
  33. }
  34. }
  35. QMUIAssert(NO, @"UIGestureRecognizer (QMUI)", @"%@", desc);
  36. }
  37. // call super
  38. void (*originSelectorIMP)(id, SEL, BOOL);
  39. originSelectorIMP = (void (*)(id, SEL, BOOL))originalIMPProvider();
  40. originSelectorIMP(selfObject, originCMD, firstArgv);
  41. };
  42. });
  43. });
  44. }
  45. - (nullable UIView *)qmui_targetView {
  46. CGPoint location = [self locationInView:self.view];
  47. UIView *targetView = [self.view hitTest:location withEvent:nil];
  48. return targetView;
  49. }
  50. @end