2011-05-18 170 views
6

我正在爲這個應用程序開發iOS 4.2,iPhone應用程序,我下載圖像並將它們保存在內部存儲(NSDocuments)中。如何在沒有UIScrollView的情況下放大或縮小UIImageView?

那麼,我在UIImageView中顯示第一個圖像。用戶可以在UIImageView(TouchesMoved)上拖動他們的手指,當用戶這樣做時,我加載其他圖像。如果用戶拖下來,我加載一個圖像,如果向上拖動,我加載其他圖像,並且還向右和向左。

這一切都完成了。但我想實現縮放。這是我的代碼,直到現:

initialDistance - >是在第一觸摸的手指之間的距離
finalDistance - >是手指之間的距離在每次移動
×時間 - >爲0
y - >是0

// this method calculate the distance between 2 fingers 
    - (CGFloat)distanceBetweenTwoPoints:(CGPoint)fromPoint toPoint:(CGPoint)toPoint { 
     float xPoint = toPoint.x - fromPoint.x; 
     float yPoint = toPoint.y - fromPoint.y; 

     return sqrt(xPoint * xPoint + yPoint * yPoint); 
    } 

     //------------------- Movimientos con los dedos ------------------------------------ 
     #pragma mark - 
     #pragma mark UIResponder 

      // First Touch 
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ 

NSSet *allTouches = [event allTouches]; 

switch ([allTouches count]) { 
    case 1: { //Single touch 

     //Get the first touch. 
     UITouch *touch1 = [[allTouches allObjects] objectAtIndex:0]; 

     switch ([touch1 tapCount]) 
     { 
      case 1: //Single Tap. 
      { 
       // Guardo la primera localización del dedo cuando pulsa por primera vez 
       //inicial = [touch1 locationInView:self.view]; 

      } break; 
      case 2: {//Double tap. 
       //Track the initial distance between two fingers. 
       //if ([[allTouches allObjects] count] >= 2) { 

       // oculto/o no, la barra de arriba cuando se hace un dobleTap 
       //[self switchToolBar]; 

      } break; 
     } 
    } break; 
    case 2: { //Double Touch 

     // calculo la distancia inicial que hay entre los dedos cuando empieza a tocar 
     UITouch *touch1 = [[allTouches allObjects] objectAtIndex:0]; 
     UITouch *touch2 = [[allTouches allObjects] objectAtIndex:1]; 

     initialDistance = [self distanceBetweenTwoPoints:[touch1 locationInView:[self view]] 
               toPoint:[touch2 locationInView:[self view]]]; 
    } 
    default: 
     break; 
} 
} 

// when the finger/s move to 
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 

NSSet *allTouches = [event allTouches]; 

switch ([allTouches count]) 
{ 
    case 1: { 



    } break; 
    case 2: { 
     //The image is being zoomed in or out. 

     UITouch *touch1 = [[allTouches allObjects] objectAtIndex:0]; 
     UITouch *touch2 = [[allTouches allObjects] objectAtIndex:1]; 

     //Calculate the distance between the two fingers. 
     CGFloat finalDistance = [self distanceBetweenTwoPoints:[touch1 locationInView:[self view]] 
                 toPoint:[touch2 locationInView:[self view]]]; 

     NSLog(@"Distancia Inicial :: %.f, Ditancia final :: %.f", initialDistance, finalDistance); 

     float factorX = 20.0; 
     float factorY = 11.0; 

     // guardo la posicion de los 2 dedos 
     //CGPoint dedo1 = [[[touches allObjects] objectAtIndex:0] locationInView:self.view]; 
     //CGPoint dedo2 = [[[touches allObjects] objectAtIndex:1] locationInView:self.view]; 

     // comparo para saber si el usuario esta haciendo zoom in o zoom out 
     if(initialDistance < finalDistance) { 
      NSLog(@"Zoom In"); 

      float newWidth = imagen.frame.size.width + (initialDistance - finalDistance + factorX); 
      float newHeight = imagen.frame.size.height + (initialDistance - finalDistance + factorY); 

      if (newWidth <= 960 && newHeight <= 640) { 
       /* 
       if (dedo1.x >= dedo2.x) { 
       x = (dedo1.x + finalDistance/2); 
       y = (dedo1.y + finalDistance/2); 
       } else { 
       x = (dedo2.x + finalDistance/2); 
       y = (dedo2.y + finalDistance/2); 
       } 
       */ 

       //x = (dedo1.x); 
       //y = (dedo1.y); 

       imagen.frame = CGRectMake(x, y, newWidth, newHeight); 
      } else { 
       imagen.frame = CGRectMake(x, y, 960, 640); 
      } 



     } 
     else { 
      NSLog(@"Zoom Out"); 

      float newWidth = imagen.frame.size.width - (finalDistance - initialDistance + factorX); 
      float newHeight = imagen.frame.size.height - (finalDistance - initialDistance + factorY); 

      if (newWidth >= 480 && newHeight >= 320) { 
       /* 
       if (dedo1.x >= dedo2.x) { 
       x = (dedo1.x + finalDistance/2); 
       y = (dedo1.y + finalDistance/2); 
       } else { 
       x = (dedo2.x + finalDistance/2); 
       y = (dedo2.y + finalDistance/2); 
       } 
       */ 
       //x -= (finalDistance - initialDistance + factorX); 
       //y -= (finalDistance - initialDistance + factorX); 

       //x = (dedo1.x); 
       //y = (dedo1.y); 

       imagen.frame = CGRectMake(x, y, newWidth, newHeight); 
      } else { 
       imagen.frame = CGRectMake(0, 0, 480, 320); 
      } 



     } 

     initialDistance = finalDistance; 

    } break; 
} 
} 

