2016-04-29 148 views
0

我想在滾動滑塊後將圖像縮小爲原始大小。 我正在使用REPagedScrollView庫來實現滑塊。 我有5個圖像滑塊。現在放大第一張圖像,然後立即滾動滑塊查看第二張圖像。然後返回查看第一張圖片。第一張圖像不縮小。該圖像仍處於放大模式。 我該如何解決這個問題。縮放圖像,然後滾動圖像查看下一張圖像,上一張圖像不縮小

在此先感謝。

我正在使用下面的代碼。

-(void)slideShow 
{ 
    [scrollView removeFromSuperview]; 
    scrollView = [[REPagedScrollView alloc] initWithFrame:self.scrollVw.bounds]; 
    scrollView.delegate=self; 
    scrollView.pageControl.pageIndicatorTintColor=[UIColor clearColor]; 
    scrollView.pageControl.currentPageIndicatorTintColor=[UIColor clearColor]; 
    current_page=0; 

    for(int i=0;i<[self.PhotoImgArr count];i++){ 


     // Image view 
     image_scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0,0,self.scrollVw.frame.size.width,self.scrollVw.frame.size.height)]; 
     self.image_view = [[AsyncImageView alloc] initWithFrame:CGRectMake(0,0, image_scroll.frame.size.width, image_scroll.frame.size.height)]; 

     self.image_view.backgroundColor=[UIColor clearColor]; 
    // self.image_view.contentMode = UIViewContentModeScaleAspectFit; 
     self.image_view.clipsToBounds = true; 
     self.image_view.userInteractionEnabled = YES; 

     //self.image_view.image=[UIImage imageNamed:@"Stars"]; 
     self.image_view.imageURL =[NSURL URLWithString:[self.PhotoImgArr objectAtIndex:i]]; 
     doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)]; 
     [doubleTap setNumberOfTapsRequired:2]; 

     [self.image_view addGestureRecognizer:doubleTap]; 
//  twoFingerPinch = [[UIPinchGestureRecognizer alloc] 
//       initWithTarget:self 
//       action:@selector(twoFingerPinch:)]; 
//  [self.image_view addGestureRecognizer:twoFingerPinch]; 
     [image_scroll setDelegate:self]; 
     [image_scroll setShowsHorizontalScrollIndicator:NO]; 
     [image_scroll setShowsVerticalScrollIndicator:NO]; 

     image_scroll.tag=i+1; 
     // image_scroll.backgroundColor=[UIColor purpleColor]; 
     image_scroll.autoresizingMask=UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight; 
     self.image_view.autoresizingMask=UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight; 

     image_scroll.contentSize = CGSizeMake(self.image_view.bounds.size.width-500, self.image_view.bounds.size.height-300); 
     image_scroll.decelerationRate = UIScrollViewDecelerationRateFast; 
     [image_scroll setMaximumZoomScale:4.0]; 
     // image_scroll.minimumZoomScale = 1.0; 
     [image_scroll setZoomScale:[image_scroll minimumZoomScale]]; 
     //image_scroll.zoomScale = 1.0; 
     [image_scroll addSubview:self.image_view]; 

     [scrollView addPage:image_scroll]; 

    } 
    scrollView.autoresizingMask=UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight; 
    scrollView.tag=0; 
    [self.scrollVw addSubview:scrollView]; 

    [scrollView scrollToPageWithIndex:self.selected_index animated:YES]; 
} 

- (void)twoFingerPinch:(UIPinchGestureRecognizer *)recognizer 
{ 
    NSLog(@"twoFingerPinch"); 
    float newScale = [image_scroll zoomScale]/2.0; 
    CGRect zoomRect = [self zoomRectForScale:newScale withCenter:[recognizer locationInView:recognizer.view]]; 
    [image_scroll zoomToRect:zoomRect animated:YES]; 
} 

- (void)handleDoubleTap:(UIGestureRecognizer *)gestureRecognizer { 
    // zoom in 
    NSLog(@"handleDoubleTap"); 
    UIScrollView *imageScroll = (UIScrollView *)[self.view viewWithTag:self.selected_index+1]; 

    float newScale = [imageScroll zoomScale] * 2.0; 

    if (newScale > imageScroll.maximumZoomScale){ 
     newScale = imageScroll.minimumZoomScale; 
     CGRect zoomRect = [self zoomRectForScale:newScale withCenter:[gestureRecognizer locationInView:gestureRecognizer.view]]; 

     [imageScroll zoomToRect:zoomRect animated:YES]; 

    } 
    else{ 

     newScale = imageScroll.maximumZoomScale; 
     CGRect zoomRect = [self zoomRectForScale:newScale withCenter:[gestureRecognizer locationInView:gestureRecognizer.view]]; 

     [imageScroll zoomToRect:zoomRect animated:YES]; 
    } 
} 

