2015-09-07 72 views
0

我想通過PinchGestureRecognizer進行縮放功能。 我可以通過此代碼進行縮放,但只要我捏出來,imageView就會恢復。 我想做的功能是,如果我有時縮放imageView,它不會每次都恢復。 而代碼scrollView.minimumZoomScale = 3.0,scrollView.maximumZoomScale = 6.0不起作用。我應該如何在PinchGestureRecognizer上保持縮放比例?

我該怎麼辦?

這裏是我的代碼

 scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds]; 
     scrollView.frame = self.view.bounds; 
     scrollView.backgroundColor = [UIColor blueColor]; 
     scrollView.contentSize = CGSizeMake(imgView.bounds.size.width+100, imgView.bounds.size.height); 
     scrollView.frame = CGRectMake(0,50,320,500); 
     scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; 
     scrollView.pagingEnabled = YES; 
     scrollView.bouncesZoom = YES; 
     scrollView.minimumZoomScale = 3.0; 
     scrollView.maximumZoomScale = 6.0; 
     scrollView.showsHorizontalScrollIndicator = NO; 
     scrollView.showsVerticalScrollIndicator = NO; 
     [self.view addSubview:scrollView]; 

     UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] 
                initWithTarget:self action:@selector(handlePinchGesture:)]; 
     [scrollView addGestureRecognizer:pinchGesture]; 

      img =[UIImage imageNamed:[NSString stringWithFormat:@"a.jpg"]]; 
      imgView = [[UIImageView alloc]initWithImage:img]; 
      imgView.frame = CGRectMake(0,0, self.view.frame.size.width, 448); 
      imgView.userInteractionEnabled = YES; 
      [scrollView imgView]; 


- (void)handlePinchGesture:(UIPinchGestureRecognizer *)sender { 
    CGFloat factor = [(UIPinchGestureRecognizer *)sender scale]; 
    imgView.transform = CGAffineTransformMakeScale(factor, factor); 
    NSLog(@"factor %f",factor); 

} 
+0

可能重複 - iPhone iOS](http://stackoverflow.com/questions/5150642/max-min-scale-of-pinch-zoom-in-uipinchgesturerecogni zer-iphone-ios) –

+0

我可以做到!非常感謝! – zmbdr

回答

0

嘗試使用CGAffineTransformScale,而不是像:

imgView.transform = CGAffineTransformScale(imgView.transform, factor, factor); 
+0

我可以做到這一點:)但它非常非常第一次.... – zmbdr

+0

而最大和最小尺寸仍然無限... – zmbdr

+0

minimumZoomScale沒有影響,當我使用你自己的捏手勢。在完成變換之後,確保imgView的框架處於某個限定的範圍內,例如,確保寬度<600和高度<400或其他,如果它更大,則縮小其大小。也許[這樣的事情](http://stackoverflow.com/a/5154514/1219956) – Fonix

0

試試這個代碼

UIScrollView *scrollview =[[UIScrollView alloc]initWithFrame:CGRectMake(0,0,320,imageViewHeight)]; 
    scrollview.showsVerticalScrollIndicator=YES; 
    scrollview.scrollEnabled=YES; 
    scrollview.maximumZoomScale = 3.0; 
    scrollview.delegate = self; 
    scrollview.userInteractionEnabled=YES; 
    [self.view addSubview:scrollview];` 
0

注意:這僅僅是另一種方式來實現您的要求

有一件事,如果你使用UIScrollView,沒有必要添加UIPinchGestureRecognizer默認滾動視圖它有捏手勢。

用於變焦有一個委託方法需要重寫

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView 

- (CGRect)zoomRectForScrollView:(UIScrollView *)scrollView withScale:(float)scale withCenter:(CGPoint)center 

例如

//initilization 
    scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds]; 
    scrollView.frame = self.view.bounds; 
    scrollView.backgroundColor = [UIColor blueColor]; 
    scrollView.contentSize = CGSizeMake(imgView.bounds.size.width+100, imgView.bounds.size.height); 
    scrollView.frame = CGRectMake(0,50,320,500); 
    scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; 
    scrollView.pagingEnabled = YES; 
    scrollView.bouncesZoom = YES; 
    scrollView.minimumZoomScale = 3.0; 
    scrollView.maximumZoomScale = 6.0; 
    scrollView.showsHorizontalScrollIndicator = NO; 
    scrollView.showsVerticalScrollIndicator = NO; 

    scrollView.delegate = self; /* add this line */ 

    [self.view addSubview:scrollView]; 


//this view will be scaled accordingly 
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView 
{ 
    return imgView; //return the view which is the subview of scroll view need to be zoomed 
} 

//it is the rect for the scaling 
- (CGRect)zoomRectForScrollView:(UIScrollView *)scrollView withScale:(float)scale withCenter:(CGPoint)center 
{ 
    CGRect zoomRect; 

    zoomRect.size.height = scrollView.frame.size.height/scale; 
    zoomRect.size.width = scrollView.frame.size.width/scale; 

    zoomRect.origin.x = center.x - (zoomRect.size.width/2.0); 
    zoomRect.origin.y = center.y - (zoomRect.size.height/2.0); 
    return zoomRect; 
} 
捏放大的在UIPinchGestureRecognizer [最大值/最小值量表
相關問題