#pragma mark - 

非常感謝!

PD:如果UIScrollView有一種方法可以在不同的圖像之間移動,我也可以打開看一看。

+0

如果您打算使用拖放/平移事件移動到下一張圖像,用戶將如何導航縮放的圖像? – 2011-05-18 21:10:58

+0

他只能縮放,不能導航。 – 2011-05-19 07:04:49

回答

21

好的。您可以考慮使用UIScrollView,只是爲了使用它的縮放功能。

假設我們有一個scrollView和一個imageView,它們都具有相同的邊界。添加imageView作爲scrollView的子視圖。

[scrollView addSubview:imageView]; 
scrollView.contentSize = imageView.frame.size; 

僅支持縮放和在scrollViewviewController不是不平移將不得不採取UIScrollViewDelegate協議。

// Disabling panning/scrolling in the scrollView 
scrollView.scrollEnabled = NO; 

// For supporting zoom, 
scrollView.minimumZoomScale = 0.5; 
scrollView.maximumZoomScale = 2.0; 

... 

// Implement a single scroll view delegate method 
- (UIView*)viewForZoomingInScrollView:(UIScrollView *)aScrollView { 
    return imageView; 
} 

現在我們有縮放可用。對於滑動,您可以使用適當配置的UISwipeGestureRecognizers。創建一個手勢來處理每個滑動方向並將其添加到滾動視圖。

UISwipeGestureRecognizer *rightSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self selector:@selector(handleRightSwipe:)]; 
rightSwipe.direction = UISwipeGestureRecognizerDirectionRight; 
rightSwipe.numberOfTouchesRequired = 1; 
[scrollView addGesture:rightSwipe]; 
[rightSwipe release]; 

和檢索基於手勢適當的圖像,並使用imageView.image = yourImage;設置。

+0

好吧,這有效,但我想用touchesMoved一樣滑動,因爲我需要在移動手指時更改圖像。這適用於1個移動,1個圖像改變。 – 2011-05-19 08:41:49

+1

嗯..我可以建議你用Pan手勢識別器來替換Swipe。配置由平移手勢識別器觸發的選擇器的行爲與您的觸摸移動方法類似,因爲它包含您要使用的翻譯數據。但是這需要每個圖像視圖都包含在一個scrollView中。 – 2011-05-19 09:42:17

+2

或者,您可以查看'UIImageView'中的'transform'屬性。 – 2011-05-19 09:43:25

3

最後,在Deepak的幫助下,我使用UIImageView的transform屬性進行縮放。

我使用CGFloat在CGAffineTransform矩陣中的位置[0,0]來設置限制。我必須在放大時將CGFloat傳遞給字符串,因爲當我將它與0進行比較時,這總是如此。

最後這是我在touchesMoved代碼,如果它是有用的人:

// comparo para saber si el usuario esta haciendo zoom in o zoom out 
     if(initialDistance < finalDistance) { 
      NSLog(@"Zoom Out"); 

      CGAffineTransform transformer = CGAffineTransformScale(imagen.transform, 1.05, 1.05); 

      NSLog(@"transformer :: A: %.f, B: %.f, C: %.f, D: %.f", imagen.transform.a, imagen.transform.b, imagen.transform.c, imagen.transform.d); 

      if (transformer.a < 5) { 
       [UIView beginAnimations:nil context:NULL]; 
       [UIView setAnimationDuration: 0.2]; 
       imagen.transform = transformer; 
       [UIView setAnimationDelegate:self]; 
       [UIView commitAnimations]; 
      } 
     } 
     else { 
      NSLog(@"Zoom In"); 

      CGAffineTransform transformer = CGAffineTransformScale(imagen.transform, 0.95, 0.95); 

      NSLog(@"transformer :: A: %.f, B: %.f, C: %.f, D: %.f", transformer.a, transformer.b, transformer.c, transformer.d); 

      NSString *num = [NSString stringWithFormat:@"%.f", transformer.a]; 

      if (![num isEqualToString:@"0"]) { 
       [UIView beginAnimations:nil context:NULL]; 
       [UIView setAnimationDuration: 0.2]; 
       imagen.transform = transformer; 
       [UIView setAnimationDelegate:self]; 
       [UIView commitAnimations]; 
      } 
     } 

和當然,有很多感謝迪帕克的。

相關問題