GDTSplashZoomOutView+GDTDraggable.m 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. //
  2. // GDTSplashZoomOutView+GDTDraggable.m
  3. // GDTMobApp
  4. //
  5. // Created by nimomeng on 2020/11/18.
  6. // Copyright © 2020 Tencent. All rights reserved.
  7. //
  8. #import "GDTSplashZoomOutView+GDTDraggable.h"
  9. @implementation GDTSplashZoomOutView (GDTDraggable)
  10. - (void)supportDrag
  11. {
  12. UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
  13. [self addGestureRecognizer:pan];
  14. }
  15. - (void)pan:(UIPanGestureRecognizer *)pan {
  16. CGPoint offset = [pan translationInView:self];
  17. [pan setTranslation:CGPointZero inView:self];
  18. // 确定ZoomOutView的位置
  19. CGFloat targetX = CGRectGetMidX(self.frame) + offset.x;
  20. CGFloat targetY = CGRectGetMidY(self.frame) + offset.y;
  21. CGFloat margin = 12;
  22. CGFloat widthLimitation = CGRectGetWidth(self.frame) + margin;
  23. CGFloat heightLimitation = CGRectGetHeight(self.frame) + margin;
  24. switch (pan.state) {
  25. case UIGestureRecognizerStateBegan:
  26. case UIGestureRecognizerStateChanged:
  27. {
  28. // ZoomOutView随手指拖动变化位置
  29. self.center = CGPointMake(targetX, targetY);
  30. }
  31. break;
  32. case UIGestureRecognizerStateEnded:
  33. case UIGestureRecognizerStateCancelled:
  34. {
  35. BOOL needAnimation;
  36. // 处理拖动到边界的情况
  37. if (targetX < widthLimitation / 2) {
  38. targetX = widthLimitation / 2;
  39. needAnimation = YES;
  40. }
  41. if (targetX > [UIScreen mainScreen].bounds.size.width - widthLimitation / 2) {
  42. targetX = [UIScreen mainScreen].bounds.size.width - widthLimitation / 2;
  43. needAnimation = YES;
  44. }
  45. if (targetY < heightLimitation / 2) {
  46. targetY = heightLimitation / 2;
  47. needAnimation = YES;
  48. }
  49. if (targetY > [UIScreen mainScreen].bounds.size.height - heightLimitation / 2) {
  50. targetY = [UIScreen mainScreen].bounds.size.height - heightLimitation / 2;
  51. needAnimation = YES;
  52. }
  53. // 给一个bounds的动画
  54. if (needAnimation) {
  55. [UIView animateWithDuration:0.3 animations:^{
  56. self.center = CGPointMake(targetX, targetY);
  57. }];
  58. }
  59. }
  60. default:
  61. break;
  62. }
  63. }
  64. @end