#pragma mark Utility methods 

- (CGRect)zoomRectForScale:(float)scale withCenter:(CGPoint)center { 
    CGRect zoomRect; 

    // the zoom rect is in the content view's coordinates. 
    // At a zoom scale of 1.0, it would be the size of the imageScrollView's bounds. 
    // As the zoom scale decreases, so more content is visible, the size of the rect grows. 
    zoomRect.size.height = [image_scroll frame].size.height/scale; 
    zoomRect.size.width = [image_scroll frame].size.width/scale; 

    // choose an origin so as to get the right center. 
    zoomRect.origin.x = center.x - (zoomRect.size.width/2.0); 
    zoomRect.origin.y = center.y - (zoomRect.size.height/2.0); 

    return zoomRect; 
} 

- (void)scrollViewDidScroll:(UIScrollView *)scrollView1 
{ 
    UIScrollView *imageScroll = (UIScrollView *)[self.view viewWithTag:self.selected_index+1]; 
    if(scrollView1.tag ==0){ 
     CGFloat pageWidth = scrollView1.frame.size.width; 
     current_page = floor((scrollView1.contentOffset.x - pageWidth/2.0)/pageWidth) + 1; 
     self.selected_index=current_page; 
     self.lblHeader.text=[NSString stringWithFormat:@"%ld of %lu",self.selected_index+1,(unsigned long)[self.PhotoImgArr count]]; 

    } 
    else 
    { 
     CGFloat offsetX = (imageScroll.bounds.size.width > imageScroll.contentSize.width)? 
     (imageScroll.bounds.size.width - imageScroll.contentSize.width) * 0.5 : 0.0; 
     CGFloat offsetY = (imageScroll.bounds.size.height > imageScroll.contentSize.height)? 
     (imageScroll.bounds.size.height - imageScroll.contentSize.height) * 0.5 : 0.0; 
     [scrollView1.subviews objectAtIndex:0].center = CGPointMake(imageScroll.contentSize.width * 0.5 + offsetX,imageScroll.contentSize.height * 0.5 + offsetY); 
    } 

} 

-(UIView *) viewForZoomingInScrollView:(UIScrollView *)scrollView1 
{ 
    if([scrollView1.subviews count]>0){ 
     return [scrollView1.subviews objectAtIndex:0]; 

    } 
    return nil; 
} 

回答

2

OK首先,不使用REPagedScrollView代替ü可以設置頁面通過改變視圖的x位置滾動型,併爲您的zooming是有用的,如果u使用REPagedScrollView有一定的併發症,美不能得到的子視圖的爲滾動視圖,而不是u得到一個UIView實例

更好u使用正常UIScrollView這是對你的情況下,還設置UIPageControl,因爲它們是在REPagedScrollView作爲單獨的視圖控制器的觀點做。

首先設置像下面

- (void)viewDidLoad { 
[super viewDidLoad]; 
// Do any additional setup after loading the view, typically from a nib. 
_scrollPager = [[UIScrollView alloc] initWithFrame:self.view.bounds]; 
_scrollPager.delegate=self; 
_scrollPager.pagingEnabled = YES; 
_current_page=0; 
_prev_next_index = 0; 

_PhotoImgArr = [[NSMutableArray alloc] initWithObjects:@"00.jpg",@"01.jpg",@"02.jpg",@"03.jpg", nil]; 
CGSize totalContentSize = CGSizeMake(self.view.bounds.size.width [_PhotoImgArr count], self.view.bounds.size.height); 
_scrollPager.contentSize = totalContentSize; 
[self.view addSubview:_scrollPager]; 
[self setUpPager]; 
} 

