2016-07-18 87 views
0

我在viewDidLoad方法中使用下面的代碼。我期待圖像在被捏住時縮放,但不會縮放。UIImageview沒有得到放大

[self.displayImage setUserInteractionEnabled:YES]; 

    NSString *savedValue = [[NSUserDefaults standardUserDefaults] 
          stringForKey:@"imageUrl"]; 
    NSLog(@"saved %@",savedValue); 

    _displayImage.image = nil; 

     dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^(void) { 
      NSData *data0 = [NSData dataWithContentsOfURL: [NSURL URLWithString:savedValue]]; 
      UIImage *image = [UIImage imageWithData: data0]; 

      dispatch_sync(dispatch_get_main_queue(), ^(void) { 
       _displayImage.image = image; 
      }); 
     }); 

    self.displayReceivedImageScrollView.delegate = self; 


    [self.displayReceivedImageScrollView addSubview:_displayImage]; 
    _displayReceivedImageScrollView.contentSize = _displayImage.frame.size; 


    _displayReceivedImageScrollView.scrollEnabled = YES; 

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


     self.displayReceivedImageScrollView.contentOffset = CGPointMake(self.displayReceivedImageScrollView.bounds.size.width, 0); 

     UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(slideToLeftWithGestureRecognizer:)]; 
     UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(slideToRightWithGestureRecognizer:)]; 
     [swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft]; 
     [swipeRight setDirection:UISwipeGestureRecognizerDirectionRight]; 

     [self.displayImage addGestureRecognizer:swipeLeft]; 
     [self.displayImage addGestureRecognizer:swipeRight]; 

我在storyboard中添加了imageview。但我在運行期間爲了啓用縮放而擴大了滾動視圖。我無法縮放它。 swipeLeft或swipeRight方法永遠不會被調用。我怎樣才能解決這個問題?

+0

爲什麼你用滑動手勢? –

+0

我想放大它 – Learner

回答

2

對於變焦,您可以使用滾動視圖的委託方法的圖像:

-(void)setScrollView:(UIScrollView *)scrollView 
{ 
    _scrollView=scrollView; 
    _scrollView.minimumZoomScale=0.2; 
    _scrollView.maximumZoomScale=3.0; 
    _scrollView.delegate=self; 
} 
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView 
{ 
    return self.imageView; 
} 
-(void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale 
{ 
    [self.scrollView flashScrollIndicators]; 
} 
+0

我試過了。它工作正常,但它可以讓圖像放大超過其原始大小。我應該如何解決它?.. – Learner

+0

你應該設置滾動視圖的縮放比例:self.scrollView.zoomScale = 1; –

+0

謝謝兄弟。有用 – Learner