2016-03-15 54 views
2

在我的应用程序中使用UIControlpage。加载图像并成功显示在页面控制中。但我想显示图像动画像从右到左移动。使用定时器调用页面控制并增加阵列数量。UIpagecontrol需要动画Rigth到左边

////使用NSTimer调用函数来显示动画。

[NSTimer scheduledTimerWithTimeInterval:5.0 
            target:self 
            selector:@selector(loadNextController) 
            userInfo:nil 
            repeats:YES]; 

ScrollViewPage = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 20, frameX, frameY)]; 
ScrollViewPage.pagingEnabled = YES; 
ScrollViewPage.backgroundColor=[UIColor redColor]; 
ScrollViewPage.delegate = self; 
ScrollViewPage.backgroundColor = [UIColor clearColor]; 
ScrollViewPage.contentSize = CGSizeMake(frameX, frameY); 
ScrollViewPage.showsHorizontalScrollIndicator = NO; 


ScrollViewPage.pagingEnabled = YES; 
pageControl = [[UIPageControl alloc] init]; 
pageControl.frame = CGRectMake(100,self.view.frame.size.height-100,self.view.frame.size.width-200,100); 
pageControl.numberOfPages = [_pageImages count]; 
pageControl.currentPage = 0; 

[self.view addSubview: ScrollViewPage]; 

for(int i = 0; i < [_pageImages count]; i++) 
    { 

     NSURL *url = [NSURL URLWithString:[_pageImages objectAtIndex:i]]; 
     NSData *data = [[NSData alloc] initWithContentsOfURL:url]; 
     UIImage *tmpImage = [UIImage imageWithData:data]; 
     _backgroundImageView = [[UIImageView alloc] initWithImage:tmpImage]; 
     _backgroundImageView.frame = CGRectMake(frameX * i, 0.0, frameX, frameY); 
     [ScrollViewPage addSubview:_backgroundImageView]; 


    } 

ScrollViewPage.contentSize = CGSizeMake(frameX*[_pageImages count], frameY); 
pageControl.backgroundColor=[UIColor orangeColor]; 
[self.view addSubview:pageControl]; 
[pageControl addTarget:self action:@selector(pageTurn:) forControlEvents:UIControlEventValueChanged]; 

/////这里所说的定时器功能

- (void)loadNextController 
{ 
    pageControl.currentPage = (pageControl.currentPage+1)%self.pageImages.count; 
    ScrollViewPage.contentOffset = CGPointMake(pageControl.currentPage * self.view.bounds.size.width, 0); 
} 

在这里,我需要显示留给了uipagecontrol正确的举措。帮助我..

回答

1

一般而言,您应该始终写出您当前的结果,期望的结果以及您尝试的结果。

我只能猜测你面临的问题是没有动画。 滚动视图有一个方法setContentOffset:animated,您可以使用该方法为转换设置动画。另一种方法是使用UIViewanimateWithDuration并设置方法块中的偏移量。

尝试这些...

[ScrollViewPage setContentOffset:CGPointMake(pageControl.currentPage * self.view.bounds.size.width, 0) animated:YES]; 

或者

[UIView animateWithDuration:0.2 animations:^{ 
     ScrollViewPage.contentOffset = CGPointMake(pageControl.currentPage * self.view.bounds.size.width, 0); 
}]; 

编辑任何或全部:循环图像。

要循环显示图像,您当时只需要在滚动视图上显示2张图像。我会用3给你看这个概念,你可以尝试和它一起玩。

NSArray *allImages; 
NSInteger currentIndex = 0; 
UIScrollView *scrollView; 
UIImageView *imageViews[3]; 

- (void)begin { 

    scrollView.contentSize = CGSizeMake(scrollView.frame.size.width*3.0, scrollView.frame.size.height); // always have 3 images at the time 
    scrollView.contentOffset = CGPointMake(scrollView.frame.size.width, 0.0); // put to center 

    for(int i=0; i<3; i++) { 
     UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(scrollView.frame.size.width*i, 0.0, scrollView.frame.size.width, scrollView.frame.size.height)]; 
     [scrollView addSubview:imageView]; 
     imageView[i] = imageView; 
    } 
    [self layoutForIndex:currentIndex]; 
} 
- (void)nextImage { 
    currentIndex++; 
    [self layoutForIndex:currentIndex]; 

    scrollView.contentOffset = CGPointZero; 
    [scrollView setContentOffset:CGPointMake(scrollView.frame.size.width, 0.0) animated:YES] 
} 

- (void)layoutForIndex:(NSInteger)index { 
    if(allImages.count > 0) { // we need at least one image to do something 
     for(int i=0; i<3; i++) { 
      // do the circling 
      NSInteger imageIndex = index-1+i; 
      while(imageIndex < 0) imageIndex+=allImages.count; 
      while(imageIndex >= allImages.count) imageIndex-=allImages.count; 

      imageViews[i].image = imageViews[imageIndex]; 
     } 
    } 
} 

但是一般来说,对于你在做什么,你甚至不需要滚动视图。接下来,您可以简单地在当前旁边添加另一个图像视图,然后使用动画重新定位这两个图像视图。

+0

非常感谢你..为我工作.. –

+0

一个疑问..显示[_ImageImages数组]中的所有图像后,如何移动uipagecontrol中的第一个图像没有动画。这意味着所有图像都会返回并转到第一张图像。我需要它自动来到第一个图像。帮助我... –

+0

我为此添加了这个概念。我希望你能理解它。 –