- (void)setUpPager 
{ 
     for(int i=0;i<[self.PhotoImgArr count];i++) 
    { 
    // Image view 
    UIScrollView *image_scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(i * self.view.bounds.size.width, 0, self.view.bounds.size.width, self.view.bounds.size.height)]; 
    UIImageView *image_view = [[UIImageView alloc] initWithFrame:image_scroll.bounds]; 
    image_view.backgroundColor=[UIColor clearColor]; 
    image_view.clipsToBounds = true; 
    image_view.image = [UIImage imageNamed:_PhotoImgArr[i]]; 

    UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)]; 
    [doubleTap setNumberOfTapsRequired:2]; 
    [image_scroll setDelegate:self]; 
    [image_scroll setShowsHorizontalScrollIndicator:NO]; 
    [image_scroll setShowsVerticalScrollIndicator:NO]; 

    image_scroll.tag=i; 
    image_scroll.autoresizingMask=UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight; 
    image_view.autoresizingMask=UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight; 

    image_scroll.contentSize = image_scroll.bounds.size; 
    image_scroll.decelerationRate = UIScrollViewDecelerationRateFast; 

    image_scroll.minimumZoomScale = 1.0f; 
    image_scroll.maximumZoomScale = 3.0f; 

    [image_scroll addSubview:image_view]; 
    [image_scroll addGestureRecognizer:doubleTap]; 
    [_scrollPager addSubview:image_scroll]; 
    } 
    _scrollPager.autoresizingMask=UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight; 
    _scrollPager.tag=100; 
} 

在同一個視圖控制器處理的雙擊動作時像下面的頁面

- (void)handleDoubleTap:(UIGestureRecognizer *)gestureRecognizer 
{ 
    // zoom in 
    NSLog(@"handleDoubleTap"); 
    if(gestureRecognizer.view == _scrollPager) return; 
    UIScrollView *doubleTapScroll = [self getImageScroller]; 
    if(doubleTapScroll) 
    { 
     //zoom 
     CGPoint pointInView = [gestureRecognizer locationInView:self.view]; 

     CGFloat newZoomScale = doubleTapScroll.zoomScale * 1.5f; 

     newZoomScale = MIN(newZoomScale, doubleTapScroll.maximumZoomScale); 
     CGRect rectToZoomTo = CGRectZero; 
     CGFloat currentZoomScale = doubleTapScroll.zoomScale; 
     if(currentZoomScale == 1.0f) 
     { 
     rectToZoomTo = [self zoomRectForScrollView:doubleTapScroll withScale:newZoomScale withCenter:pointInView];//CGRectMake(x, y, w, h); 
     } 
     else if (currentZoomScale >= 1.0f) 
     { 
      rectToZoomTo = [self zoomRectForScrollView:doubleTapScroll withScale:1 withCenter:self.view.center]; 
     } 
     [doubleTapScroll zoomToRect:rectToZoomTo animated:YES]; 
    } 
} 

一件事了滾動的你沒有需要處理捏,它將自動處理縮放。

最終處理變焦的

#pragma mark Utility methods 
- (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; 
} 

- (void)scrollViewDidScroll:(UIScrollView *)scrollView1 
{ 
} 

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView 
{ 
    if(scrollView == _scrollPager) 
    { 
    float currentPage = scrollView.contentOffset.x /scrollView.frame.size.width; 
    if(currentPage != _current_page) 
    { 
     _prev_next_index = _current_page; 
     _current_page = ceil(currentPage); 
     [self resetScrollerAtViewIndex:_prev_next_index]; 
    } 
    } 
} 


-(UIView *) viewForZoomingInScrollView:(UIScrollView *)scrollView1 
{ 
    if(scrollView1 == _scrollPager) 
     return nil; 
    else 
    { 
     UIScrollView *image_scroll = [self getImageScroller]; 
     if(image_scroll) 
      if([image_scroll.subviews count]>0) 
      { 
       return [image_scroll.subviews objectAtIndex:0]; 
      } 
    } 
    return nil; 
} 

- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

- (UIScrollView *)getImageScroller 
{ 
    return [_scrollPager viewWithTag:_current_page]; 
} 

- (void)resetScrollerAtViewIndex:(NSInteger)index 
{ 

    UIScrollView *scrollView = [_scrollPager viewWithTag:index]; 
    CGFloat zoomScale = scrollView.zoomScale; 
    if(zoomScale != 1.0) 
     [scrollView setZoomScale:1 animated:YES]; 
} 

編輯變焦出來的時候返回上一頁或下一頁 只需更換以下方法

- (void)resetScrollerAtViewIndex:(NSInteger)index 
{ 
    //CGFloat zoomScale = 1.0; 
    UIScrollView *scrollView = [_scrollPager viewWithTag:index]; 
    CGRect rectToZoomTo = CGRectZero; 

    rectToZoomTo = [self zoomRectForScrollView:scrollView withScale:1 withCenter:self.view.center]; 
    [scrollView zoomToRect:self.view.bounds animated:YES]; 
    [scrollView setContentOffset:CGPointZero]; 
    [scrollView setContentSize:self.view.bounds.size]; 
} 
+0

嗨@Shankar BS只有第一形象越來越在視圖中顯示,但滾動禁用,我們無法滾動圖像..任何線索請。 – iShwar