2014-05-15 165 views
1

我正在通過字符串在UIImageView上加載圖像,我希望水平滾動以查看UIImageView上的圖像。 在我的xib文件中,我有一個滾動視圖,其中有一個圖像視圖。我使用這段代碼,但我的頁面不滾動,只有字符串中的第一個圖像被加載。任何人都可以請幫忙。使用水平滾動在圖像視圖中加載圖像

代碼:

-(void)viewDidLoad 
    { 
     count=1; 
     // imagesName = [[NSMutableArray alloc]initWithObjects :@"election_band_base.jpg", @"ElectionBoxAPPA-Hindi(1).jpg", @"photos.png", @"business.png", @"health.png", nil]; 

     imagesName1 = [NSString stringWithFormat :@"election_band_base.jpg", @"ElectionBoxAPPA-Hindi(1).jpg", @"photos.png", @"business.png", @"health.png", nil]; 

     [imagesName addObject:imagesName1]; 

       items = [[NSMutableArray alloc]init]; 


     // [_imageView1.image setImage=imagesName1]; 

     [super viewDidLoad]; 
     // Do any additional setup after loading the view, typically from a nib. 
     // _imageView1.image=[UIImage animatedImageWithImages:imagesName duration:0]; 

     _imageView1.image=[UIImage imageNamed:imagesName1 ]; 

     [self loadScrollView]; 

    } 



     -(void)loadScrollView 

     { 

      scrollView.contentSize = CGSizeMake(0, scrollView.frame.size.height); 



      NSMutableArray *controllers = [[NSMutableArray alloc] init]; 

      for (unsigned i = 0; i < [imagesName count]; i++) { 

       [controllers addObject:[NSNull null]]; 

      } 

      self.viewControllers = controllers; 
      count=1; 


      // a page is the width of the scroll view 

      scrollView.pagingEnabled = YES; 

      scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * [imagesName count], scrollView.frame.size.height); 
      //scrollView.contentSize = CGSizeMake(900,80); 


      scrollView.showsHorizontalScrollIndicator =YES; 

      scrollView.showsVerticalScrollIndicator = YES; 

      scrollView.scrollsToTop = NO; 

      scrollView.delegate = self; 


      pageControl.numberOfPages = [imagesName count]; 

      pageControl.currentPage = 0; 





      // pages are created on demand 

      // load the visible page 

      // load the page on either side to avoid flashes when the user starts scrolling 

      [self loadScrollViewWithPage:0]; 

      [self loadScrollViewWithPage:1]; 

     } 







     - (void)loadScrollViewWithPage:(int)page { 

      if (page < 0) return; 

      if (page >= [imagesName count]) 

       return; 


      // replace the placeholder if necessary 

      controller = [viewControllers objectAtIndex:page]; 

      if ((NSNull *)controller == [NSNull null]) { 

       NSString *deviceType = [UIDevice currentDevice].model; 

       if([deviceType isEqualToString:@"iPhone"]) 

       { 

        controller = [[MyViewController alloc] initWithNibName:@"MyViewController" bundle:nil]; 

       } 

       else{ 

        controller = [[MyViewController alloc] initWithNibName:@"MyViewController_ipad" bundle:nil]; 

       } 

       [controller initWithPageNumber:page]; 

       [controller setArrData:imagesName]; 

       [viewControllers replaceObjectAtIndex:page withObject:controller]; 



      } 


      // add the controller's view to the scroll view 

      if (nil == controller.view.superview) { 

       CGRect frame = scrollView.frame; 

       frame.origin.x = frame.size.width * page; 

       frame.origin.y = 0; 

       controller.view.frame = frame; 

       [scrollView addSubview:controller.view]; 

      } 

     } 

     - (void)unloadScrollViewWithPage:(int)page { 

      if (page < 0) return; 

      if (page >= [imagesName count]) return; 



      controller = [viewControllers objectAtIndex:page]; 



      if ((NSNull *)controller != [NSNull null]) { 

       if (nil != controller.view.superview) 

        [controller.view removeFromSuperview]; 



       [viewControllers replaceObjectAtIndex:page withObject:[NSNull null]]; 



      } 

     } 



     - (void)scrollViewDidScroll:(UIScrollView *)sender { 

      // We don't want a "feedback loop" between the UIPageControl and the scroll delegate in 

      // which a scroll event generated from the user hitting the page control triggers updates from 

      // the delegate method. We use a boolean to disable the delegate logic when the page control is used. 

      if (pageControlUsed) { 

       // do nothing - the scroll was initiated from the page control, not the user dragging 

       return; 

      } 


      // Switch the indicator when more than 50% of the previous/next page is visible 

      CGFloat pageWidth = scrollView.frame.size.width; 

      int page = floor((scrollView.contentOffset.x - pageWidth/2)/pageWidth) + 1; 

      pageControl.currentPage = page; 

      // NSLog(@"current page %d",page); 


      // load the visible page and the page on either side of it (to avoid flashes when the user starts scrolling) 

      [self unloadScrollViewWithPage:page - 2]; 

      [self loadScrollViewWithPage:page - 1]; 

      [self loadScrollViewWithPage:page]; 

      [self loadScrollViewWithPage:page + 1]; 

      [self unloadScrollViewWithPage:page + 2]; 

      count=page+1; 




      // A possible optimization would be to unload the views+controllers which are no longer visible 

     } 



     // At the begin of scroll dragging, reset the boolean used when scrolls originate from the UIPageControl 

     - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollViewLoc 

     { 

      CGFloat pageWidth = scrollViewLoc.frame.size.width; 

      CGPoint translation = [scrollViewLoc.panGestureRecognizer translationInView:scrollViewLoc.superview]; 

      int page = floor((scrollViewLoc.contentOffset.x - pageWidth/2)/pageWidth) + 1; 





           } 



     // At the end of scroll animation, reset the boolean used when scrolls originate from the UIPageControl 

     - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { 

      pageControlUsed = NO; 

     } 

- (IBAction)changePage:(id)sender 
{ 
    int page = pageControl.currentPage; 


      // load the visible page and the page on either side of it (to avoid flashes when the user starts scrolling) 

      [self loadScrollViewWithPage:page - 1]; 

      [self loadScrollViewWithPage:page]; 

      [self loadScrollViewWithPage:page + 1]; 



      // update the scroll view to the appropriate page 

      CGRect frame = scrollView.frame; 

      frame.origin.x = frame.size.width * page; 

      frame.origin.y = 0; 

      [scrollView scrollRectToVisible:frame animated:YES]; 



      // Set the boolean used when scrolls originate from the UIPageControl. See scrollViewDidScroll: above. 

      pageControlUsed = YES; 



     } 
+0

你想要滾動多少圖片?增加你的寬度併爲你的滾動視圖添加以下幾行self.scrView.showsHorizo​​ntalScrollIndicator = YES; self.scrView.showsVerticalScrollIndicator = NO; self.scrView.pagingEnabled = YES; – karthikeyan

+0

我有一個包含近50幅圖像的圖像陣列。 –

+0

我已經添加這些代碼行 –

回答

3

所以,你要建庫視圖:

對於此實現以下步驟:

  1. UIScrollView添加到視圖控制器的視圖中。

  2. viewDidLoad方法:在「ImageArray」中加載圖像的名稱。 (我假設你所有的圖片都命名爲 「img1.png」, 「img2.png」, 「img3.png」,....)

    ImageArray=[[NSMutableArray alloc]init]; 
    
    for (int i=0; i<19; i++) { 
        NSString *imgtext=[[NSString alloc]initWithFormat:@"img%d",i+1]; 
        [ImageArray addObject:imgtext]; 
    } 
    
  3. viewWillAppear方法,添加以下代碼:

    for (int i = 0; i < ImageArray.count; i++) { 
    CGRect frame; 
    frame.origin.x = self.scrollview.frame.size.width * i; 
    frame.origin.y = 0; 
    frame.size = self.scrollview.frame.size; 
    UIView *subview = [[UIView alloc] initWithFrame:frame]; 
    
    UIImage *image = [UIImage imageNamed: [NSString stringWithFormat:@"%@.png",[ImageArray objectAtIndex:i]]]; 
    UIImageView *imageView = [[UIImageView alloc] initWithImage: image]; 
    [imageView setFrame:CGRectMake(0, 0, frame.size.width,frame.size.height)]; 
    [subview addSubview:imageView]; 
    [self.scrollview addSubview:subview]; 
    } 
    
    self.scrollview.contentSize = CGSizeMake(self.scrollview.frame.size.width * ImageArray.count, self.scrollview.frame.size.height); 
    
    self.scrollview.contentOffset=CGPointMake (self.scrollview.frame.size.width, 0); 
    

希望它能幫助。

+1

謝謝它工作!!!!!!!但在索引0的圖像正在加載總是 –

+0

@ pooja_1205,你怎麼進入這個視圖?我的意思是,在點擊前一個屏幕中的縮略圖視圖後,您是否顯示該視圖? –

0

我在我的應用程序中,我已經添加了多張與水平滾動做了一個滾動視圖。下面是我只需在視圖控制器這樣做可以幫助你的函數..

- (void)makeFriendsScrollView{ 
    int friendsCount = 10; 
    float xPos = 10; 
    for (int i = 0; i < friendsCount; i++) { 


     UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(xPos,25 , 54, 54)]; 
     imgView.image = [UIImage imageNamed:@"user_default.png"]; 
     [imgView.layer setCornerRadius:27.0]; 
     imgView.clipsToBounds = YES; 

     [scrollViewFriends addSubview:imgView]; 
     xPos = xPos + 64; 

    } 
    scrollViewFriends.contentSize = CGSizeMake(friendsCount*65, 90); 
    [scrollViewFriends.layer setBorderColor:[UIColor lightGrayColor].CGColor]; 
    [scrollViewFriends.layer setBorderWidth:0.5f]; 
} 
+0

此代碼不適用於me.only imageview和標籤在視圖中可見。 –

+0

你還想展示什麼? – Ashutosh

+0

我想查看水平滾動的圖像。滾動下一個圖像應該會出現。 –

0

檢查出來...並更改根據您在viewWillAppear中的方法這requirement..try

self.arraname=[NSArray arrayWithObjects:@"1.jpg",@"2.jpg",@"3.jpg", nil]; 
// int friendsCount = 10; 
    float xPos = 160; 
    float x1=0; 
    float y=60; 
    for (int i = 0; i < [self.arraname count]; i++) 
    { 

     x1=xPos+(260*i); 
     _imgView = [[UIImageView alloc] initWithFrame:CGRectMake(x1, y, 54, 54)]; 
     _imgView.image = [UIImage imageNamed:[self.arraname objectAtIndex:i]]; 
     [_imgView.layer setCornerRadius:27.0]; 
     _imgView.clipsToBounds = YES; 

     [self.scroll addSubview:_imgView]; 

    } 
    NSLog(@"%f",x1); 
    self.scroll.contentSize=CGSizeMake(x1+200, 0); 
    self.scroll.showsHorizontalScrollIndicator = YES; 
    self.scroll.showsVerticalScrollIndicator=NO; 
    self.scroll.pagingEnabled = YES;