2012-05-24 11 views
1

我是來所以用了很長時間後一個問題,我沒有足夠的教訓還:(。照片應用程式,例如與手勢識別放大模式滾動視圖滾動

我有一個UIScrollView其中有UIImageViewUIImageView的圖像將隨着左右滑動而改變,我知道覆蓋UIScrollView的觸摸將不是一個好的和有效的解決方案,所以我在UIScrollView(不是子視圖)上添加了一個透明視圖,覆蓋UIScrollView的範圍並重寫其觸摸方法以改變圖像的滑動方式

現在隨着hel p的pinch gesture recognizer添加到我的透明視圖中我可以以編程方式縮放UIScrollView和圖像。直到這裏,我能夠順利地工作,但我有另一個典型的功能。我必須用手指上下移動放大的圖像,以便看到裁剪後的圖像。我在UILongPressGestureRecognizer的幫助下將其添加到我的透明視圖中。在1秒的觸摸持續時間後,我用一個四向箭頭圖像添加一個UIImageView並添加觸摸位置。現在當我移動手指時,fourwayarrow image會跟隨我的手指,但滾動視圖中的圖像不會跟隨我的手指。它的一種生澀,經常像我綁上一根彈性繩子一樣工作。因此,最初我必須將字符串拉到某個閾值(直到該點的滾動視圖不移動),並且當它達到該級別時,它突然開始失去控制。我在這裏添加代碼讓你看看。

如果您需要任何其他信息,請隨時添加評論。

- (void) handleLongPress:(UILongPressGestureRecognizer *)longPressGestureRecognizer{ 
    CGPoint touchLocation = [longPressGestureRecognizer locationInView:self.transparentLayer]; 
    switch (longPressGestureRecognizer.state) { 
     case UIGestureRecognizerStatePossible: break; 
     case UIGestureRecognizerStateBegan:{ 
      if (fourWayArrowImageView == nil) { 
       fourWayArrowImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"4wayarrow.png"]]; 
       [fourWayArrowImageView setBackgroundColor:[UIColor clearColor]]; 
      } 
      _previousLocation.x = touchLocation.x; 
      _previousLocation.y = touchLocation.y; 
      [fourWayArrowImageView setCenter:touchLocation]; 
      [self.transparentLayer addSubview:fourWayArrowImageView]; 
     } 
      break; 
     case UIGestureRecognizerStateChanged:{ 
      float dx = (touchLocation.x - _previousLocation.x); 
      float dy = (touchLocation.y - _previousLocation.y); 
      CGPoint currentMaxOffset = [self currentMaxOffset]; 
      CGPoint currentOffset = [self.rotatableImageViewContainingScrollView contentOffset]; 
      float theScale = [self.rotatableImageViewContainingScrollView zoomScale]; 
      currentOffset.x -= (dx*theScale); 
      currentOffset.y -= (dy*theScale); 

      [fourWayArrowImageView setCenter:touchLocation]; 
      CGSize size = self.rotatableImageViewContainingScrollView.frame.size; 
      [self.rotatableImageViewContainingScrollView scrollRectToVisible:CGRectMake(currentOffset.x, currentOffset.y, size.width, size.height) animated:YES]; 
      _prevMaxOffset = CGPointMake(currentMaxOffset.x, currentMaxOffset.y); 
     } 
      break; 
     case UIGestureRecognizerStateCancelled: 
     case UIGestureRecognizerStateFailed: 
     case UIGestureRecognizerStateEnded: [fourWayArrowImageView removeFromSuperview]; break; 
    } 
} 

回答

0

找到了解決方案。其實我犯了一個錯誤。在這種情況下,我並沒有在滾動後更新之前的位置,也不應該滾動滾動。所以請看看答案。順便說一句感謝誰給時間所有球員:

- (void) handleLongPress:(UILongPressGestureRecognizer *)longPressGestureRecognizer{ 
    CGPoint touchLocation = [longPressGestureRecognizer locationInView:self.transparentLayer]; 
    switch (longPressGestureRecognizer.state) { 
     case UIGestureRecognizerStatePossible: break; 
     case UIGestureRecognizerStateBegan:{ 
      if (fourWayArrowImageView == nil) { 
       fourWayArrowImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"4wayarrow.png"]]; 
       [fourWayArrowImageView setBackgroundColor:[UIColor clearColor]]; 
      } 
      _previousLocation = CGPointMake(touchLocation.x, touchLocation.y); 
      [fourWayArrowImageView setCenter:touchLocation]; 
      [self.transparentLayer addSubview:fourWayArrowImageView]; 
     } 
      break; 
     case UIGestureRecognizerStateChanged:{ 
      float dx = (touchLocation.x - _previousLocation.x); 
      float dy = (touchLocation.y - _previousLocation.y); 
      CGPoint currentMaxOffset = [self currentMaxOffset]; 
      CGPoint currentOffset = [self.rotatableImageViewContainingScrollView contentOffset]; 
      //float theScale = [self.rotatableImageViewContainingScrollView zoomScale]; 
      //currentOffset.x -= (dx*theScale); 
      //currentOffset.y -= (dy*theScale); 
      currentOffset.x -= (dx); 
      currentOffset.y -= (dy); 
      [fourWayArrowImageView setCenter:touchLocation]; 
      CGSize size = self.rotatableImageViewContainingScrollView.frame.size; 
      [self.rotatableImageViewContainingScrollView scrollRectToVisible:CGRectMake(currentOffset.x, currentOffset.y, size.width, size.height) animated:NO]; //bool value in animated should be false. 
      _prevMaxOffset = CGPointMake(currentMaxOffset.x, currentMaxOffset.y); 
      _previousLocation = CGPointMake(touchLocation.x, touchLocation.y); // added this line in the answer. 
     } 
      break; 
     case UIGestureRecognizerStateCancelled: 
     case UIGestureRecognizerStateFailed: 
     case UIGestureRecognizerStateEnded: [fourWayArrowImageView removeFromSuperview]; break; 
    } 
} 

所以在縮放模式滾動現在正是就像照片應用程序。

希望這有助於

+0

你在一個非常迂迴的方式這樣做。你應該看看Apple的PhotoScroller示例代碼。您可以使用分頁滾動視圖並使用內置於UIScrollView的標準縮放/平移支持。你不需要自己做所有這些工作。 –

+0

@MikeWeller謝謝麥克..但我有一個客戶端要求做任何縮放級別的滑動旋轉..這不能與滾動視圖的默認縮放和滾動,因爲你無法滑動時,它是放大模式。所以我不得不這樣做 –

相關問題