Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.24 KB | None | 0 0
  1. - (void)handlePanGesture:(UIPanGestureRecognizer *)gestureRecognizer
  2. {
  3. //UIView which is moved by the user
  4. MyImageView *currentView = gestureRecognizer.view;
  5.  
  6. switch (gestureRecognizer.state)
  7. {
  8. case UIGestureRecognizerStatePossible:
  9. {
  10. break;
  11. }
  12. case UIGestureRecognizerStateBegan:
  13. {
  14. //save both values in global instance variables
  15. currentFrameOriginX = currentView.frame.origin.x;
  16. currentFrameOriginY = currentView.frame.origin.y;
  17.  
  18. //global BOOL variable, to check if scrollView movement is performed
  19. scrolling = NO;
  20. break;
  21. }
  22.  
  23. case UIGestureRecognizerStateChanged:
  24. {
  25. CGRect rect = CGRectMake(currentFrameOriginX + [gestureRecognizer translationInView:currentView.superview].x, currentFrameOriginY + [gestureRecognizer translationInView:currentView.superview].y, currentView.frame.size.width, currentView.frame.size.height);
  26.  
  27. if (CGRectContainsRect(currentView.superview.frame, rect)) {
  28.  
  29. /*PROBLEM: Here is a problem. I need this change of the frame here, to move the UIView along the movement from the user. In my autoScroll-method I have to set the frame of currentView, too. But I can't set the frame of currentView here and in the autoScroll. But as long as the NSTimer runs and is calling autoScroll: this if-statement isn't called, so I can't move the UIView with my finger anymore. */
  30. if (!scrolling) {
  31. //currently the NSTimer method for the automatically scrolling isn't performed, so:
  32. //change the frame according to the pan gesture
  33. currentView.frame = rect;
  34. }
  35.  
  36. UIScrollView *scrollView = self.myScrollView; //reference to the "root" UIScrollView
  37. CGRect visibleRect;
  38. visibleRect.origin = scrollView.contentOffset;
  39. visibleRect.size = scrollView.bounds.size;
  40.  
  41. CGRect frame = currentView.frame;
  42.  
  43. CGFloat scale = 1.0 / scrollView.zoomScale;
  44. visibleRect.origin.x *= scale;
  45. visibleRect.origin.y *= scale;
  46. visibleRect.size.width *= scale;
  47. visibleRect.size.height *= scale;
  48.  
  49. CGSize scrollZone = CGSizeMake(10.0f, 10.0f);
  50. float scrollStep = 3.0f;
  51. CGPoint scrollAmount = CGPointZero;
  52.  
  53. //determine the change of x and y
  54. if (frame.origin.x+scrollZone.width < visibleRect.origin.x) {
  55. scrollAmount.x = -scrollStep;
  56. }
  57. else if((frame.origin.x+frame.size.width)-scrollZone.width > visibleRect.origin.x + visibleRect.size.width) {
  58. scrollAmount.x = scrollStep;
  59. }
  60. else if (frame.origin.y+scrollZone.height < visibleRect.origin.y) {
  61. scrollAmount.y = -scrollStep;
  62. }
  63. else if((frame.origin.y+frame.size.height)-scrollZone.height > visibleRect.origin.y + visibleRect.size.height) {
  64. scrollAmount.y = scrollStep;
  65. }
  66.  
  67. if ((scrollAmount.x != 0) | (scrollAmount.y != 0)) {
  68. if (![scrollTimer isValid]) {
  69. //scrollTimer is a global NSTimer instance variable
  70. [scrollTimer invalidate];
  71. scrollTimer = nil;
  72.  
  73. NSString *scrollString = NSStringFromCGPoint(scrollAmount);
  74. NSDictionary *info = [NSDictionary dictionaryWithObjectsAndKeys:scrollString, @"scrollString", currentView, @"currentView", nil];
  75.  
  76. scrollTimer = [[NSTimer alloc]initWithFireDate:[NSDate date] interval:0.03f target:self selector:@selector(autoScroll:) userInfo:info repeats:YES];
  77. [[NSRunLoop mainRunLoop] addTimer:scrollTimer forMode:NSRunLoopCommonModes];
  78. }
  79. }
  80. else {
  81. [scrollTimer invalidate];
  82. scrollTimer = nil;
  83. scrolling = NO;
  84. }
  85. }
  86. break;
  87. }
  88. case UIGestureRecognizerStateEnded:
  89. {
  90. //quite know the scrolling should stop, maybe it would be better when the scrollView scrolls even if the user does nothing when the subview is over the visible area
  91. [scrollTimer invalidate];
  92. scrollTimer = nil;
  93. scrolling = NO;
  94. break;
  95. }
  96. default:
  97. {
  98. [scrollTimer invalidate];
  99. scrollTimer = nil;
  100. scrolling = NO;
  101. break;
  102. }
  103. }
  104. }
  105.  
  106.  
  107. -(void)autoScroll:(NSTimer*)timer {
  108.  
  109. scrolling = YES; //the scroll method is executed quite know
  110.  
  111. NSDictionary *info = [timer userInfo];
  112.  
  113. UIScrollView *scrollView = self.myScrollView;
  114. CGRect visibleRect;
  115. visibleRect.origin = scrollView.contentOffset;
  116. visibleRect.size = scrollView.bounds.size;
  117.  
  118. CGPoint scrollAmount = CGPointFromString([info objectForKey:@"scrollString"]);
  119. MyImageView *currentView = [info objectForKey:@"currentView"];
  120.  
  121. //stop scrolling when the UIView is at the edge of the containerView (referenced over 'self')
  122. if ((currentView.frame.origin.x <= 0 | currentView.frame.origin.y <= 0) ||
  123. ((currentView.frame.origin.x+currentView.frame.size.width) > self.frame.size.width | (currentView.frame.origin.y+currentView.frame.size.height) > self.frame.size.height)
  124. ) {
  125. scrolling = NO;
  126. return;
  127. }
  128.  
  129. //move the UIView
  130. CGFloat scale = 1.0 / scrollView.zoomScale;
  131. if (scrollAmount.x != 0) {
  132. scrollAmount.x *= scale;
  133. }
  134. if (scrollAmount.y != 0) {
  135. scrollAmount.y *= scale;
  136. }
  137. CGRect frame = currentView.frame;
  138. frame.origin.x += scrollAmount.x;
  139. frame.origin.y += scrollAmount.y;
  140. currentView.frame = frame;
  141. currentFrameOriginX = currentView.frame.origin.x;
  142. currentFrameOriginY = currentView.frame.origin.y;
  143.  
  144. //move the scrollView
  145. CGPoint contentOffset = scrollView.contentOffset;
  146. contentOffset.x += scrollAmount.x;
  147. contentOffset.y += scrollAmount.y;
  148. [scrollView setContentOffset:contentOffset animated:NO];
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement