如何通过iphone中的下一个按钮滚动视图中的下一个图像

时间:2011-12-22 05:47:15

标签: iphone objective-c

我在滚动视图中有图像正在加载来自数组的图像我也希望这样当我按下一步按钮时,下一张图像应该出现我已经完成了下面的代码,用于在scrollview中加载图像我想通过按钮点击prevoius和下一张图像当我点击下一个按钮时,它不会显示下一个图像

-(void) loadNews{

 NSURL*myurl=url; 

  myurl = [myurl stringByReplacingOccurrencesOfString:@"\n" withString:@""];
  myurl = [myurl stringByTrimmingCharactersInSet:[NSCharacterSet   whitespaceCharacterSet]]; 



  NSURL*urlloaded= [[NSURL alloc]initWithString:myurl];
 //NSURL*url= [[NSURL alloc]initWithString:@"http://localhost:8888/RowOne.xml"];
  NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:urlloaded];

  //Initialize the delegate.

   RowTwoParser *parser = [[RowTwoParser alloc] initXMLParser];

  //Set delegate

   [xmlParser setDelegate:parser];   
   BOOL success = [xmlParser parse];

  if(success)

   NSLog(@"No Errors");

   else

  NSLog(@"Error Error Error!!!");



  int k=0;
  for (int  i=0; i < [appDelegate.articles 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];
   [self.scrollView addSubview:subView];
   RowTwo*aRowTwo=[appDelegate.articles objectAtIndex:i];



   CGRect mywebframe=CGRectMake(20, 60, 728, 800);

   UIImageView*imageView=[[UIImageView alloc] initWithFrame:mywebframe];  


   NSString*thumb2=aRowTwo.image;

   thumb2 = [thumb2 stringByReplacingOccurrencesOfString:@"\n" withString:@""];
   thumb2 = [thumb2 stringByTrimmingCharactersInSet:[NSCharacterSet   whitespaceCharacterSet]];



   NSURL *url = [NSURL URLWithString:thumb2];
   NSData *data = [[NSData alloc] initWithContentsOfURL:url];
   UIImage *image2 = [[UIImage alloc] initWithData:data];
   imageView.image =image2 ; 

   [subView addSubview:imageView];
   k++;
    }  

    //NSInteger numberofPages=10-j;
    NSInteger numberofPages=[appDelegate.articles count];
   self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width *   numberofPages, self.scrollView.frame.size.height);
  CGRect frame;
  //frame.origin.x = self.scrollView.frame.size.width * self.pageControl.currentPage;
  frame.origin.x = self.scrollView.frame.size.width * myindex;
  frame.origin.y = 0;
  frame.size = self.scrollView.frame.size;
  [self.scrollView scrollRectToVisible:frame animated:YES];



  [pageControl setNumberOfPages:numberofPages];
  [pageControl setActivePageColor:[UIColor clearColor]];
  [pageControl setInactivePageColor:[UIColor clearColor]];  

   }

下一个按钮代码

   -(IBAction)nextButton{
     for (int nextIndex=[pageControl currentPage]; nextIndex < [appDelegate.articles count]; nextIndex++) {


   CGRect frame;
   frame.origin.x = self.scrollView.frame.size.width * nextIndex;
   frame.origin.y = 0;
   frame.size = self.scrollView.frame.size;
   UIView*subView=[[UIView alloc ]initWithFrame:frame];
   [self.scrollView addSubview:subView];
   RowTwo*aRowTwo=[appDelegate.articles objectAtIndex:nextIndex];



   CGRect mywebframe=CGRectMake(20, 60, 728, 800);

   UIImageView*imageView=[[UIImageView alloc] initWithFrame:mywebframe];  


   NSString*thumb2=aRowTwo.image;

  thumb2 = [thumb2 stringByReplacingOccurrencesOfString:@"\n" withString:@""];
  thumb2 = [thumb2 stringByTrimmingCharactersInSet:[NSCharacterSet   whitespaceCharacterSet]];



   NSURL *url = [NSURL URLWithString:thumb2];
   NSData *data = [[NSData alloc] initWithContentsOfURL:url];
   UIImage *image2 = [[UIImage alloc] initWithData:data];
   imageView.image =image2 ; 

   [subView addSubview:imageView];

// k + \      }

  //NSInteger numberofPages=10-j;
  NSInteger numberofPages=[appDelegate.articles count];
  self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * numberofPages, self.scrollView.frame.size.height);
 CGRect frame;
 //frame.origin.x = self.scrollView.frame.size.width * self.pageControl.currentPage;
 frame.origin.x = self.scrollView.frame.size.width * myindex;
 frame.origin.y = 0;
  frame.size = self.scrollView.frame.size;
 [self.scrollView scrollRectToVisible:frame animated:YES];



  [pageControl setNumberOfPages:numberofPages];
  [pageControl setActivePageColor:[UIColor clearColor]];
  [pageControl setInactivePageColor:[UIColor clearColor]];  


 }

1 个答案:

答案 0 :(得分:0)

如果它们处于滚动视图中,则使用

CGPoint offset = CGPointMake(xPositionToScroll, yPositionToScroll);
[self.scrollView setContentOffset:topOffset animated:YES];

到某一点。